PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/pluginmap_generator.php

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