|
11 | 11 |
|
12 | 12 | Usage:
|
13 | 13 |
|
14 |
| -templatemap_generator.php [-h|--help] templatepath files... |
| 14 | +templatemap_generator.php [-h|--help] templatepath <files...> |
15 | 15 |
|
16 | 16 | --help|-h Print this usage message.
|
17 | 17 | templatepath Path to templates relative to current working
|
18 | 18 | path; used to identify what to strip from
|
19 | 19 | 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 |
21 | 21 | map, relative to the current working path.
|
22 | 22 |
|
23 | 23 | The script assumes that paths included in the template map are relative
|
|
30 | 30 | need to edit the file after generation to ensure it contains valid
|
31 | 31 | PHP).
|
32 | 32 |
|
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: |
34 | 39 |
|
35 | 40 | For any shell, you can pipe the results of `find`:
|
36 | 41 |
|
|
48 | 53 |
|
49 | 54 | Examples:
|
50 | 55 |
|
| 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 | +
|
51 | 61 | # Create a template_map.config.php file in the Application module's
|
52 | 62 | # 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: |
54 | 64 | $ 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 |
56 | 66 |
|
57 | 67 | # OR using find:
|
58 | 68 | $ ../../../vendor/bin/templatemap_generator.php \
|
59 | 69 | > ../view \
|
60 |
| - > $(find ../view -name '*.phtml') > template_map.config.php |
| 70 | + > $(find ../view -name '*.html.php') > template_map.config.php |
61 | 71 | EOH;
|
62 | 72 |
|
63 | 73 | // Called without arguments
|
|
80 | 90 | exit(2);
|
81 | 91 | }
|
82 | 92 |
|
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)) { |
85 | 100 | fwrite(STDERR, 'No files specified.' . PHP_EOL . PHP_EOL);
|
86 | 101 | fwrite(STDERR, $help . PHP_EOL);
|
87 | 102 | exit(2);
|
88 | 103 | }
|
89 | 104 |
|
90 |
| -$basePath = $argv[1]; |
91 |
| -$files = array_slice($argv, 2); |
92 |
| -$map = []; |
| 105 | +$map = []; |
93 | 106 | $realPath = realpath($basePath);
|
94 | 107 |
|
95 | 108 | $entries = array_map(function ($file) use ($basePath, $realPath) {
|
96 |
| - $file = str_replace('\\', '/', $file); |
| 109 | + $file = str_replace('\\', '/', $file); |
97 | 110 |
|
98 | 111 | $template = (0 === strpos($file, $realPath))
|
99 | 112 | ? substr($file, strlen($realPath))
|
|
118 | 131 | . '];';
|
119 | 132 |
|
120 | 133 | 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