Drupal investigation

build-phar.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Build a PHPCS phar.
  5. *
  6. * PHP version 5
  7. *
  8. * @category PHP
  9. * @package PHP_CodeSniffer
  10. * @author Benjamin Pearson <bpearson@squiz.com.au>
  11. * @author Greg Sherwood <gsherwood@squiz.net>
  12. * @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
  13. * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
  14. * @link http://pear.php.net/package/PHP_CodeSniffer
  15. */
  16. error_reporting(E_ALL | E_STRICT);
  17. if (ini_get('phar.readonly') === '1') {
  18. echo 'Unable to build, phar.readonly in php.ini is set to read only.'.PHP_EOL;
  19. exit(1);
  20. }
  21. $cwd = getCwd();
  22. require_once __DIR__.'/../CodeSniffer.php';
  23. $scripts = array(
  24. 'phpcs',
  25. 'phpcbf',
  26. );
  27. foreach ($scripts as $script) {
  28. echo "Building $script phar".PHP_EOL;
  29. $pharFile = $cwd.'/'.$script.'.phar';
  30. echo "\t=> $pharFile".PHP_EOL;
  31. if (file_exists($pharFile) === true) {
  32. echo "\t** file exists, removing **".PHP_EOL;
  33. unlink($pharFile);
  34. }
  35. $phar = new Phar($pharFile, 0, $script.'.phar');
  36. echo "\t=> adding files from package.xml... ";
  37. buildFromPackage($phar);
  38. echo 'done'.PHP_EOL;
  39. echo "\t=> adding stub... ";
  40. $stub = '#!/usr/bin/env php'."\n";
  41. $stub .= '<?php'."\n";
  42. $stub .= 'Phar::mapPhar(\''.$script.'.phar\');'."\n";
  43. $stub .= 'require_once "phar://'.$script.'.phar/CodeSniffer/CLI.php";'."\n";
  44. $stub .= '$cli = new PHP_CodeSniffer_CLI();'."\n";
  45. $stub .= '$cli->run'.$script.'();'."\n";
  46. $stub .= '__HALT_COMPILER();';
  47. $phar->setStub($stub);
  48. echo 'done'.PHP_EOL;
  49. }//end foreach
  50. /**
  51. * Build from a package list.
  52. *
  53. * @param object $phar The Phar class.
  54. *
  55. * @return void
  56. */
  57. function buildFromPackage(&$phar)
  58. {
  59. $packageFile = realpath(__DIR__.'/../package.xml');
  60. $dom = new DOMDocument('1.0', 'utf-8');
  61. $loaded = $dom->loadXML(file_get_contents($packageFile));
  62. if ($loaded === false) {
  63. echo "Unable to load package file: $packageFile".PHP_EOL;
  64. exit(1);
  65. }
  66. $contents = $dom->getElementsByTagName('contents');
  67. $topLevels = $contents->item(0)->childNodes;
  68. $tlLength = $topLevels->length;
  69. for ($l = 0; $l < $tlLength; $l++) {
  70. $currentLevel = $topLevels->item($l);
  71. buildFromNode($phar, $currentLevel, '');
  72. }
  73. // Add licence file.
  74. $phar->addFile(realpath(__DIR__.'/../licence.txt'), 'licence.txt');
  75. $phar['licence.txt']->compress(Phar::GZ);
  76. }//end buildFromPackage()
  77. /**
  78. * Add from a node.
  79. *
  80. * @param object $phar The Phar class.
  81. * @param object $node The node to add.
  82. * @param string $prefix The prefix of the structure.
  83. *
  84. * @return void
  85. */
  86. function buildFromNode(&$phar, $node, $prefix='')
  87. {
  88. $nodeName = $node->nodeName;
  89. if ($nodeName !== 'dir' && $nodeName !== 'file') {
  90. // Invalid node.
  91. return;
  92. }
  93. $path = $prefix.$node->getAttribute('name');
  94. if ($node->getAttribute('role') === 'php' || $node->getAttribute('role') === 'data') {
  95. $path = ltrim($path, '/');
  96. $phar->addFile(realpath(__DIR__.'/../'.$path), $path);
  97. $phar[$path]->compress(Phar::GZ);
  98. }
  99. if ($nodeName === 'dir') {
  100. // Descend into the depths.
  101. $path = rtrim($path, '/').'/';
  102. $children = $node->childNodes;
  103. $childLn = $children->length;
  104. for ($c = 0; $c < $childLn; $c++) {
  105. $child = $children->item($c);
  106. buildFromNode($phar, $child, $path);
  107. }
  108. }
  109. }//end buildFromNode()