4 * Zend Framework (http://framework.zend.com/)
6 * @link http://github.com/zendframework/zf2 for the canonical source repository
9 * @subpackage Exception
10 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
11 * @license http://framework.zend.com/license/new-bsd New BSD License
15 use Zend\Loader\StandardAutoloader
;
18 * Generate class maps for use with autoloading.
21 * --help|-h Get usage message
22 * --library|-l [ <string> ] Library to parse; if none provided, assumes
24 * --output|-o [ <string> ] Where to write autoload file; if not provided,
25 * assumes "autoload_classmap.php" in library directory
26 * --append|-a Append to autoload file if it exists
27 * --overwrite|-w Whether or not to overwrite existing autoload
31 $libPath = getenv('LIB_PATH') ?
getenv('LIB_PATH') : __DIR__
. '/../library';
32 if (!is_dir($libPath)) {
33 // Try to load StandardAutoloader from include_path
34 if (false === (include 'Zend/Loader/StandardAutoloader.php')) {
35 echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL
;
39 // Try to load StandardAutoloader from library
40 if (false === (include $libPath . '/Zend/Loader/StandardAutoloader.php')) {
41 echo "Unable to locate autoloader via library; aborting" . PHP_EOL
;
47 $loader = new StandardAutoloader(array('autoregister_zf' => true));
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 plugin map file; if not provided, assumes "plugin_classmap.php" in library directory',
54 'append|a' => 'Append to plugin map file if it exists',
55 'overwrite|w' => 'Whether or not to overwrite existing autoload file',
59 $opts = new Console\
Getopt($rules);
61 } catch (Console\Exception\RuntimeException
$e) {
62 echo $e->getUsageMessage();
66 if ($opts->getOption('h')) {
67 echo $opts->getUsageMessage();
72 if (array_key_exists('PWD', $_SERVER)) {
73 $path = $_SERVER['PWD'];
76 if (isset($opts->l
)) {
77 $libraryPath = $opts->l
;
78 $libraryPath = rtrim($libraryPath, '/\\') . DIRECTORY_SEPARATOR
;
79 if (!is_dir($libraryPath)) {
80 echo "Invalid library directory provided" . PHP_EOL
. PHP_EOL
;
81 echo $opts->getUsageMessage();
84 $path = realpath($libraryPath);
88 $appending = $opts->getOption('a');
89 $output = $path . DIRECTORY_SEPARATOR
. 'plugin_classmap.php';
90 if (isset($opts->o
)) {
95 } elseif (!is_writeable(dirname($output))) {
96 echo "Cannot write to '$output'; aborting." . PHP_EOL
98 . $opts->getUsageMessage();
100 } elseif (file_exists($output)) {
101 if (!$opts->getOption('w') && !$appending) {
102 echo "Plugin map file already exists at '$output'," . PHP_EOL
103 . "but 'overwrite' flag was not specified; aborting." . PHP_EOL
105 . $opts->getUsageMessage();
113 echo "Appending to plugin class map '$output' for classes in '$path'..." . PHP_EOL
;
115 echo "Creating plugin class map for classes in '$path'..." . PHP_EOL
;
119 // Get the ClassFileLocator, and pass it the library path
120 $l = new \Zend\File\
ClassFileLocator($path);
122 // Iterate over each element in the path, and create a map of pluginname => classname
123 $map = new \stdClass
;
124 foreach ($l as $file) {
125 $namespaces = $file->getNamespaces();
126 $namespace = empty($file->namespace) ?
'' : $file->namespace . '\\';
128 foreach ($file->getClasses() as $classname) {
129 $plugin = $classname;
130 foreach ($namespaces as $namespace) {
132 if (0 === strpos($plugin, $namespace)) {
133 $plugin = str_replace($namespace, '', $plugin);
136 $plugin = strtolower($plugin);
137 $map->{$plugin} = $classname;
142 $content = var_export((array) $map, true) . ';';
144 // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
145 $content = str_replace("\\'", "'", $content);
147 // Convert to an array and remove the first "array ("
148 $content = explode("\n", $content);
149 array_shift($content);
151 // Load existing class map file and remove the closing "bracket ");" from it
152 $existing = file($output, FILE_IGNORE_NEW_LINES
);
153 array_pop($existing);
156 $content = implode("\n", $existing +
$content);
158 // Create a file with the class/file map.
159 // Stupid syntax highlighters make separating < from PHP declaration necessary
160 $content = '<' . "?php\n\n"
161 . "// plugin class map\n"
162 . "// auto-generated using "
163 . basename($_SERVER['argv'][0]) . ', ' . date('Y-m-d H:i:s') . "\n\n"
164 . 'return ' . var_export((array) $map, true) . ';';
166 // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
167 $content = str_replace("\\'", "'", $content);
170 // Make the file end by EOL
171 $content = rtrim($content, "\n") . "\n";
173 // Write the contents to disk
174 file_put_contents($output, $content);
177 echo "Wrote plugin classmap file to '" . realpath($output) . "'" . PHP_EOL
;