minor fix in hover for prior commit
[openemr.git] / vendor / zendframework / zendframework / bin / pluginmap_generator.php
blobfc12921a722ce3f4e0fa8adc5f1f0af6220c2e1e
1 #!/usr/bin/env php
2 <?php
3 /**
4 * Zend Framework (http://framework.zend.com/)
6 * @link http://github.com/zendframework/zf2 for the canonical source repository
7 * @category Zend
8 * @package Zend_Loader
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
14 use Zend\Console;
15 use Zend\Loader\StandardAutoloader;
17 /**
18 * Generate class maps for use with autoloading.
20 * Usage:
21 * --help|-h Get usage message
22 * --library|-l [ <string> ] Library to parse; if none provided, assumes
23 * current directory
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
28 * file
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;
36 exit(2);
38 } else {
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;
42 exit(2);
46 // Setup autoloading
47 $loader = new StandardAutoloader(array('autoregister_zf' => true));
48 $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 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',
58 try {
59 $opts = new Console\Getopt($rules);
60 $opts->parse();
61 } catch (Console\Exception\RuntimeException $e) {
62 echo $e->getUsageMessage();
63 exit(2);
66 if ($opts->getOption('h')) {
67 echo $opts->getUsageMessage();
68 exit();
71 $path = $libPath;
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();
82 exit(2);
84 $path = realpath($libraryPath);
87 $usingStdout = false;
88 $appending = $opts->getOption('a');
89 $output = $path . DIRECTORY_SEPARATOR . 'plugin_classmap.php';
90 if (isset($opts->o)) {
91 $output = $opts->o;
92 if ('-' == $output) {
93 $output = STDOUT;
94 $usingStdout = true;
95 } elseif (!is_writeable(dirname($output))) {
96 echo "Cannot write to '$output'; aborting." . PHP_EOL
97 . PHP_EOL
98 . $opts->getUsageMessage();
99 exit(2);
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
104 . PHP_EOL
105 . $opts->getUsageMessage();
106 exit(2);
111 if (!$usingStdout) {
112 if ($appending) {
113 echo "Appending to plugin class map '$output' for classes in '$path'..." . PHP_EOL;
114 } else {
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) {
131 $namespace .= '\\';
132 if (0 === strpos($plugin, $namespace)) {
133 $plugin = str_replace($namespace, '', $plugin);
136 $plugin = strtolower($plugin);
137 $map->{$plugin} = $classname;
141 if ($appending) {
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);
155 // Merge
156 $content = implode("\n", $existing + $content);
157 } else {
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);
176 if (!$usingStdout) {
177 echo "Wrote plugin classmap file to '" . realpath($output) . "'" . PHP_EOL;