PageRenderTime 56ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/classmap_generator.php

https://github.com/lazenge/zf2
PHP | 147 lines | 95 code | 14 blank | 38 comment | 15 complexity | d59906aca7f5b9ddbbd836d85df84660 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-2011 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 ".classmap.php" in library directory
  30. * --overwrite|-w Whether or not to overwrite existing autoload
  31. * file
  32. */
  33. $libPath = __DIR__ . '/../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(__DIR__ . '/../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->register();
  50. $rules = array(
  51. 'help|h' => 'Get usage message',
  52. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  53. 'output|o-s' => 'Where to write autoload file; if not provided, assumes ".classmap.php" in library directory',
  54. 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
  55. );
  56. try {
  57. $opts = new Zend\Console\Getopt($rules);
  58. $opts->parse();
  59. } catch (Zend\Console\Getopt\Exception $e) {
  60. echo $e->getUsageMessage();
  61. exit(2);
  62. }
  63. if ($opts->getOption('h')) {
  64. echo $opts->getUsageMessage();
  65. exit();
  66. }
  67. $path = $libPath;
  68. if (array_key_exists('PWD', $_SERVER)) {
  69. $path = $_SERVER['PWD'];
  70. }
  71. if (isset($opts->l)) {
  72. $path = $opts->l;
  73. if (!is_dir($path)) {
  74. echo "Invalid library directory provided" . PHP_EOL . PHP_EOL;
  75. echo $opts->getUsageMessage();
  76. exit(2);
  77. }
  78. $path = realpath($path);
  79. }
  80. $usingStdout = false;
  81. $output = $path . DIRECTORY_SEPARATOR . '.classmap.php';
  82. if (isset($opts->o)) {
  83. $output = $opts->o;
  84. if ('-' == $output) {
  85. $output = STDOUT;
  86. $usingStdout = true;
  87. } elseif (!is_writeable(dirname($output))) {
  88. echo "Cannot write to '$output'; aborting." . PHP_EOL
  89. . PHP_EOL
  90. . $opts->getUsageMessage();
  91. exit(2);
  92. } elseif (file_exists($output)) {
  93. if (!$opts->getOption('w')) {
  94. echo "Autoload file already exists at '$output'," . PHP_EOL
  95. . "but 'overwrite' flag was not specified; aborting." . PHP_EOL
  96. . PHP_EOL
  97. . $opts->getUsageMessage();
  98. exit(2);
  99. }
  100. }
  101. }
  102. $strip = $path;
  103. if (!$usingStdout) {
  104. echo "Creating class file map for library in '$path'..." . PHP_EOL;
  105. }
  106. // Get the ClassFileLocator, and pass it the library path
  107. $l = new \Zend\File\ClassFileLocator($path);
  108. // Iterate over each element in the path, and create a map of
  109. // classname => filename, where the filename is relative to the library path
  110. $map = new \stdClass;
  111. $strip .= DIRECTORY_SEPARATOR;
  112. iterator_apply($l, function() use ($l, $map, $strip){
  113. $file = $l->current();
  114. $namespace = empty($file->namespace) ? '' : $file->namespace . '\\';
  115. $filename = str_replace($strip, '', $file->getRealpath());
  116. $map->{$namespace . $file->classname} = $filename;
  117. return true;
  118. });
  119. // Create a file with the class/file map.
  120. // Stupid syntax highlighters make separating < from PHP declaration necessary
  121. $content = '<' . "?php\n"
  122. . 'return ' . var_export((array) $map, true) . ';';
  123. // Prefix with __DIR__; modify the generated content
  124. $content = preg_replace('#(=> )#', '$1__DIR__ . DIRECTORY_SEPARATOR . ', $content);
  125. // Write the contents to disk
  126. file_put_contents($output, $content);
  127. if (!$usingStdout) {
  128. echo "Wrote classmap file to '" . realpath($output) . "'" . PHP_EOL;
  129. }