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
#!/usr/bin/env php
<?php
/**
* @link http://github.com/zendframework/zend-view for the canonical source repository
* @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
$help = <<< EOH
Generate template maps.
Usage:
templatemap_generator.php [-h|--help] templatepath <files...>
--help|-h Print this usage message.
templatepath Path to templates relative to current working
path; used to identify what to strip from
template names. Must be a directory.
<files...> List of files to include in the template
map, relative to the current working path.
The script assumes that paths included in the template map are relative
to the current working directory.
The script will output a PHP script that will return the template map
on successful completion. You may save this to a file using standard
piping operators; use ">" to write to/ovewrite a file, ">>" to append
to a file (which may have unexpected and/or intended results; you will
need to edit the file after generation to ensure it contains valid
PHP).
We recommend you then include the generated file within your module
configuration:
'template_map' => include __DIR__ . '/template_map.config.php',
If only the templatepath argument is provided, the script will look for
all .phtml files under that directory, creating a map for you.
If you want to specify a specific list of files -- for instance, if you
are using an extension other than .phtml -- we recommend one of the
following constructs:
For any shell, you can pipe the results of `find`:
$(find ../view -name '*.phtml')
For zsh, or bash where you have enabled globstar (`shopt -s globstar` in
either your bash profile or from within your terminal):
../view/**/*.phtml
Examples:
# Using only a templatepath argument, which will match any .phtml
# files found under the provided path:
$ cd module/Application/config/
$ ../../../vendor/bin/templatemap_generator.php ../view > template_map.config.php
# Create a template_map.config.php file in the Application module's
# config directory, relative to the view directory, and only containing
# .html.php files; overwrite any existing files:
$ cd module/Application/config/
$ ../../../vendor/bin/templatemap_generator.php ../view ../view/**/*.html.php > template_map.config.php
# OR using find:
$ ../../../vendor/bin/templatemap_generator.php \
> ../view \
> $(find ../view -name '*.html.php') > template_map.config.php
EOH;
// Called without arguments
if ($argc < 2) {
fwrite(STDERR, 'No arguments provided.' . PHP_EOL . PHP_EOL);
fwrite(STDERR, $help . PHP_EOL);
exit(2);
}
// Requested help
if (in_array($argv[1], ['-h', '--help'], true)) {
echo $help, PHP_EOL;
exit(0);
}
// Invalid path argument
if (! is_dir($argv[1])) {
fwrite(STDERR, 'templatepath argument is not a directory.' . PHP_EOL . PHP_EOL);
fwrite(STDERR, $help . PHP_EOL);
exit(2);
}
$basePath = $argv[1];
$files = ($argc < 3)
? findTemplateFilesInTemplatePath($basePath)
: array_slice($argv, 2);
// No files provided
if (empty($files)) {
fwrite(STDERR, 'No files specified.' . PHP_EOL . PHP_EOL);
fwrite(STDERR, $help . PHP_EOL);
exit(2);
}
$map = [];
$realPath = realpath($basePath);
$entries = array_map(function ($file) use ($basePath, $realPath) {
$file = str_replace('\\', '/', $file);
$template = (0 === strpos($file, $realPath))
? substr($file, strlen($realPath))
: $file;
$template = (0 === strpos($template, $basePath))
? substr($template, strlen($basePath))
: $template;
$template = preg_match('#(?P<template>.*?)\.[a-z0-9]+$#i', $template, $matches)
? $matches['template']
: $template;
$template = preg_replace('#^\.*/#', '', $template);
return sprintf(" '%s' => __DIR__ . '/%s',", $template, $file);
}, $files);
echo '<' . "?php\nreturn [\n"
. implode("\n", $entries) . "\n"
. '];';
exit(0);
function findTemplateFilesInTemplatePath($templatePath)
{
$rdi = new RecursiveDirectoryIterator(
$templatePath,
RecursiveDirectoryIterator::FOLLOW_SYMLINKS | RecursiveDirectoryIterator::SKIP_DOTS
);
$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::LEAVES_ONLY);
$files = [];
foreach ($rii as $file) {
if (strtolower($file->getExtension()) != 'phtml') {
continue;
}
$files[] = $file->getPathname();
}
return $files;
}