PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/pluginmap_generator.php

https://bitbucket.org/gencer/zf2
PHP | 179 lines | 132 code | 14 blank | 33 comment | 8 complexity | 48197d1a5d7cebbcd705950b7ff6f4a6 MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Zend Framework (http://framework.zend.com/)
  5. *
  6. * @link http://github.com/zendframework/zf2 for the canonical source repository
  7. * @category Zend
  8. * @package Zend_Loader
  9. * @subpackage Exception
  10. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  11. * @license http://framework.zend.com/license/new-bsd New BSD License
  12. */
  13. use Zend\Console;
  14. use Zend\Loader\StandardAutoloader;
  15. /**
  16. * Generate class maps for use with autoloading.
  17. *
  18. * Usage:
  19. * --help|-h Get usage message
  20. * --library|-l [ <string> ] Library to parse; if none provided, assumes
  21. * current directory
  22. * --output|-o [ <string> ] Where to write autoload file; if not provided,
  23. * assumes "autoload_classmap.php" in library directory
  24. * --append|-a Append to autoload file if it exists
  25. * --overwrite|-w Whether or not to overwrite existing autoload
  26. * file
  27. */
  28. $libPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
  29. if (!is_dir($libPath)) {
  30. // Try to load StandardAutoloader from include_path
  31. if (false === (include 'Zend/Loader/StandardAutoloader.php')) {
  32. echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL;
  33. exit(2);
  34. }
  35. } else {
  36. // Try to load StandardAutoloader from library
  37. if (false === (include $libPath . '/Zend/Loader/StandardAutoloader.php')) {
  38. echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
  39. exit(2);
  40. }
  41. }
  42. // Setup autoloading
  43. $loader = new StandardAutoloader(array('autoregister_zf' => true));
  44. $loader->register();
  45. $rules = array(
  46. 'help|h' => 'Get usage message',
  47. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  48. 'output|o-s' => 'Where to write plugin map file; if not provided, assumes "plugin_classmap.php" in library directory',
  49. 'append|a' => 'Append to plugin map file if it exists',
  50. 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
  51. );
  52. try {
  53. $opts = new Console\Getopt($rules);
  54. $opts->parse();
  55. } catch (Console\Exception\RuntimeException $e) {
  56. echo $e->getUsageMessage();
  57. exit(2);
  58. }
  59. if ($opts->getOption('h')) {
  60. echo $opts->getUsageMessage();
  61. exit();
  62. }
  63. $path = $libPath;
  64. if (array_key_exists('PWD', $_SERVER)) {
  65. $path = $_SERVER['PWD'];
  66. }
  67. if (isset($opts->l)) {
  68. $libraryPath = $opts->l;
  69. $libraryPath = rtrim($libraryPath, '/\\') . DIRECTORY_SEPARATOR;
  70. if (!is_dir($libraryPath)) {
  71. echo "Invalid library directory provided" . PHP_EOL . PHP_EOL;
  72. echo $opts->getUsageMessage();
  73. exit(2);
  74. }
  75. $path = realpath($libraryPath);
  76. }
  77. $usingStdout = false;
  78. $appending = $opts->getOption('a');
  79. $output = $path . DIRECTORY_SEPARATOR . 'plugin_classmap.php';
  80. if (isset($opts->o)) {
  81. $output = $opts->o;
  82. if ('-' == $output) {
  83. $output = STDOUT;
  84. $usingStdout = true;
  85. } elseif (!is_writeable(dirname($output))) {
  86. echo "Cannot write to '$output'; aborting." . PHP_EOL
  87. . PHP_EOL
  88. . $opts->getUsageMessage();
  89. exit(2);
  90. } elseif (file_exists($output)) {
  91. if (!$opts->getOption('w') && !$appending) {
  92. echo "Plugin map file already exists at '$output'," . PHP_EOL
  93. . "but 'overwrite' flag was not specified; aborting." . PHP_EOL
  94. . PHP_EOL
  95. . $opts->getUsageMessage();
  96. exit(2);
  97. }
  98. }
  99. }
  100. if (!$usingStdout) {
  101. if ($appending) {
  102. echo "Appending to plugin class map '$output' for classes in '$path'..." . PHP_EOL;
  103. } else {
  104. echo "Creating plugin class map for classes in '$path'..." . PHP_EOL;
  105. }
  106. }
  107. // Get the ClassFileLocator, and pass it the library path
  108. $l = new \Zend\File\ClassFileLocator($path);
  109. // Iterate over each element in the path, and create a map of pluginname => classname
  110. $map = new \stdClass;
  111. foreach ($l as $file) {
  112. $namespaces = $file->getNamespaces();
  113. $namespace = empty($file->namespace) ? '' : $file->namespace . '\\';
  114. foreach ($file->getClasses() as $classname) {
  115. $plugin = $classname;
  116. foreach ($namespaces as $namespace) {
  117. $namespace .= '\\';
  118. if (0 === strpos($plugin, $namespace)) {
  119. $plugin = str_replace($namespace, '', $plugin);
  120. }
  121. }
  122. $plugin = strtolower($plugin);
  123. $map->{$plugin} = $classname;
  124. }
  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("\n", $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("\n", $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. // Make the file end by EOL
  150. $content = rtrim($content, "\n") . "\n";
  151. // Write the contents to disk
  152. file_put_contents($output, $content);
  153. if (!$usingStdout) {
  154. echo "Wrote plugin classmap file to '" . realpath($output) . "'" . PHP_EOL;
  155. }