docker dev update
[openemr.git] / vendor / zendframework / zend-view / bin / templatemap_generator.php
blobcf712250ace3f1166cfe6e818d82c89417738331
1 #!/usr/bin/env php
2 <?php
3 /**
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
7 */
9 $help = <<< EOH
10 Generate template maps.
12 Usage:
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
31 PHP).
33 We recommend you then include the generated file within your module
34 configuration:
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
43 following constructs:
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):
52 ../view/**/*.phtml
54 Examples:
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
67 # OR using find:
68 $ ../../../vendor/bin/templatemap_generator.php \
69 > ../view \
70 > $(find ../view -name '*.html.php') > template_map.config.php
71 EOH;
73 // Called without arguments
74 if ($argc < 2) {
75 fwrite(STDERR, 'No arguments provided.' . PHP_EOL . PHP_EOL);
76 fwrite(STDERR, $help . PHP_EOL);
77 exit(2);
80 // Requested help
81 if (in_array($argv[1], ['-h', '--help'], true)) {
82 echo $help, PHP_EOL;
83 exit(0);
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);
90 exit(2);
93 $basePath = $argv[1];
94 $files = ($argc < 3)
95 ? findTemplateFilesInTemplatePath($basePath)
96 : array_slice($argv, 2);
98 // No files provided
99 if (empty($files)) {
100 fwrite(STDERR, 'No files specified.' . PHP_EOL . PHP_EOL);
101 fwrite(STDERR, $help . PHP_EOL);
102 exit(2);
105 $map = [];
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))
113 : $file;
115 $template = (0 === strpos($template, $basePath))
116 ? substr($template, strlen($basePath))
117 : $template;
119 $template = preg_match('#(?P<template>.*?)\.[a-z0-9]+$#i', $template, $matches)
120 ? $matches['template']
121 : $template;
123 $template = preg_replace('#^\.*/#', '', $template);
125 return sprintf(" '%s' => __DIR__ . '/%s',", $template, $file);
126 }, $files);
128 echo '<' . "?php\nreturn [\n"
129 . implode("\n", $entries) . "\n"
130 . '];';
132 exit(0);
134 function findTemplateFilesInTemplatePath($templatePath)
136 $rdi = new RecursiveDirectoryIterator(
137 $templatePath,
138 RecursiveDirectoryIterator::FOLLOW_SYMLINKS | RecursiveDirectoryIterator::SKIP_DOTS
140 $rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::LEAVES_ONLY);
142 $files = [];
143 foreach ($rii as $file) {
144 if (strtolower($file->getExtension()) != 'phtml') {
145 continue;
148 $files[] = $file->getPathname();
151 return $files;