Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit e4545a3

Browse files
committed
Allow passing only the template path, as long as it contains .phtml files
Simplifies usage for the default use case. If developers want to specify an alternate extension, they can use file globbing.
1 parent fa46aff commit e4545a3

File tree

1 file changed

+46
-12
lines changed

1 file changed

+46
-12
lines changed

bin/templatemap_generator.php

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
1212
Usage:
1313
14-
templatemap_generator.php [-h|--help] templatepath files...
14+
templatemap_generator.php [-h|--help] templatepath <files...>
1515
1616
--help|-h Print this usage message.
1717
templatepath Path to templates relative to current working
1818
path; used to identify what to strip from
1919
template names. Must be a directory.
20-
files... List of files to include in the template
20+
<files...> List of files to include in the template
2121
map, relative to the current working path.
2222
2323
The script assumes that paths included in the template map are relative
@@ -30,7 +30,12 @@
3030
need to edit the file after generation to ensure it contains valid
3131
PHP).
3232
33-
To provide a list of files, we recommend using one of the following.
33+
If only the templatepath argument is provided, the script will look for
34+
all .phtml files under that directory, creating a map for you.
35+
36+
If you want to specify a specific list of files -- for instance, if you
37+
are using an extension other than .phtml -- we recommend one of the
38+
following constructs:
3439
3540
For any shell, you can pipe the results of `find`:
3641
@@ -48,16 +53,21 @@
4853
4954
Examples:
5055
56+
# Using only a templatepath argument, which will match any .phtml
57+
# files found under the provided path:
58+
$ cd module/Application/config/
59+
$ ../../../vendor/bin/templatemap_generator.php ../view > template_map.config.php
60+
5161
# Create a template_map.config.php file in the Application module's
5262
# config directory, relative to the view directory, and only containing
53-
# .phtml files; overwrite any existing files:
63+
# .html.php files; overwrite any existing files:
5464
$ cd module/Application/config/
55-
$ ../../../vendor/bin/templatemap_generator.php ../view ../view/**/*.phtml > template_map.config.php
65+
$ ../../../vendor/bin/templatemap_generator.php ../view ../view/**/*.html.php > template_map.config.php
5666
5767
# OR using find:
5868
$ ../../../vendor/bin/templatemap_generator.php \
5969
> ../view \
60-
> $(find ../view -name '*.phtml') > template_map.config.php
70+
> $(find ../view -name '*.html.php') > template_map.config.php
6171
EOH;
6272

6373
// Called without arguments
@@ -80,20 +90,23 @@
8090
exit(2);
8191
}
8292

83-
// Not enough arguments
84-
if ($argc < 3) {
93+
$basePath = $argv[1];
94+
$files = ($argc < 3)
95+
? findTemplateFilesInTemplatePath($basePath)
96+
: array_slice($argv, 2);
97+
98+
// No files provided
99+
if (empty($files)) {
85100
fwrite(STDERR, 'No files specified.' . PHP_EOL . PHP_EOL);
86101
fwrite(STDERR, $help . PHP_EOL);
87102
exit(2);
88103
}
89104

90-
$basePath = $argv[1];
91-
$files = array_slice($argv, 2);
92-
$map = [];
105+
$map = [];
93106
$realPath = realpath($basePath);
94107

95108
$entries = array_map(function ($file) use ($basePath, $realPath) {
96-
$file = str_replace('\\', '/', $file);
109+
$file = str_replace('\\', '/', $file);
97110

98111
$template = (0 === strpos($file, $realPath))
99112
? substr($file, strlen($realPath))
@@ -118,3 +131,24 @@
118131
. '];';
119132

120133
exit(0);
134+
135+
function findTemplateFilesInTemplatePath($templatePath)
136+
{
137+
$rdi = new RecursiveDirectoryIterator($templatePath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
138+
$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::LEAVES_ONLY);
139+
140+
$files = [];
141+
foreach ($rii as $file) {
142+
if (! $file instanceof SplFileInfo) {
143+
continue;
144+
}
145+
146+
if (! preg_match('#^phtml$#i', $file->getExtension())) {
147+
continue;
148+
}
149+
150+
$files[] = $file->getPathname();
151+
}
152+
153+
return $files;
154+
}

0 commit comments

Comments
 (0)