PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/dev/tools/migration/get_aliases_map.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 192 lines | 111 code | 18 blank | 63 comment | 12 complexity | f881e858c9e43a88ae9cc67802f6d634 MD5 | raw file
  1. <?php
  2. /**
  3. * Automated replacement of factory names into real ones and put result information into file
  4. *
  5. * Magento
  6. *
  7. * NOTICE OF LICENSE
  8. *
  9. * This source file is subject to the Open Software License (OSL 3.0)
  10. * that is bundled with this package in the file LICENSE.txt.
  11. * It is also available through the world-wide-web at this URL:
  12. * http://opensource.org/licenses/osl-3.0.php
  13. * If you did not receive a copy of the license and are unable to
  14. * obtain it through the world-wide-web, please send an email
  15. * to license@magentocommerce.com so we can send you a copy immediately.
  16. *
  17. * DISCLAIMER
  18. *
  19. * Do not edit or add to this file if you wish to upgrade Magento to newer
  20. * versions in the future. If you wish to customize Magento for your
  21. * needs please refer to http://www.magentocommerce.com for more information.
  22. *
  23. * @category Magento
  24. * @package tools
  25. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  26. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  27. */
  28. define('USAGE', <<<USAGE
  29. $>./get_aliases_map.php -- [-ph]
  30. Build Magento 1 Aliases To Magento 2 Classes Names.
  31. Additional parameters:
  32. -p path to code scope of magento instance
  33. -h print usage
  34. USAGE
  35. );
  36. $options = getopt('p:h');
  37. if (isset($options['h'])) {
  38. print USAGE;
  39. exit(0);
  40. }
  41. require_once realpath(dirname(dirname(dirname(__DIR__)))) . '/dev/tests/static/framework/bootstrap.php';
  42. require_once realpath(dirname(dirname(dirname(__DIR__)))) . '/lib/Zend/Json.php';
  43. $magentoBaseDir = dirname(__DIR__) . '/../../';
  44. if (isset($options['p'])) {
  45. $magentoBaseDir = $options['p'];
  46. }
  47. $utilityFiles = new Utility_Files($magentoBaseDir);
  48. $map = array();
  49. $compositeModules = getFilesCombinedArray(dirname(__FILE__) . '/aliases_map', '/^composite_modules_.*\.php$/');
  50. // PHP code
  51. foreach ($utilityFiles->getPhpFiles(true, true, true, false) as $file) {
  52. $content = file_get_contents($file);
  53. $classes = Legacy_ClassesTest::collectPhpCodeClasses($content);
  54. if ($classes) {
  55. $factoryNames = array_filter($classes, 'isFactoryName');
  56. foreach ($factoryNames as $factoryName) {
  57. list($module, $name) = getModuleName($factoryName, $compositeModules);
  58. $patterns = array(
  59. '::getModel(\'%s\'' => 'Model',
  60. '::getSingleton(\'%s\'' => 'Model',
  61. '::getResourceModel(\'%s\'' => 'Model_Resource',
  62. '::getResourceSingleton(\'%s\'' => 'Model_Resource',
  63. 'addBlock(\'%s\'' => 'Block',
  64. 'createBlock(\'%s\'' => 'Block',
  65. 'getBlockClassName(\'%s\'' => 'Block',
  66. 'getBlockSingleton(\'%s\'' => 'Block'
  67. );
  68. foreach ($patterns as $pattern => $classType) {
  69. if (isPatternExist($content, $pattern, $factoryName)) {
  70. if (!isset($map[$classType])) {
  71. $map[$classType] = array();
  72. }
  73. $map[$classType][$factoryName] = getClassName($module, $classType, $name);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. // layouts
  80. $classType = 'Block';
  81. $layouts = $utilityFiles->getLayoutFiles(array(), false);
  82. foreach ($layouts as $file) {
  83. $xml = simplexml_load_file($file);
  84. $classes = Utility_Classes::collectLayoutClasses($xml);
  85. $factoryNames = array_filter($classes, 'isFactoryName');
  86. if (!$factoryNames) {
  87. continue;
  88. }
  89. foreach ($factoryNames as $factoryName) {
  90. list($module, $name) = getModuleName($factoryName, $compositeModules);
  91. $map[$classType][$factoryName] = getClassName($module, $classType, $name);
  92. }
  93. }
  94. echo Zend_Json::prettyPrint(Zend_Json::encode($map));
  95. /**
  96. * Get combined array from similar files by pattern
  97. *
  98. * @param string $dirPath
  99. * @param string $filePattern
  100. * @return array
  101. */
  102. function getFilesCombinedArray($dirPath, $filePattern)
  103. {
  104. $result = array();
  105. $directoryIterator = new DirectoryIterator($dirPath);
  106. $patternIterator = new RegexIterator($directoryIterator, $filePattern);
  107. foreach ($patternIterator as $fileInfo) {
  108. $arrayFromFile = include_once($fileInfo->getPathname());
  109. $result = array_merge($result, $arrayFromFile);
  110. }
  111. return $result;
  112. }
  113. /**
  114. * Check if pattern exist in file content
  115. *
  116. * @param string $content
  117. * @param string $pattern
  118. * @param string $alias
  119. * @return bool
  120. */
  121. function isPatternExist($content, $pattern, $alias)
  122. {
  123. $search = sprintf($pattern, $alias);
  124. return strpos($content, $search) !== false;
  125. }
  126. /**
  127. * Build class name supported in magento 2
  128. *
  129. * @param string $module
  130. * @param string $type
  131. * @param string $name
  132. * @return string|bool
  133. */
  134. function getClassName($module, $type, $name = null)
  135. {
  136. if (empty($name)) {
  137. if ('Helper' !== $type) {
  138. return false;
  139. }
  140. $name = 'data';
  141. }
  142. return implode('_', array_map('ucfirst', explode('_', $module . '_' . $type . '_' . $name)));
  143. }
  144. /**
  145. * Whether the given class name is a factory name
  146. *
  147. * @param string $class
  148. * @return bool
  149. */
  150. function isFactoryName($class)
  151. {
  152. return false !== strpos($class, '/') || preg_match('/^[a-z\d]+(_[A-Za-z\d]+)?$/', $class);
  153. }
  154. /**
  155. * Transform factory name into a pair of module and name
  156. *
  157. * @param string $factoryName
  158. * @return array
  159. */
  160. function getModuleName($factoryName, $compositeModules = array())
  161. {
  162. if (false !== strpos($factoryName, '/')) {
  163. list($module, $name) = explode('/', $factoryName);
  164. } else {
  165. $module = $factoryName;
  166. $name = false;
  167. }
  168. if (array_key_exists($module, $compositeModules)) {
  169. $module = $compositeModules[$module];
  170. } elseif (false === strpos($module, '_')) {
  171. $module = "Mage_{$module}";
  172. }
  173. return array($module, $name);
  174. }