PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/bin/classmap_generator.php

https://bitbucket.org/openemr/openemr
PHP | 248 lines | 181 code | 27 blank | 40 comment | 30 complexity | 12c2e8bda0a584d8b791f21ef3a309aa MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0, GPL-3.0, LGPL-2.1
  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. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  8. * @license http://framework.zend.com/license/new-bsd New BSD License
  9. */
  10. use Zend\Console;
  11. use Zend\File\ClassFileLocator;
  12. use Zend\Loader\StandardAutoloader;
  13. /**
  14. * Generate class maps for use with autoloading.
  15. *
  16. * Usage:
  17. * --help|-h Get usage message
  18. * --library|-l [ <string> ] Library to parse; if none provided, assumes
  19. * current directory
  20. * --output|-o [ <string> ] Where to write autoload file; if not provided,
  21. * assumes "autoload_classmap.php" in library directory
  22. * --append|-a Append to autoload file if it exists
  23. * --overwrite|-w Whether or not to overwrite existing autoload
  24. * file
  25. * --ignore|-i [ <string> ] Comma-separated namespaces to ignore
  26. * --sort|-s Alphabetically sort classes
  27. */
  28. $zfLibraryPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
  29. if (is_dir($zfLibraryPath)) {
  30. // Try to load StandardAutoloader from library
  31. if (false === include($zfLibraryPath . '/Zend/Loader/StandardAutoloader.php')) {
  32. echo 'Unable to locate autoloader via library; aborting' . PHP_EOL;
  33. exit(2);
  34. }
  35. } else {
  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. }
  42. $libraryPath = getcwd();
  43. // Setup autoloading
  44. $loader = new StandardAutoloader(array('autoregister_zf' => true));
  45. $loader->register();
  46. $rules = array(
  47. 'help|h' => 'Get usage message',
  48. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  49. 'output|o-s' => 'Where to write autoload file; if not provided, assumes "autoload_classmap.php" in library directory',
  50. 'append|a' => 'Append to autoload file if it exists',
  51. 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
  52. 'ignore|i-s' => 'Comma-separated namespaces to ignore',
  53. 'sort|s' => 'Alphabetically sort classes',
  54. );
  55. try {
  56. $opts = new Console\Getopt($rules);
  57. $opts->parse();
  58. } catch (Console\Exception\RuntimeException $e) {
  59. echo $e->getUsageMessage();
  60. exit(2);
  61. }
  62. if ($opts->getOption('h')) {
  63. echo $opts->getUsageMessage();
  64. exit(0);
  65. }
  66. $ignoreNamespaces = array();
  67. if (isset($opts->i)) {
  68. $ignoreNamespaces = explode(',', $opts->i);
  69. }
  70. $relativePathForClassmap = '';
  71. if (isset($opts->l)) {
  72. if (!is_dir($opts->l)) {
  73. echo 'Invalid library directory provided' . PHP_EOL
  74. . PHP_EOL;
  75. echo $opts->getUsageMessage();
  76. exit(2);
  77. }
  78. $libraryPath = $opts->l;
  79. }
  80. $libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath));
  81. $usingStdout = false;
  82. $appending = $opts->getOption('a');
  83. $output = $libraryPath . '/autoload_classmap.php';
  84. if (isset($opts->o)) {
  85. $output = $opts->o;
  86. if ('-' == $output) {
  87. $output = STDOUT;
  88. $usingStdout = true;
  89. } elseif (is_dir($output)) {
  90. echo 'Invalid output file provided' . PHP_EOL
  91. . PHP_EOL;
  92. echo $opts->getUsageMessage();
  93. exit(2);
  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) && !$opts->getOption('w') && !$appending) {
  100. echo "Autoload file already exists at '$output'," . PHP_EOL
  101. . "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL
  102. . PHP_EOL
  103. . $opts->getUsageMessage();
  104. exit(2);
  105. } else {
  106. // We need to add the $libraryPath into the relative path that is created in the classmap file.
  107. $classmapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output)));
  108. // Simple case: $libraryPathCompare is in $classmapPathCompare
  109. if (strpos($libraryPath, $classmapPath) === 0) {
  110. $relativePathForClassmap = substr($libraryPath, strlen($classmapPath) + 1) . '/';
  111. } else {
  112. $libraryPathParts = explode('/', $libraryPath);
  113. $classmapPathParts = explode('/', $classmapPath);
  114. // Find the common part
  115. $count = count($classmapPathParts);
  116. for ($i = 0; $i < $count; $i++) {
  117. if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $classmapPathParts[$i]) {
  118. // Common part end
  119. break;
  120. }
  121. }
  122. // Add parent dirs for the subdirs of classmap
  123. $relativePathForClassmap = str_repeat('../', $count - $i);
  124. // Add library subdirs
  125. $count = count($libraryPathParts);
  126. for (; $i < $count; $i++) {
  127. $relativePathForClassmap .= $libraryPathParts[$i] . '/';
  128. }
  129. }
  130. }
  131. }
  132. if (!$usingStdout) {
  133. if ($appending) {
  134. echo "Appending to class file map '$output' for library in '$libraryPath'..." . PHP_EOL;
  135. } else {
  136. echo "Creating class file map for library in '$libraryPath'..." . PHP_EOL;
  137. }
  138. }
  139. // Get the ClassFileLocator, and pass it the library path
  140. $l = new ClassFileLocator($libraryPath);
  141. // Iterate over each element in the path, and create a map of
  142. // classname => filename, where the filename is relative to the library path
  143. $map = new stdClass;
  144. foreach ($l as $file) {
  145. $filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
  146. // Add in relative path to library
  147. $filename = $relativePathForClassmap . $filename;
  148. foreach ($file->getClasses() as $class) {
  149. foreach ($ignoreNamespaces as $ignoreNs) {
  150. if ($ignoreNs == substr($class, 0, strlen($ignoreNs))) {
  151. continue 2;
  152. }
  153. }
  154. $map->{$class} = $filename;
  155. }
  156. }
  157. $map = (array) $map;
  158. if ($opts->getOption('s')) {
  159. ksort($map);
  160. }
  161. if ($appending) {
  162. $content = var_export($map, true) . ';';
  163. // Prefix with __DIR__; modify the generated content
  164. $content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
  165. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  166. $content = str_replace("\\'", "'", $content);
  167. // Convert to an array and remove the first "array("
  168. $content = explode("\n", $content);
  169. array_shift($content);
  170. // Load existing class map file and remove the closing "bracket ");" from it
  171. $existing = file($output, FILE_IGNORE_NEW_LINES);
  172. array_pop($existing);
  173. // Merge
  174. $content = implode("\n", array_merge($existing, $content));
  175. } else {
  176. // Create a file with the class/file map.
  177. // Stupid syntax highlighters make separating < from PHP declaration necessary
  178. $content = '<' . "?php\n"
  179. . "// Generated by ZF2's ./bin/classmap_generator.php\n"
  180. . 'return ' . var_export($map, true) . ';';
  181. // Prefix with __DIR__; modify the generated content
  182. $content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
  183. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  184. $content = str_replace("\\'", "'", $content);
  185. }
  186. // Remove unnecessary double-backslashes
  187. $content = str_replace('\\\\', '\\', $content);
  188. // Exchange "array (" width "array("
  189. $content = str_replace('array (', 'array(', $content);
  190. // Identing array content
  191. $content = preg_replace('(\n )', "\n ", $content);
  192. // Align "=>" operators to match coding standard
  193. preg_match_all('(\n\s+([^=]+)=>)', $content, $matches, PREG_SET_ORDER);
  194. $maxWidth = 0;
  195. foreach ($matches as $match) {
  196. $maxWidth = max($maxWidth, strlen($match[1]));
  197. }
  198. $content = preg_replace_callback('(\n\s+([^=]+)=>)', function ($matches) {
  199. global $maxWidth;
  200. return str_replace(' =>', str_repeat(' ', $maxWidth - strlen($matches[1])) . ' =>', $matches[0]);
  201. }, $content);
  202. // Make the file end by EOL
  203. $content = rtrim($content, "\n") . "\n";
  204. // Write the contents to disk
  205. file_put_contents($output, $content);
  206. if (!$usingStdout) {
  207. echo "Wrote classmap file to '" . realpath($output) . "'" . PHP_EOL;
  208. }