Updated ACL help file, minor styling changes (#1590)
[openemr.git] / vendor / zendframework / zendframework / bin / templatemap_generator.php
blob8acd7b0ac9df09b0c0bc24e182f22fa4e23de807
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 * @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 */
11 use Zend\Console;
12 use Zend\Loader\StandardAutoloader;
14 /**
15 * Generate template maps.
17 * Usage:
18 * --help|-h Get usage message
19 * --library|-l [ <string> ] Library to parse; if none provided, assumes
20 * current directory
21 * --view|-v [ <string> ] View path to parse; if none provided, assumes
22 * view as template directory
23 * --extensions|-e [ <string> ] List of accepted file extensions (regex alternation
24 * without parenthesis); default: *
25 * --output|-o [ <string> ] Where to write map file; if not provided,
26 * assumes "template_map.php" in library directory
27 * --append|-a Append to map file if it exists
28 * --overwrite|-w Whether or not to overwrite existing map file
31 // Setup/verify autoloading
32 if (file_exists(__DIR__ . '/../vendor/autoload.php')) {
33 // Local install
34 require __DIR__ . '/../vendor/autoload.php';
35 } elseif (file_exists(getcwd() . '/vendor/autoload.php')) {
36 // Root project is current working directory
37 require getcwd() . '/vendor/autoload.php';
38 } elseif (file_exists(__DIR__ . '/../../../autoload.php')) {
39 // Relative to composer install
40 require __DIR__ . '/../../../autoload.php';
41 } else {
42 fwrite(STDERR, "Unable to setup autoloading; aborting\n");
43 exit(2);
46 $libraryPath = getcwd();
47 $viewPath = getcwd() . '/view';
49 $rules = array(
50 'help|h' => 'Get usage message',
51 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
52 'view|v-s' => 'View path to parse; if none provided, assumes view as template directory',
53 'extensions|e-s' => 'List of accepted file extensions (regex alternation: *html, phtml|tpl); default: *',
54 'output|o-s' => 'Where to write map file; if not provided, assumes "template_map.php" in library directory',
55 'append|a' => 'Append to map file if it exists',
56 'overwrite|w' => 'Whether or not to overwrite existing map file',
59 try {
60 $opts = new Console\Getopt($rules);
61 $opts->parse();
62 } catch (Console\Exception\RuntimeException $e) {
63 echo $e->getUsageMessage();
64 exit(2);
67 if ($opts->getOption('h')) {
68 echo $opts->getUsageMessage();
69 exit(0);
72 $fileExtensions = '*';
73 if (isset($opts->e) && $opts->e != '*') {
74 if (!preg_match('/^(\*?[[:alnum:]]\*?+\|?)+$/', $opts->e)) {
75 echo 'Invalid extensions list specified. Expecting wildcard or alternation: *, *html, phtml|tpl' . PHP_EOL
76 . PHP_EOL;
77 echo $opts->getUsageMessage();
78 exit(2);
80 $fileExtensions = '(' . $opts->e . ')';
82 $fileExtensions = str_replace('*', '.*', $fileExtensions);
84 $relativePathForMap = '';
85 if (isset($opts->l)) {
86 if (!is_dir($opts->l)) {
87 echo 'Invalid library directory provided' . PHP_EOL
88 . PHP_EOL;
89 echo $opts->getUsageMessage();
90 exit(2);
92 $libraryPath = $opts->l;
94 $libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath));
96 if (isset($opts->v)) {
97 if (!is_dir($opts->v)) {
98 echo 'Invalid view template directory provided' . PHP_EOL
99 . PHP_EOL;
100 echo $opts->getUsageMessage();
101 exit(2);
103 $viewPath = $opts->v;
106 if (!is_dir($viewPath)) {
107 printf('Invalid view path provided (%s)', $viewPath);
108 echo PHP_EOL . PHP_EOL;
109 echo $opts->getUsageMessage();
110 exit(2);
113 $viewPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath));
115 $usingStdout = false;
116 $appending = $opts->getOption('a');
117 $output = $libraryPath . '/template_map.php';
118 if (isset($opts->o)) {
119 $output = $opts->o;
120 if ('-' == $output) {
121 $output = STDOUT;
122 $usingStdout = true;
123 } elseif (is_dir($output)) {
124 echo 'Invalid output file provided' . PHP_EOL
125 . PHP_EOL;
126 echo $opts->getUsageMessage();
127 exit(2);
128 } elseif (!is_writeable(dirname($output))) {
129 echo "Cannot write to '$output'; aborting." . PHP_EOL
130 . PHP_EOL
131 . $opts->getUsageMessage();
132 exit(2);
133 } elseif (file_exists($output) && !$opts->getOption('w') && !$appending) {
134 echo "Template map file already exists at '$output'," . PHP_EOL
135 . "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL
136 . PHP_EOL
137 . $opts->getUsageMessage();
138 exit(2);
139 } else {
140 // We need to add the $libraryPath into the relative path that is created in the template map file.
141 $mapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output)));
143 // Simple case: $libraryPathCompare is in $mapPathCompare
144 if (strpos($libraryPath, $mapPath) === 0) {
145 $relativePathForMap = substr($libraryPath, strlen($mapPath) + 1) . '/';
146 } else {
147 $libraryPathParts = explode('/', $libraryPath);
148 $mapPathParts = explode('/', $mapPath);
150 // Find the common part
151 $count = count($mapPathParts);
152 for ($i = 0; $i < $count; $i++) {
153 if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $mapPathParts[$i]) {
154 // Common part end
155 break;
159 // Add parent dirs for the subdirs of map
160 $relativePathForMap = str_repeat('../', $count - $i);
162 // Add library subdirs
163 $count = count($libraryPathParts);
164 for (; $i < $count; $i++) {
165 $relativePathForMap .= $libraryPathParts[$i] . '/';
171 if (!$usingStdout) {
172 if ($appending) {
173 echo "Appending to template file map '$output' for library in '$libraryPath'..." . PHP_EOL;
174 } else {
175 echo "Creating template file map for library in '$libraryPath'..." . PHP_EOL;
179 $dirOrIterator = new RecursiveDirectoryIterator($viewPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
180 $l = new RecursiveIteratorIterator($dirOrIterator);
182 // Iterate over each element in the path, and create a map of
183 // template name => filename, where the filename is relative to the view path
184 $map = new stdClass;
185 foreach ($l as $file) {
186 /* @var $file SplFileInfo */
187 if (!$file->isFile() || !preg_match('/^' . $fileExtensions . '$/', $file->getExtension())) {
188 continue;
190 $filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
192 // Add in relative path to library
193 $filename = $relativePathForMap . $filename;
194 $baseName = $file->getBasename('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION));
195 $mapName = str_replace(str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath)) . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $baseName);
196 $map->{$mapName} = $filename;
199 // Create a file with the map.
201 if ($appending && file_exists($output) && is_array(include $output)) {
202 // Append mode and the output file already exists: retrieve its
203 // content and merges with the new map
204 // Remove the last line as it is the end of the array, and we want to
205 // append our new templates
206 $content = file($output, FILE_IGNORE_NEW_LINES);
207 array_pop($content);
208 $content = implode(PHP_EOL, $content) . PHP_EOL;
209 } else {
210 // Write mode or the file does not exists: create a new file
211 // Stupid syntax highlighters make separating < from PHP declaration necessary
212 $content = '<' . "?php" . PHP_EOL
213 . '// Generated by ZF2\'s ./bin/templatemap_generator.php' . PHP_EOL
214 . 'return array(' . PHP_EOL;
217 // Process the template map as a string before inserting it to the output file
219 $mapExport = var_export((array) $map, true);
221 // Prefix with __DIR__
222 $mapExport = preg_replace("#(=> ')#", "=> __DIR__ . '/", $mapExport);
224 // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
225 $mapExport = str_replace("\\'", "'", $mapExport);
227 // Remove unnecessary double-backslashes
228 $mapExport = str_replace('\\\\', '\\', $mapExport);
230 // Remove "array ("
231 $mapExport = str_replace('array (', '', $mapExport);
233 // Align "=>" operators to match coding standard
234 preg_match_all('(\n\s+([^=]+)=>)', $mapExport, $matches, PREG_SET_ORDER);
235 $maxWidth = 0;
237 foreach ($matches as $match) {
238 $maxWidth = max($maxWidth, strlen($match[1]));
241 $mapExport = preg_replace_callback('(\n\s+([^=]+)=>)', function ($matches) use ($maxWidth) {
242 return PHP_EOL . ' ' . $matches[1] . str_repeat(' ', $maxWidth - strlen($matches[1])) . '=>';
243 }, $mapExport);
245 // Trim the content
246 $mapExport = trim($mapExport, "\n");
248 // Append the map to the file, close the array and write a new line
249 $content .= $mapExport . ';' . PHP_EOL;
251 // Write the contents to disk
252 file_put_contents($output, $content);
254 if (!$usingStdout) {
255 echo "Wrote templatemap file to '" . realpath($output) . "'" . PHP_EOL;