4 * @link http://github.com/zendframework/zend-view for the canonical source repository
5 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
6 * @license http://framework.zend.com/license/new-bsd New BSD License
10 Generate template maps
.
14 templatemap_generator
.php
[-h|
--help
] templatepath
<files
...>
16 --help|
-h
Print this usage message
.
17 templatepath Path to templates relative to current working
18 path
; used to identify what to strip from
19 template names
. Must be a directory
.
20 <files
...> List of files to
include in the template
21 map
, relative to the current working path
.
23 The script assumes that paths included in the template map are relative
24 to the current working directory
.
26 The script will output a PHP script that will
return the template map
27 on successful completion
. You may save this to a file using standard
28 piping operators
; use ">" to write to
/ovewrite a file
, ">>" to append
29 to a
file (which may have unexpected
and/or intended results
; you will
30 need to edit the file after generation to ensure it contains valid
33 We recommend you then
include the generated file within your module
36 'template_map' => include __DIR__
. '/template_map.config.php',
38 If only the templatepath argument is provided
, the script will look
for
39 all
.phtml files under that directory
, creating a map
for you
.
41 If you want to specify a specific
list of files
-- for instance
, if you
42 are using an extension other than
.phtml
-- we recommend one of the
45 For any shell
, you can pipe the results of `find`
:
47 $
(find
../view
-name
'*.phtml')
49 For zsh
, or bash where you have enabled
globstar (`shopt
-s globstar` in
50 either your bash profile
or from within your terminal
):
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
61 # Create a template_map.config.php file in the Application module's
62 # config directory, relative to the view directory, and only containing
63 # .html.php files; overwrite any existing files:
64 $ cd module
/Application
/config
/
65 $
../../../vendor
/bin
/templatemap_generator
.php
../view
../view
/**/*.html
.php
> template_map
.config
.php
68 $
../../../vendor
/bin
/templatemap_generator
.php \
70 > $
(find
../view
-name
'*.html.php') > template_map
.config
.php
73 // Called without arguments
75 fwrite(STDERR
, 'No arguments provided.' . PHP_EOL
. PHP_EOL
);
76 fwrite(STDERR
, $help . PHP_EOL
);
81 if (in_array($argv[1], ['-h', '--help'], true)) {
86 // Invalid path argument
87 if (! is_dir($argv[1])) {
88 fwrite(STDERR
, 'templatepath argument is not a directory.' . PHP_EOL
. PHP_EOL
);
89 fwrite(STDERR
, $help . PHP_EOL
);
95 ?
findTemplateFilesInTemplatePath($basePath)
96 : array_slice($argv, 2);
100 fwrite(STDERR
, 'No files specified.' . PHP_EOL
. PHP_EOL
);
101 fwrite(STDERR
, $help . PHP_EOL
);
106 $realPath = realpath($basePath);
108 $entries = array_map(function ($file) use ($basePath, $realPath) {
109 $file = str_replace('\\', '/', $file);
111 $template = (0 === strpos($file, $realPath))
112 ?
substr($file, strlen($realPath))
115 $template = (0 === strpos($template, $basePath))
116 ?
substr($template, strlen($basePath))
119 $template = preg_match('#(?P<template>.*?)\.[a-z0-9]+$#i', $template, $matches)
120 ?
$matches['template']
123 $template = preg_replace('#^\.*/#', '', $template);
125 return sprintf(" '%s' => __DIR__ . '/%s',", $template, $file);
128 echo '<' . "?php\nreturn [\n"
129 . implode("\n", $entries) . "\n"
134 function findTemplateFilesInTemplatePath($templatePath)
136 $rdi = new RecursiveDirectoryIterator(
138 RecursiveDirectoryIterator
::FOLLOW_SYMLINKS | RecursiveDirectoryIterator
::SKIP_DOTS
140 $rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator
::LEAVES_ONLY
);
143 foreach ($rii as $file) {
144 if (strtolower($file->getExtension()) != 'phtml') {
148 $files[] = $file->getPathname();