forked from zendframework/zendframework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
templatemap_generator.php
executable file
·243 lines (205 loc) · 8.3 KB
/
templatemap_generator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env php
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
use Zend\Console;
use Zend\Loader\StandardAutoloader;
/**
* Generate template maps.
*
* Usage:
* --help|-h Get usage message
* --library|-l [ <string> ] Library to parse; if none provided, assumes
* current directory
* --output|-o [ <string> ] Where to write map file; if not provided,
* assumes "template_map.php" in library directory
* --append|-a Append to map file if it exists
* --overwrite|-w Whether or not to overwrite existing map file
*/
$zfLibraryPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
if (is_dir($zfLibraryPath)) {
// Try to load StandardAutoloader from library
if (false === include($zfLibraryPath . '/Zend/Loader/StandardAutoloader.php')) {
echo 'Unable to locate autoloader via library; aborting' . PHP_EOL;
exit(2);
}
} else {
// Try to load StandardAutoloader from include_path
if (false === include('Zend/Loader/StandardAutoloader.php')) {
echo 'Unable to locate autoloader via include_path; aborting' . PHP_EOL;
exit(2);
}
}
$libraryPath = getcwd();
$viewPath = getcwd() . '/view';
// Setup autoloading
$loader = new StandardAutoloader(array('autoregister_zf' => true));
$loader->register();
$rules = array(
'help|h' => 'Get usage message',
'library|l-s' => 'Library to parse; if none provided, assumes current directory',
'view|v-s' => 'View path to parse; if none provided, assumes view as template directory',
'output|o-s' => 'Where to write map file; if not provided, assumes "template_map.php" in library directory',
'append|a' => 'Append to map file if it exists',
'overwrite|w' => 'Whether or not to overwrite existing map file',
);
try {
$opts = new Console\Getopt($rules);
$opts->parse();
} catch (Console\Exception\RuntimeException $e) {
echo $e->getUsageMessage();
exit(2);
}
if ($opts->getOption('h')) {
echo $opts->getUsageMessage();
exit(0);
}
$relativePathForMap = '';
if (isset($opts->l)) {
if (!is_dir($opts->l)) {
echo 'Invalid library directory provided' . PHP_EOL
. PHP_EOL;
echo $opts->getUsageMessage();
exit(2);
}
$libraryPath = $opts->l;
}
$libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath));
if (isset($opts->v)) {
if (!is_dir($opts->v)) {
echo 'Invalid view template directory provided' . PHP_EOL
. PHP_EOL;
echo $opts->getUsageMessage();
exit(2);
}
$viewPath = $opts->v;
}
if (!is_dir($viewPath)) {
printf('Invalid view path provided (%s)', $viewPath);
echo PHP_EOL . PHP_EOL;
echo $opts->getUsageMessage();
exit(2);
}
$viewPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath));
$usingStdout = false;
$appending = $opts->getOption('a');
$output = $libraryPath . '/template_map.php';
if (isset($opts->o)) {
$output = $opts->o;
if ('-' == $output) {
$output = STDOUT;
$usingStdout = true;
} elseif (is_dir($output)) {
echo 'Invalid output file provided' . PHP_EOL
. PHP_EOL;
echo $opts->getUsageMessage();
exit(2);
} elseif (!is_writeable(dirname($output))) {
echo "Cannot write to '$output'; aborting." . PHP_EOL
. PHP_EOL
. $opts->getUsageMessage();
exit(2);
} elseif (file_exists($output) && !$opts->getOption('w') && !$appending) {
echo "Template map file already exists at '$output'," . PHP_EOL
. "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL
. PHP_EOL
. $opts->getUsageMessage();
exit(2);
} else {
// We need to add the $libraryPath into the relative path that is created in the template map file.
$mapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output)));
// Simple case: $libraryPathCompare is in $mapPathCompare
if (strpos($libraryPath, $mapPath) === 0) {
$relativePathForMap = substr($libraryPath, strlen($mapPath) + 1) . '/';
} else {
$libraryPathParts = explode('/', $libraryPath);
$mapPathParts = explode('/', $mapPath);
// Find the common part
$count = count($mapPathParts);
for ($i = 0; $i < $count; $i++) {
if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $mapPathParts[$i]) {
// Common part end
break;
}
}
// Add parent dirs for the subdirs of map
$relativePathForMap = str_repeat('../', $count - $i);
// Add library subdirs
$count = count($libraryPathParts);
for (; $i < $count; $i++) {
$relativePathForMap .= $libraryPathParts[$i] . '/';
}
}
}
}
if (!$usingStdout) {
if ($appending) {
echo "Appending to template file map '$output' for library in '$libraryPath'..." . PHP_EOL;
} else {
echo "Creating template file map for library in '$libraryPath'..." . PHP_EOL;
}
}
$dirOrIterator = new RecursiveDirectoryIterator($viewPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
$l = new RecursiveIteratorIterator($dirOrIterator);
// Iterate over each element in the path, and create a map of
// template name => filename, where the filename is relative to the view path
$map = new stdClass;
foreach ($l as $file) {
if (!$file->isFile()) {
continue;
}
$filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
// Add in relative path to library
$filename = $relativePathForMap . $filename;
$baseName = $file->getBasename('.' . $file->getExtension());
$mapName = str_replace(str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath)) . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $baseName);
$map->{$mapName} = $filename;
}
if ($appending) {
$content = var_export((array) $map, true) . ';';
// Prefix with __DIR__; modify the generated content
$content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
// Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
$content = str_replace("\\'", "'", $content);
// Convert to an array and remove the first "array("
$content = explode("\n", $content);
array_shift($content);
// Load existing map file and remove the closing "bracket ");" from it
$existing = file($output, FILE_IGNORE_NEW_LINES);
array_pop($existing);
// Merge
$content = implode("\n", array_merge($existing, $content));
} else {
// Create a file with the map.
// Stupid syntax highlighters make separating < from PHP declaration necessary
$content = '<' . "?php\n"
. "// Generated by ZF2's ./bin/templatemap_generator.php\n"
. 'return ' . var_export((array) $map, true) . ';';
// Prefix with __DIR__; modify the generated content
$content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
// Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
$content = str_replace("\\'", "'", $content);
}
// Remove unnecessary double-backslashes
$content = str_replace('\\\\', '\\', $content);
// Exchange "array (" width "array("
$content = str_replace('array (', 'array(', $content);
// Align "=>" operators to match coding standard
preg_match_all('(\n\s+([^=]+)=>)', $content, $matches, PREG_SET_ORDER);
$maxWidth = 0;
foreach ($matches as $match) {
$maxWidth = max($maxWidth, strlen($match[1]));
}
$content = preg_replace('(\n\s+([^=]+)=>)e', "'\n \\1' . str_repeat(' ', " . $maxWidth . " - strlen('\\1')) . '=>'", $content);
// Make the file end by EOL
$content = rtrim($content, "\n") . "\n";
// Write the contents to disk
file_put_contents($output, $content);
if (!$usingStdout) {
echo "Wrote templatemap file to '" . realpath($output) . "'" . PHP_EOL;
}