classmap_generator.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. * --overwrite|-w Whether or not to overwrite existing autoload
  31. * file
  32. */
  33. $libPath = dirname(__FILE__) . '/../library';
  34. if (!is_dir($libPath)) {
  35. // Try to load StandardAutoloader from include_path
  36. if (false === include('Zend/Loader/StandardAutoloader.php')) {
  37. echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL;
  38. exit(2);
  39. }
  40. } else {
  41. // Try to load StandardAutoloader from library
  42. if (false === include(dirname(__FILE__) . '/../library/Zend/Loader/StandardAutoloader.php')) {
  43. echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
  44. exit(2);
  45. }
  46. }
  47. // Setup autoloading
  48. $loader = new Zend_Loader_StandardAutoloader();
  49. $loader->setFallbackAutoloader(true);
  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 autoload file; if not provided, assumes "autoload_classmap.php" in library directory',
  55. 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
  56. );
  57. try {
  58. $opts = new Zend_Console_Getopt($rules);
  59. $opts->parse();
  60. } catch (Zend_Console_Getopt_Exception $e) {
  61. echo $e->getUsageMessage();
  62. exit(2);
  63. }
  64. if ($opts->getOption('h')) {
  65. echo $opts->getUsageMessage();
  66. exit();
  67. }
  68. $path = $libPath;
  69. if (array_key_exists('PWD', $_SERVER)) {
  70. $path = $_SERVER['PWD'];
  71. }
  72. if (isset($opts->l)) {
  73. $path = $opts->l;
  74. if (!is_dir($path)) {
  75. echo "Invalid library directory provided" . PHP_EOL . PHP_EOL;
  76. echo $opts->getUsageMessage();
  77. exit(2);
  78. }
  79. $path = realpath($path);
  80. }
  81. $usingStdout = false;
  82. $output = $path . DIRECTORY_SEPARATOR . 'autoload_classmap.php';
  83. if (isset($opts->o)) {
  84. $output = $opts->o;
  85. if ('-' == $output) {
  86. $output = STDOUT;
  87. $usingStdout = true;
  88. } elseif (!is_writeable(dirname($output))) {
  89. echo "Cannot write to '$output'; aborting." . PHP_EOL
  90. . PHP_EOL
  91. . $opts->getUsageMessage();
  92. exit(2);
  93. } elseif (file_exists($output)) {
  94. if (!$opts->getOption('w')) {
  95. echo "Autoload file already exists at '$output'," . PHP_EOL
  96. . "but 'overwrite' flag was not specified; aborting." . PHP_EOL
  97. . PHP_EOL
  98. . $opts->getUsageMessage();
  99. exit(2);
  100. }
  101. }
  102. }
  103. $strip = $path;
  104. if (!$usingStdout) {
  105. echo "Creating class file map for library in '$path'..." . PHP_EOL;
  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
  110. // classname => filename, where the filename is relative to the library path
  111. $map = new stdClass;
  112. $strip .= DIRECTORY_SEPARATOR;
  113. function createMap(Iterator $i, $map, $strip) {
  114. $file = $i->current();
  115. $namespace = empty($file->namespace) ? '' : $file->namespace . '\\';
  116. $filename = str_replace($strip, '', $file->getRealpath());
  117. // Windows portability
  118. $filename = str_replace(array('/', '\\'), "' . DIRECTORY_SEPARATOR . '", $filename);
  119. $map->{$namespace . $file->classname} = $filename;
  120. return true;
  121. }
  122. iterator_apply($l, 'createMap', array($l, $map, $strip));
  123. // Create a file with the class/file map.
  124. // Stupid syntax highlighters make separating < from PHP declaration necessary
  125. $dirStore = 'dirname_' . uniqid();
  126. $content = '<' . "?php\n"
  127. . '$' . $dirStore . " = dirname(__FILE__);\n"
  128. . 'return ' . var_export((array) $map, true) . ';';
  129. // Prefix with dirname(__FILE__); modify the generated content
  130. $content = preg_replace('#(=> )#', '$1$' . $dirStore . ' . DIRECTORY_SEPARATOR . ', $content);
  131. $content = str_replace("\\'", "'", $content);
  132. // Write the contents to disk
  133. file_put_contents($output, $content);
  134. if (!$usingStdout) {
  135. echo "Wrote classmap file to '" . realpath($output) . "'" . PHP_EOL;
  136. }