PageRenderTime 64ms CodeModel.GetById 37ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/zfals.php

https://github.com/lazenge/zf2
PHP | 156 lines | 137 code | 14 blank | 5 comment | 21 complexity | 0ba3055f1ddb7b3f9b59d00429aad2de MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. $libPath = __DIR__ . '/../library';
  3. if (!is_dir($libPath)) {
  4. echo "Unable to find Zend Framework library; aborting" . PHP_EOL;
  5. exit(2);
  6. }
  7. $libPath = realpath($libPath);
  8. // Add ZF to the include_path, if it isn't already
  9. $incPath = get_include_path();
  10. if (!strstr($incPath, $libPath)) {
  11. set_include_path($libPath . PATH_SEPARATOR . $incPath);
  12. }
  13. // Setup autoloading
  14. $loader = new Zend\Loader\StandardAutoloader();
  15. require_once 'Zend/Loader/StandardAutoloader.php';
  16. $loader->register();
  17. $rules = array(
  18. 'help|h' => 'Get usage message',
  19. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  20. 'namespace|n-s' => 'Namespace in which to create map; by default, uses last segment of library directory name',
  21. 'output|o-s' => 'Where to write autoload file; if not provided, assumes "_autoload.php" in library directory',
  22. 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
  23. 'keepdepth|k-i' => 'How many additional segments of the library path to keep in the generated classfile map',
  24. 'usedir|d' => 'Prepend filenames with __DIR__',
  25. );
  26. try {
  27. $opts = new Zend\Console\Getopt($rules);
  28. $opts->parse();
  29. } catch (Zend\Console\Getopt\Exception $e) {
  30. echo $e->getUsageMessage();
  31. exit(2);
  32. }
  33. if ($opts->getOption('h')) {
  34. echo $opts->getUsageMessage();
  35. exit();
  36. }
  37. $path = $libPath;
  38. if (array_key_exists('PWD', $_SERVER)) {
  39. $path = $_SERVER['PWD'];
  40. }
  41. if (isset($opts->l)) {
  42. $path = $opts->l;
  43. if (!is_dir($path)) {
  44. echo "Invalid library directory provided" . PHP_EOL . PHP_EOL;
  45. echo $opts->getUsageMessage();
  46. exit(2);
  47. }
  48. $path = realpath($path);
  49. }
  50. $namespace = substr($path, strrpos($path, DIRECTORY_SEPARATOR) + 1);
  51. if (isset($opts->n)) {
  52. $tmp = $opts->n;
  53. if (!empty($tmp)) {
  54. if (!preg_match('#^[a-z][a-z0-9]*(\\\\[a-z][a-z[0-9_]])*#', $tmp)) {
  55. echo "Invalid namespace provided; aborting." . PHP_EOL
  56. . PHP_EOL
  57. . $opts->getUsageMessage();
  58. exit(2);
  59. }
  60. $namespace = $tmp;
  61. }
  62. }
  63. $usingStdout = false;
  64. $output = $path . DIRECTORY_SEPARATOR . '_autoload.php';
  65. if (isset($opts->o)) {
  66. $output = $opts->o;
  67. if ('-' == $output) {
  68. $output = STDOUT;
  69. $usingStdout = true;
  70. } elseif (!is_writeable(dirname($output))) {
  71. echo "Cannot write to '$output'; aborting." . PHP_EOL
  72. . PHP_EOL
  73. . $opts->getUsageMessage();
  74. exit(2);
  75. } elseif (file_exists($output)) {
  76. if (!$opts->getOption('w')) {
  77. echo "Autoload file already exists at '$output'," . PHP_EOL
  78. . "but 'overwrite' flag was not specified; aborting." . PHP_EOL
  79. . PHP_EOL
  80. . $opts->getUsageMessage();
  81. exit(2);
  82. }
  83. }
  84. }
  85. $strip = $path;
  86. $keepDepth = 0;
  87. if (isset($opts->k)) {
  88. $keepDepth = $opts->k;
  89. if ($keepDepth < 0) {
  90. $keepDepth = 0;
  91. }
  92. }
  93. if ($keepDepth > 0) {
  94. $segments = explode(DIRECTORY_SEPARATOR, $path);
  95. do {
  96. array_pop($segments);
  97. --$keepDepth;
  98. } while (count($segments) > 0 && $keepDepth > 0);
  99. $strip = implode(DIRECTORY_SEPARATOR, $segments);
  100. }
  101. $prefixWithDir = $opts->getOption('d');
  102. if (!$usingStdout) {
  103. echo "Creating class file map for library in '$path'..." . PHP_EOL;
  104. }
  105. // Get the ClassFileLocator, and pass it the library path
  106. $l = new \Zend\File\ClassFileLocator($path);
  107. // Iterate over each element in the path, and create a map of
  108. // classname => filename, where the filename is relative to the library path
  109. $map = new \stdClass;
  110. $strip .= DIRECTORY_SEPARATOR;
  111. iterator_apply($l, function() use ($l, $map, $strip){
  112. $file = $l->current();
  113. $namespace = empty($file->namespace) ? '' : $file->namespace . '\\';
  114. $filename = str_replace($strip, '', $file->getRealpath());
  115. $map->{$namespace . $file->classname} = $filename;
  116. return true;
  117. });
  118. // Create a file with the class/file map.
  119. // Stupid syntax highlighters make separating < from PHP declaration necessary
  120. $map = var_export((array) $map, true);
  121. $content =<<<EOT
  122. <?php
  123. namespace $namespace;
  124. \$_map = $map;
  125. spl_autoload_register(function(\$class) use (\$_map) {
  126. if (array_key_exists(\$class, \$_map)) {
  127. require_once \$_map[\$class];
  128. }
  129. });
  130. EOT;
  131. // If requested to prefix with __DIR__, modify the content
  132. if ($prefixWithDir) {
  133. $content = preg_replace('#(=> )#', '$1__DIR__ . DIRECTORY_SEPARATOR . ', $content);
  134. }
  135. file_put_contents($output, $content);
  136. if (!$usingStdout) {
  137. echo "Wrote autoload file to '" . realpath($output) . "'" . PHP_EOL;
  138. }