Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
classmap_generator.php
Go to the documentation of this file.
1 <?php
37 $libPath = dirname(__FILE__) . '/../library';
38 if (!is_dir($libPath)) {
39  // Try to load StandardAutoloader from include_path
40  if (false === include('Zend/Loader/StandardAutoloader.php')) {
41  echo "Unable to locate autoloader via include_path; aborting" . PHP_EOL;
42  exit(2);
43  }
44 } else {
45  // Try to load StandardAutoloader from library
46  if (false === include(dirname(__FILE__) . '/../library/Zend/Loader/StandardAutoloader.php')) {
47  echo "Unable to locate autoloader via library; aborting" . PHP_EOL;
48  exit(2);
49  }
50 }
51 
52 $libraryPath = getcwd();
53 
54 // Setup autoloading
55 $loader = new Zend_Loader_StandardAutoloader(array('autoregister_zf' => true));
56 $loader->setFallbackAutoloader(true);
57 $loader->register();
58 
59 $rules = array(
60  'help|h' => 'Get usage message',
61  'library|l-s' => 'Library to parse; if none provided, assumes current directory',
62  'output|o-s' => 'Where to write autoload file; if not provided, assumes "autoload_classmap.php" in library directory',
63  'append|a' => 'Append to autoload file if it exists',
64  'overwrite|w' => 'Whether or not to overwrite existing autoload file',
65  'ignore|i-s' => 'Comma-separated namespaces to ignore',
66 );
67 
68 try {
70  $opts->parse();
71 } catch (Zend_Console_Getopt_Exception $e) {
72  echo $e->getUsageMessage();
73  exit(2);
74 }
75 
76 if ($opts->getOption('h')) {
77  echo $opts->getUsageMessage();
78  exit(0);
79 }
80 
82 if (isset($opts->i)) {
83  $ignoreNamespaces = explode(',', $opts->i);
84 }
85 
87 if (isset($opts->l)) {
88  if (!is_dir($opts->l)) {
89  echo 'Invalid library directory provided' . PHP_EOL
90  . PHP_EOL;
91  echo $opts->getUsageMessage();
92  exit(2);
93  }
94  $libraryPath = $opts->l;
95 }
96 $libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath));
97 
98 $usingStdout = false;
99 $appending = $opts->getOption('a');
100 $output = $libraryPath . '/autoload_classmap.php';
101 if (isset($opts->o)) {
102  $output = $opts->o;
103  if ('-' == $output) {
104  $output = STDOUT;
105  $usingStdout = true;
106  } elseif (is_dir($output)) {
107  echo 'Invalid output file provided' . PHP_EOL
108  . PHP_EOL;
109  echo $opts->getUsageMessage();
110  exit(2);
111  } elseif (!is_writeable(dirname($output))) {
112  echo "Cannot write to '$output'; aborting." . PHP_EOL
113  . PHP_EOL
114  . $opts->getUsageMessage();
115  exit(2);
116  } elseif (file_exists($output) && !$opts->getOption('w') && !$appending) {
117  echo "Autoload file already exists at '$output'," . PHP_EOL
118  . "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL
119  . PHP_EOL
120  . $opts->getUsageMessage();
121  exit(2);
122  } else {
123  // We need to add the $libraryPath into the relative path that is created in the classmap file.
124  $classmapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output)));
125 
126  // Simple case: $libraryPathCompare is in $classmapPathCompare
127  if (strpos($libraryPath, $classmapPath) === 0) {
128  $relativePathForClassmap = substr($libraryPath, strlen($classmapPath) + 1) . '/';
129  } else {
130  $libraryPathParts = explode('/', $libraryPath);
131  $classmapPathParts = explode('/', $classmapPath);
132 
133  // Find the common part
134  $count = count($classmapPathParts);
135  for ($i = 0; $i < $count; $i++) {
136  if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $classmapPathParts[$i]) {
137  // Common part end
138  break;
139  }
140  }
141 
142  // Add parent dirs for the subdirs of classmap
143  $relativePathForClassmap = str_repeat('../', $count - $i);
144 
145  // Add library subdirs
146  $count = count($libraryPathParts);
147  for (; $i < $count; $i++) {
148  $relativePathForClassmap .= $libraryPathParts[$i] . '/';
149  }
150  }
151  }
152 }
153 
154 if (!$usingStdout) {
155  if ($appending) {
156  echo "Appending to class file map '$output' for library in '$libraryPath'..." . PHP_EOL;
157  } else {
158  echo "Creating class file map for library in '$libraryPath'..." . PHP_EOL;
159  }
160 }
161 
162 // Get the ClassFileLocator, and pass it the library path
164 
165 // Iterate over each element in the path, and create a map of
166 // classname => filename, where the filename is relative to the library path
167 $map = new stdClass;
168 foreach ($l as $file) {
169  $filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
170 
171  // Add in relative path to library
172  $filename = $relativePathForClassmap . $filename;
173 
174  foreach ($file->getClasses() as $class) {
175  foreach ($ignoreNamespaces as $ignoreNs) {
176  if ($ignoreNs == substr($class, 0, strlen($ignoreNs))) {
177  continue 2;
178  }
179  }
180 
181  $map->{$class} = $filename;
182  }
183 }
184 
185 if ($appending) {
186  $content = var_export((array) $map, true) . ';';
187 
188  // Prefix with dirname(__FILE__); modify the generated content
189  $content = preg_replace("#(=> ')#", "=> dirname(__FILE__) . '/", $content);
190 
191  // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
192  $content = str_replace("\\'", "'", $content);
193 
194  // Convert to an array and remove the first "array("
195  $content = explode("\n", $content);
196  array_shift($content);
197 
198  // Load existing class map file and remove the closing "bracket ");" from it
199  $existing = file($output, FILE_IGNORE_NEW_LINES);
200  array_pop($existing);
201 
202  // Merge
203  $content = implode("\n", array_merge($existing, $content));
204 } else {
205  // Create a file with the class/file map.
206  // Stupid syntax highlighters make separating < from PHP declaration necessary
207  $content = '<' . "?php\n"
208  . "// Generated by ZF's ./bin/classmap_generator.php\n"
209  . 'return ' . var_export((array) $map, true) . ';';
210 
211  // Prefix with dirname(__FILE__); modify the generated content
212  $content = preg_replace("#(=> ')#", "=> dirname(__FILE__) . '/", $content);
213 
214  // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
215  $content = str_replace("\\'", "'", $content);
216 }
217 
218 // Remove unnecessary double-backslashes
219 $content = str_replace('\\\\', '\\', $content);
220 
221 // Exchange "array (" width "array("
222 $content = str_replace('array (', 'array(', $content);
223 
224 // Align "=>" operators to match coding standard
225 preg_match_all('(\n\s+([^=]+)=>)', $content, $matches, PREG_SET_ORDER);
227 
228 foreach ($matches as $match) {
229  $maxWidth = max($maxWidth, strlen($match[1]));
230 }
231 
232 $content = preg_replace('(\n\s+([^=]+)=>)e', "'\n \\1' . str_repeat(' ', " . $maxWidth . " - strlen('\\1')) . '=>'", $content);
233 
234 // Make the file end by EOL
235 $content = rtrim($content, "\n") . "\n";
236 
237 // Write the contents to disk
239 
240 if (!$usingStdout) {
241  echo "Wrote classmap file to '" . realpath($output) . "'" . PHP_EOL;
242 }
catch(Zend_Console_Getopt_Exception $e) if($opts->getOption('h')) $ignoreNamespaces
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
defined('MTF_BOOT_FILE')||define('MTF_BOOT_FILE' __FILE__
Definition: bootstrap.php:7
$_option $_optionId $class
Definition: date.phtml:13
exit
Definition: redirect.phtml:12
if(isset($opts->i)) $relativePathForClassmap
if(isset($opts->l)) $libraryPath
if(isset($opts->o)) if(! $usingStdout) $l
$i
Definition: gallery.phtml:31