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

/vendor/zendframework/zendframework/bin/pluginmap_generator.php

https://bitbucket.org/raulgracia/zendframework2
PHP | 176 lines | 123 code | 15 blank | 38 comment | 9 complexity | ee1e59d2c7ca996928087f700af69d76 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Zend Framework
  5. *
  6. * LICENSE
  7. *
  8. * This source file is subject to the new BSD license that is bundled
  9. * with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://framework.zend.com/license/new-bsd
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to license@zend.com so we can send you a copy immediately.
  15. *
  16. * @category Zend
  17. * @package Zend_Loader
  18. * @subpackage Exception
  19. * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
  20. * @license http://framework.zend.com/license/new-bsd New BSD License
  21. */
  22. use Zend\Console;
  23. use Zend\Loader\StandardAutoloader;
  24. /**
  25. * Generate class maps for use with autoloading.
  26. *
  27. * Usage:
  28. * --help|-h Get usage message
  29. * --library|-l [ <string> ] Library to parse; if none provided, assumes
  30. * current directory
  31. * --output|-o [ <string> ] Where to write autoload file; if not provided,
  32. * assumes "autoload_classmap.php" in library directory
  33. * --append|-a Append to autoload file if it exists
  34. * --overwrite|-w Whether or not to overwrite existing autoload
  35. * file
  36. */
  37. $libPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
  38. if (!is_dir($libPath)) {
  39. // Try to load StandardAutoloader from include_path
  40. if (false === (include 'Zend/Loader/StandardAutoloader.php')) {
  41. echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL;
  42. exit(2);
  43. }
  44. } else {
  45. // Try to load StandardAutoloader from library
  46. if (false === (include $libPath . '/Zend/Loader/StandardAutoloader.php')) {
  47. echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
  48. exit(2);
  49. }
  50. }
  51. // Setup autoloading
  52. $loader = new StandardAutoloader(array('autoregister_zf' => true));
  53. $loader->register();
  54. $rules = array(
  55. 'help|h' => 'Get usage message',
  56. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  57. 'output|o-s' => 'Where to write plugin map file; if not provided, assumes "plugin_classmap.php" in library directory',
  58. 'append|a' => 'Append to plugin map file if it exists',
  59. 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
  60. );
  61. try {
  62. $opts = new Console\Getopt($rules);
  63. $opts->parse();
  64. } catch (Console\Exception\RuntimeException $e) {
  65. echo $e->getUsageMessage();
  66. exit(2);
  67. }
  68. if ($opts->getOption('h')) {
  69. echo $opts->getUsageMessage();
  70. exit();
  71. }
  72. $path = $libPath;
  73. if (array_key_exists('PWD', $_SERVER)) {
  74. $path = $_SERVER['PWD'];
  75. }
  76. if (isset($opts->l)) {
  77. $libraryPath = $opts->l;
  78. $libraryPath = rtrim($libraryPath, '/\\') . DIRECTORY_SEPARATOR;
  79. if (!is_dir($libraryPath)) {
  80. echo "Invalid library directory provided" . PHP_EOL . PHP_EOL;
  81. echo $opts->getUsageMessage();
  82. exit(2);
  83. }
  84. $path = realpath($libraryPath);
  85. }
  86. $usingStdout = false;
  87. $appending = $opts->getOption('a');
  88. $output = $path . DIRECTORY_SEPARATOR . 'plugin_classmap.php';
  89. if (isset($opts->o)) {
  90. $output = $opts->o;
  91. if ('-' == $output) {
  92. $output = STDOUT;
  93. $usingStdout = true;
  94. } elseif (!is_writeable(dirname($output))) {
  95. echo "Cannot write to '$output'; aborting." . PHP_EOL
  96. . PHP_EOL
  97. . $opts->getUsageMessage();
  98. exit(2);
  99. } elseif (file_exists($output)) {
  100. if (!$opts->getOption('w') && !$appending) {
  101. echo "Plugin map file already exists at '$output'," . PHP_EOL
  102. . "but 'overwrite' flag was not specified; aborting." . PHP_EOL
  103. . PHP_EOL
  104. . $opts->getUsageMessage();
  105. exit(2);
  106. }
  107. }
  108. }
  109. if (!$usingStdout) {
  110. if ($appending) {
  111. echo "Appending to plugin class map '$output' for classes in '$path'..." . PHP_EOL;
  112. } else {
  113. echo "Creating plugin class map for classes in '$path'..." . PHP_EOL;
  114. }
  115. }
  116. // Get the ClassFileLocator, and pass it the library path
  117. $l = new \Zend\File\ClassFileLocator($path);
  118. // Iterate over each element in the path, and create a map of pluginname => classname
  119. $map = new \stdClass;
  120. foreach ($l as $file) {
  121. $namespace = empty($file->namespace) ? '' : $file->namespace . '\\';
  122. $plugin = strtolower($file->classname);
  123. $class = $namespace . $file->classname;
  124. $map->{$plugin} = $class;
  125. }
  126. if ($appending) {
  127. $content = var_export((array) $map, true) . ';';
  128. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  129. $content = str_replace("\\'", "'", $content);
  130. // Convert to an array and remove the first "array ("
  131. $content = explode(PHP_EOL, $content);
  132. array_shift($content);
  133. // Load existing class map file and remove the closing "bracket ");" from it
  134. $existing = file($output, FILE_IGNORE_NEW_LINES);
  135. array_pop($existing);
  136. // Merge
  137. $content = implode(PHP_EOL, $existing + $content);
  138. } else {
  139. // Create a file with the class/file map.
  140. // Stupid syntax highlighters make separating < from PHP declaration necessary
  141. $content = '<' . "?php\n\n"
  142. . "// plugin class map\n"
  143. . "// auto-generated using "
  144. . basename($_SERVER['argv'][0]) . ', ' . date('Y-m-d H:i:s') . "\n\n"
  145. . 'return ' . var_export((array) $map, true) . ';';
  146. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  147. $content = str_replace("\\'", "'", $content);
  148. }
  149. // Write the contents to disk
  150. file_put_contents($output, $content);
  151. if (!$usingStdout) {
  152. echo "Wrote plugin classmap file to '" . realpath($output) . "'" . PHP_EOL;
  153. }