|
| 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 | + */ |
| 8 | + |
| 9 | +$help = <<< EOH |
| 10 | +Generate template maps. |
| 11 | +
|
| 12 | +Usage: |
| 13 | +
|
| 14 | +templatemap_generator.php [-h|--help] templatepath <files...> |
| 15 | +
|
| 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. |
| 22 | +
|
| 23 | +The script assumes that paths included in the template map are relative |
| 24 | +to the current working directory. |
| 25 | +
|
| 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). |
| 32 | +
|
| 33 | +We recommend you then include the generated file within your module |
| 34 | +configuration: |
| 35 | +
|
| 36 | + 'template_map' => include __DIR__ . '/template_map.config.php', |
| 37 | +
|
| 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. |
| 40 | +
|
| 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: |
| 44 | +
|
| 45 | +For any shell, you can pipe the results of `find`: |
| 46 | +
|
| 47 | + $(find ../view -name '*.phtml') |
| 48 | +
|
| 49 | +For zsh, or bash where you have enabled globstar (`shopt -s globstar` in |
| 50 | +either your bash profile or from within your terminal): |
| 51 | +
|
| 52 | + ../view/**/*.phtml |
| 53 | +
|
| 54 | +Examples: |
| 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 | +
|
| 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 |
| 66 | +
|
| 67 | + # OR using find: |
| 68 | + $ ../../../vendor/bin/templatemap_generator.php \ |
| 69 | + > ../view \ |
| 70 | + > $(find ../view -name '*.html.php') > template_map.config.php |
| 71 | +EOH; |
| 72 | + |
| 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); |
| 78 | +} |
| 79 | + |
| 80 | +// Requested help |
| 81 | +if (in_array($argv[1], ['-h', '--help'], true)) { |
| 82 | + echo $help, PHP_EOL; |
| 83 | + exit(0); |
| 84 | +} |
| 85 | + |
| 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); |
| 91 | +} |
| 92 | + |
| 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)) { |
| 100 | + fwrite(STDERR, 'No files specified.' . PHP_EOL . PHP_EOL); |
| 101 | + fwrite(STDERR, $help . PHP_EOL); |
| 102 | + exit(2); |
| 103 | +} |
| 104 | + |
| 105 | +$map = []; |
| 106 | +$realPath = realpath($basePath); |
| 107 | + |
| 108 | +$entries = array_map(function ($file) use ($basePath, $realPath) { |
| 109 | + $file = str_replace('\\', '/', $file); |
| 110 | + |
| 111 | + $template = (0 === strpos($file, $realPath)) |
| 112 | + ? substr($file, strlen($realPath)) |
| 113 | + : $file; |
| 114 | + |
| 115 | + $template = (0 === strpos($template, $basePath)) |
| 116 | + ? substr($template, strlen($basePath)) |
| 117 | + : $template; |
| 118 | + |
| 119 | + $template = preg_match('#(?P<template>.*?)\.[a-z0-9]+$#i', $template, $matches) |
| 120 | + ? $matches['template'] |
| 121 | + : $template; |
| 122 | + |
| 123 | + $template = preg_replace('#^\.*/#', '', $template); |
| 124 | + $file = sprintf('__DIR__ . \'/%s\'', $file); |
| 125 | + |
| 126 | + return sprintf(" '%s' => %s,\n", $template, $file); |
| 127 | +}, $files); |
| 128 | + |
| 129 | +echo '<' . "?php\nreturn [\n" |
| 130 | + . implode('', $entries) |
| 131 | + . '];'; |
| 132 | + |
| 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