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

/dev/tools/license_placeholder/replace.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 100 lines | 58 code | 5 blank | 37 comment | 9 complexity | 64e8fa219b6da112097fb36beb6fdf4a MD5 | raw file
  1. <?php
  2. /**
  3. * Automated replacement of license notice into placeholders
  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. $sourceDir = realpath(__DIR__ . '/../../..');
  29. // scan for files (split up into several calls to overcome maximum limitation of 260 chars in glob pattern)
  30. $files = globDir($sourceDir, '*.{xml,xml.template,xml.additional,xml.dist,xml.sample,xsd,mxml}', GLOB_BRACE);
  31. $files = array_merge($files, globDir($sourceDir, '*.{php,php.sample,phtml,html,htm,css,js,as,sql}', GLOB_BRACE));
  32. // exclude files from blacklist
  33. $blacklist = require __DIR__ . '/blacklist.php';
  34. foreach ($blacklist as $item) {
  35. $excludeDirs = glob("{$sourceDir}/{$item}", GLOB_ONLYDIR) ?: array();
  36. foreach ($excludeDirs as $excludeDir) {
  37. foreach ($files as $i => $file) {
  38. if (0 === strpos($file, $excludeDir)) {
  39. unset($files[$i]);
  40. }
  41. }
  42. }
  43. if (!$excludeDirs) {
  44. $excludeFiles = glob("{$sourceDir}/{$item}", GLOB_BRACE) ?: array();
  45. foreach ($excludeFiles as $excludeFile) {
  46. $i = array_search($excludeFile, $files);
  47. if (false !== $i) {
  48. unset($files[$i]);
  49. }
  50. }
  51. }
  52. }
  53. // replace
  54. $licensePlaceholder = ' * {license}' . "\n";
  55. $replacements = array(
  56. array('/\s\*\sMagento.+?NOTICE OF LICENSE.+?DISCLAIMER.+?@/s', $licensePlaceholder . " *\n * @"),
  57. array('/\ \*\ \{license_notice\}\s/s', $licensePlaceholder),
  58. );
  59. foreach ($files as $file) {
  60. $content = file_get_contents($file);
  61. $newContent = $content;
  62. foreach ($replacements as $row) {
  63. list($regex, $replacement) = $row;
  64. $newContent = preg_replace($regex, $replacement, $content);
  65. if ($newContent != $content) {
  66. break;
  67. }
  68. }
  69. $newContent = preg_replace('/^\s\*\s@copyright.+?$/m', '', $newContent);
  70. $newContent = preg_replace('/^\s\*\s@license.+$/m', '', $newContent);
  71. $newContent = preg_replace('/(\{license\}.+?)\n\n\ \*/s', '\\1' . " *", $newContent);
  72. if ($newContent != $content) {
  73. file_put_contents($file, $newContent);
  74. }
  75. }
  76. /**
  77. * Perform a glob search in specified directory
  78. *
  79. * @param string $dir
  80. * @param string $filesPattern
  81. * @param int $flags
  82. * @return array
  83. */
  84. function globDir($dir, $filesPattern, $flags)
  85. {
  86. if (!$dir || !is_dir($dir)) {
  87. return array();
  88. }
  89. $result = glob($dir . '/' . $filesPattern, $flags) ?: array();
  90. $dirs = glob($dir . '/*', GLOB_ONLYDIR) ?: array();
  91. foreach ($dirs as $innerDir) {
  92. $result = array_merge($result, globDir($innerDir, $filesPattern, $flags));
  93. }
  94. return $result;
  95. }