Skip to content

Instantly share code, notes, and snippets.

@Flyingmana
Last active February 17, 2018 04:54
Show Gist options
  • Save Flyingmana/c83a09288b666225b5d0 to your computer and use it in GitHub Desktop.
Save Flyingmana/c83a09288b666225b5d0 to your computer and use it in GitHub Desktop.
a script to easily add a config to map all the magento URNs to their actual files in a project
<?php
/**
* license is MIT, you will find the text of it in the internet
*/
echo "start generation script\n";
function getAllSchemaFilePaths()
{
$basedir = getcwd().DIRECTORY_SEPARATOR;
$Directory = new RecursiveDirectoryIterator($basedir);
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.xsd$/i', RecursiveRegexIterator::GET_MATCH);
$paths = [];
foreach ($Regex as $k => $v) {
$paths[] = $k;
}
return $paths;
}
function transformXsdPathToMagentoUri($path)
{
$result = null;
$parts = explode(DIRECTORY_SEPARATOR, $path);
$parts = array_reverse($parts);
if (strpos($path, 'app/code') !== false) {
if ($parts[1] == 'etc') {
$result = 'urn:magento:module:'.$parts[3].'_'.$parts[2].':etc/'.$parts[0];
} elseif ($parts[2] == 'etc') {
$result = 'urn:magento:module:'.$parts[4].'_'.$parts[3].':etc/'.$parts[1].'/'.$parts[0];
}
}
if (strpos($path, 'Magento/Framework') !== false) {
if ($parts[1] == 'etc') {
$result = 'urn:magento:framework:'.$parts[2].'/etc/'.$parts[0];
} elseif ($parts[3] == 'Framework') {
$result = 'urn:magento:framework:'.$parts[2].'/'.$parts[1].'/'.$parts[0];
}
}
return $result;
}
function mapFullArray($paths)
{
$result = [];
foreach ($paths as $path) {
$transformed = transformXsdPathToMagentoUri($path);
if ($transformed !== null) {
$result[$path] = $transformed;
}
}
return $result;
}
function unitTest()
{
echo "start unitTest:\n";
$testArray = [
'/work/sandbox/magento2/lib/internal/Magento/Framework/Event/etc/events.xsd' => 'urn:magento:framework:Event/etc/events.xsd',
'/work/sandbox/magento2/app/code/Magento/Integration/etc/integration/config.xsd' => 'urn:magento:module:Magento_Integration:etc/integration/config.xsd',
'/work/sandbox/magento2/app/code/Magento/Store/etc/config.xsd' => 'urn:magento:module:Magento_Store:etc/config.xsd',
];
echo PHP_EOL;
foreach ($testArray as $input => $expected) {
$result = transformXsdPathToMagentoUri($input);
$messages = [];
if ($result === $expected) {
echo '.';
} else {
echo 'F';
$messages[] = $result . ' does not match ' . $expected;
}
}
echo PHP_EOL;
if (!empty($messages)) {
var_dump($messages);
}
echo "finish unitTest:\n";
}
function findUnresolvedPaths()
{
echo "show unresolved Paths:\n";
$paths = getAllSchemaFilePaths();
foreach ($paths as $path) {
$urn = transformXsdPathToMagentoUri($path);
if ($urn === null) {
var_dump($path);
}
}
echo 'paths with "/_files/" and also "argument/types.xsd" seem not to require translation?'."\n";
foreach (array(
'/design/adminhtml/Magento/backend/etc/view.xsd',
) as $file) {
echo "is '$file' used somewhere via urn?\n";
}
echo "that were all unresolved Paths:\n";
}
/**
* @param $map
*
* in phpstorm 8.0 inside .idea/misc.xml
* the element <project version="4"> contains an <component name="ProjectResources"> element
* with all the mappings for local xsd files
*
* @return string|void
*/
function transformMapToPhpstormConfigString($map)
{
$cwd = getcwd();
if (!file_exists($cwd.'/.idea')) {
echo "thats not an phpstorm project directory\n";
return;
}
$xml = '';
foreach ($map as $path => $urn) {
$xml .= '<resource url="'.$urn.'" location="'.str_replace($cwd, '$PROJECT_DIR$', $path).'" />'.PHP_EOL;
}
$xml = '<component name="ProjectResources">'.PHP_EOL.$xml.PHP_EOL.'</component>';
return $xml;
}
var_dump(transformMapToPhpstormConfigString(mapFullArray(getAllSchemaFilePaths())));
//unitTest();
//findUnresolvedPaths();
echo "finish generation script\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment