PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tools/migration/factory_table_names.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 185 lines | 110 code | 19 blank | 56 comment | 21 complexity | e527954e3810d3dfe86269b0d611f36a MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage
  23. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. define('USAGE', <<<USAGE
  27. $>./factory_table_names.php -- [-dseh]
  28. additional parameters:
  29. -d replacement in dry-run mode
  30. -s search for table names not in list for replacement
  31. -e output with errors during replacement
  32. -h print usage
  33. USAGE
  34. );
  35. $shortOpts = 'ehds';
  36. $options = getopt($shortOpts);
  37. if (isset($options['h'])) {
  38. print USAGE;
  39. exit(0);
  40. }
  41. $outputWithErrors = isset($options['e']);
  42. $isDryRunMode = isset($options['d']);
  43. $isSearchTables = isset($options['s']);
  44. require realpath(dirname(dirname(dirname(__DIR__)))) . '/dev/tests/static/framework/bootstrap.php';
  45. $tablesAssociation = getFilesCombinedArray(dirname(__FILE__) . '/factory_table_names', 'replace_*.php');
  46. $blackList = getFilesCombinedArray(dirname(__FILE__) . '/factory_table_names', 'blacklist_*.php');
  47. $phpFiles = Utility_Files::init()->getPhpFiles(true, false, false, false);
  48. $replacementResult = false;
  49. if (!$isSearchTables || $isDryRunMode) {
  50. $replacementResult = replaceTableNames($phpFiles, $tablesAssociation, $outputWithErrors, $isDryRunMode);
  51. }
  52. $searchResult = $isSearchTables ? searchTableNamesNotInReplacedList($phpFiles, $tablesAssociation, $blackList) : false;
  53. if ($replacementResult || $searchResult) {
  54. exit(1);
  55. }
  56. exit(0);
  57. /**
  58. * Get combined array from similar files by pattern
  59. *
  60. * @param $dirPath
  61. * @param $filePattern
  62. * @return array
  63. */
  64. function getFilesCombinedArray($dirPath, $filePattern)
  65. {
  66. $result = array();
  67. foreach (glob($dirPath . '/' . $filePattern, GLOB_NOSORT | GLOB_BRACE) as $filePath) {
  68. $arrayFromFile = include_once($filePath);
  69. $result = array_merge($result, $arrayFromFile);
  70. }
  71. return $result;
  72. }
  73. /**
  74. * Replace table names in all files
  75. *
  76. * @param array $files
  77. * @param array $tablesAssociation
  78. * @param $outputWithErrors
  79. * @param $isDryRunMode
  80. * @return bool
  81. */
  82. function replaceTableNames(array $files, array &$tablesAssociation, $outputWithErrors, $isDryRunMode)
  83. {
  84. $isErrorsFound = false;
  85. $errors = array();
  86. foreach ($files as $filePath) {
  87. $search = $replace = array();
  88. $tables = Legacy_TableTest::extractTables($filePath);
  89. $tables = array_filter(
  90. $tables,
  91. function ($table) {
  92. return false !== strpos($table['name'], '/');
  93. }
  94. );
  95. if (!empty($tables)) {
  96. foreach ($tables as $table) {
  97. $tableName = $table['name'];
  98. if (isset($tablesAssociation[$tableName])) {
  99. $search[] = $tableName;
  100. $replace[] = $tablesAssociation[$tableName];
  101. } else {
  102. $errors[] = $tableName;
  103. }
  104. }
  105. if (!empty($replace) && !empty($search)) {
  106. replaceTableNamesInFile($filePath, $search, $replace, $isDryRunMode);
  107. }
  108. if (!empty($errors)) {
  109. if ($outputWithErrors) {
  110. echo "Error - Missed table names in config: \n" . implode(", ", $errors) . "\n";
  111. }
  112. $errors = array();
  113. $isErrorsFound = true;
  114. }
  115. }
  116. }
  117. return $isErrorsFound;
  118. }
  119. /**
  120. * Replace table names in an file
  121. *
  122. * @param $filePath
  123. * @param $search
  124. * @param $replace
  125. * @param $isDryRunMode
  126. */
  127. function replaceTableNamesInFile($filePath, $search, $replace, $isDryRunMode)
  128. {
  129. $content = file_get_contents($filePath);
  130. $newContent = str_replace($search, $replace, $content);
  131. if ($newContent != $content) {
  132. echo "{$filePath}\n";
  133. echo 'Replaced tables: '; print_r($search);
  134. if (!$isDryRunMode) {
  135. file_put_contents($filePath, $newContent);
  136. }
  137. }
  138. }
  139. /**
  140. * Looking for table names which not defined in current config
  141. *
  142. * @param array $files
  143. * @param array $tablesAssociation
  144. * @param array $blackList
  145. * @return bool
  146. */
  147. function searchTableNamesNotInReplacedList(array $files, array &$tablesAssociation, array &$blackList)
  148. {
  149. $search = array();
  150. foreach ($files as $filePath) {
  151. $tables = Legacy_TableTest::extractTables($filePath);
  152. foreach ($tables as $table) {
  153. if (in_array($table['name'], $blackList)) {
  154. continue;
  155. }
  156. if (!in_array($table['name'], array_values($tablesAssociation)) && !in_array($table['name'], $search)) {
  157. $search[] = $table['name'];
  158. }
  159. }
  160. }
  161. if (!empty($search)) {
  162. echo "List of table names not in association list: \n";
  163. print_r(array_unique($search));
  164. }
  165. return false;
  166. }