PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/zendframework/zendframework/bin/templatemap_generator.php

https://bitbucket.org/openemr/openemr
PHP | 260 lines | 185 code | 28 blank | 47 comment | 38 complexity | 426b0bf2e657bf30c15b0649b09cf138 MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, LGPL-3.0, BSD-3-Clause, Unlicense, MPL-2.0, GPL-3.0, LGPL-2.1
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * Zend Framework (http://framework.zend.com/)
  5. *
  6. * @link http://github.com/zendframework/zf2 for the canonical source repository
  7. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  8. * @license http://framework.zend.com/license/new-bsd New BSD License
  9. */
  10. use Zend\Console;
  11. use Zend\Loader\StandardAutoloader;
  12. /**
  13. * Generate template maps.
  14. *
  15. * Usage:
  16. * --help|-h Get usage message
  17. * --library|-l [ <string> ] Library to parse; if none provided, assumes
  18. * current directory
  19. * --view|-v [ <string> ] View path to parse; if none provided, assumes
  20. * view as template directory
  21. * --extensions|-e [ <string> ] List of accepted file extensions (regex alternation
  22. * without parenthesis); default: *
  23. * --output|-o [ <string> ] Where to write map file; if not provided,
  24. * assumes "template_map.php" in library directory
  25. * --append|-a Append to map file if it exists
  26. * --overwrite|-w Whether or not to overwrite existing map file
  27. */
  28. $zfLibraryPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
  29. if (is_dir($zfLibraryPath)) {
  30. // Try to load StandardAutoloader from library
  31. if (false === include($zfLibraryPath . '/Zend/Loader/StandardAutoloader.php')) {
  32. echo 'Unable to locate autoloader via library; aborting' . PHP_EOL;
  33. exit(2);
  34. }
  35. } else {
  36. // Try to load StandardAutoloader from include_path
  37. if (false === include('Zend/Loader/StandardAutoloader.php')) {
  38. echo 'Unable to locate autoloader via include_path; aborting' . PHP_EOL;
  39. exit(2);
  40. }
  41. }
  42. $libraryPath = getcwd();
  43. $viewPath = getcwd() . '/view';
  44. // Setup autoloading
  45. $loader = new StandardAutoloader(array('autoregister_zf' => true));
  46. $loader->register();
  47. $rules = array(
  48. 'help|h' => 'Get usage message',
  49. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  50. 'view|v-s' => 'View path to parse; if none provided, assumes view as template directory',
  51. 'extensions|e-s' => 'List of accepted file extensions (regex alternation: *html, phtml|tpl); default: *',
  52. 'output|o-s' => 'Where to write map file; if not provided, assumes "template_map.php" in library directory',
  53. 'append|a' => 'Append to map file if it exists',
  54. 'overwrite|w' => 'Whether or not to overwrite existing map file',
  55. );
  56. try {
  57. $opts = new Console\Getopt($rules);
  58. $opts->parse();
  59. } catch (Console\Exception\RuntimeException $e) {
  60. echo $e->getUsageMessage();
  61. exit(2);
  62. }
  63. if ($opts->getOption('h')) {
  64. echo $opts->getUsageMessage();
  65. exit(0);
  66. }
  67. $fileExtensions = '*';
  68. if (isset($opts->e) && $opts->e != '*') {
  69. if (!preg_match('/^(\*?[[:alnum:]]\*?+\|?)+$/', $opts->e)) {
  70. echo 'Invalid extensions list specified. Expecting wildcard or alternation: *, *html, phtml|tpl' . PHP_EOL
  71. . PHP_EOL;
  72. echo $opts->getUsageMessage();
  73. exit(2);
  74. }
  75. $fileExtensions = '(' . $opts->e . ')';
  76. }
  77. $fileExtensions = str_replace('*', '.*', $fileExtensions);
  78. $relativePathForMap = '';
  79. if (isset($opts->l)) {
  80. if (!is_dir($opts->l)) {
  81. echo 'Invalid library directory provided' . PHP_EOL
  82. . PHP_EOL;
  83. echo $opts->getUsageMessage();
  84. exit(2);
  85. }
  86. $libraryPath = $opts->l;
  87. }
  88. $libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath));
  89. if (isset($opts->v)) {
  90. if (!is_dir($opts->v)) {
  91. echo 'Invalid view template directory provided' . PHP_EOL
  92. . PHP_EOL;
  93. echo $opts->getUsageMessage();
  94. exit(2);
  95. }
  96. $viewPath = $opts->v;
  97. }
  98. if (!is_dir($viewPath)) {
  99. printf('Invalid view path provided (%s)', $viewPath);
  100. echo PHP_EOL . PHP_EOL;
  101. echo $opts->getUsageMessage();
  102. exit(2);
  103. }
  104. $viewPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath));
  105. $usingStdout = false;
  106. $appending = $opts->getOption('a');
  107. $output = $libraryPath . '/template_map.php';
  108. if (isset($opts->o)) {
  109. $output = $opts->o;
  110. if ('-' == $output) {
  111. $output = STDOUT;
  112. $usingStdout = true;
  113. } elseif (is_dir($output)) {
  114. echo 'Invalid output file provided' . PHP_EOL
  115. . PHP_EOL;
  116. echo $opts->getUsageMessage();
  117. exit(2);
  118. } elseif (!is_writeable(dirname($output))) {
  119. echo "Cannot write to '$output'; aborting." . PHP_EOL
  120. . PHP_EOL
  121. . $opts->getUsageMessage();
  122. exit(2);
  123. } elseif (file_exists($output) && !$opts->getOption('w') && !$appending) {
  124. echo "Template map file already exists at '$output'," . PHP_EOL
  125. . "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL
  126. . PHP_EOL
  127. . $opts->getUsageMessage();
  128. exit(2);
  129. } else {
  130. // We need to add the $libraryPath into the relative path that is created in the template map file.
  131. $mapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output)));
  132. // Simple case: $libraryPathCompare is in $mapPathCompare
  133. if (strpos($libraryPath, $mapPath) === 0) {
  134. $relativePathForMap = substr($libraryPath, strlen($mapPath) + 1) . '/';
  135. } else {
  136. $libraryPathParts = explode('/', $libraryPath);
  137. $mapPathParts = explode('/', $mapPath);
  138. // Find the common part
  139. $count = count($mapPathParts);
  140. for ($i = 0; $i < $count; $i++) {
  141. if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $mapPathParts[$i]) {
  142. // Common part end
  143. break;
  144. }
  145. }
  146. // Add parent dirs for the subdirs of map
  147. $relativePathForMap = str_repeat('../', $count - $i);
  148. // Add library subdirs
  149. $count = count($libraryPathParts);
  150. for (; $i < $count; $i++) {
  151. $relativePathForMap .= $libraryPathParts[$i] . '/';
  152. }
  153. }
  154. }
  155. }
  156. if (!$usingStdout) {
  157. if ($appending) {
  158. echo "Appending to template file map '$output' for library in '$libraryPath'..." . PHP_EOL;
  159. } else {
  160. echo "Creating template file map for library in '$libraryPath'..." . PHP_EOL;
  161. }
  162. }
  163. $dirOrIterator = new RecursiveDirectoryIterator($viewPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
  164. $l = new RecursiveIteratorIterator($dirOrIterator);
  165. // Iterate over each element in the path, and create a map of
  166. // template name => filename, where the filename is relative to the view path
  167. $map = new stdClass;
  168. foreach ($l as $file) {
  169. /* @var $file SplFileInfo */
  170. if (!$file->isFile() || !preg_match('/^' . $fileExtensions . '$/', $file->getExtension())) {
  171. continue;
  172. }
  173. $filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
  174. // Add in relative path to library
  175. $filename = $relativePathForMap . $filename;
  176. $baseName = $file->getBasename('.' . pathinfo($file->getFilename(), PATHINFO_EXTENSION));
  177. $mapName = str_replace(str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath)) . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $baseName);
  178. $map->{$mapName} = $filename;
  179. }
  180. // Create a file with the map.
  181. if ($appending && file_exists($output) && is_array(include $output)) {
  182. // Append mode and the output file already exists: retrieve its
  183. // content and merges with the new map
  184. // Remove the last line as it is the end of the array, and we want to
  185. // append our new templates
  186. $content = file($output, FILE_IGNORE_NEW_LINES);
  187. array_pop($content);
  188. $content = implode(PHP_EOL, $content) . PHP_EOL;
  189. } else {
  190. // Write mode or the file does not exists: create a new file
  191. // Stupid syntax highlighters make separating < from PHP declaration necessary
  192. $content = '<' . "?php" . PHP_EOL
  193. . '// Generated by ZF2\'s ./bin/templatemap_generator.php' . PHP_EOL
  194. . 'return array(' . PHP_EOL;
  195. }
  196. // Process the template map as a string before inserting it to the output file
  197. $mapExport = var_export((array) $map, true);
  198. // Prefix with __DIR__
  199. $mapExport = preg_replace("#(=> ')#", "=> __DIR__ . '/", $mapExport);
  200. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  201. $mapExport = str_replace("\\'", "'", $mapExport);
  202. // Remove unnecessary double-backslashes
  203. $mapExport = str_replace('\\\\', '\\', $mapExport);
  204. // Remove "array ("
  205. $mapExport = str_replace('array (', '', $mapExport);
  206. // Align "=>" operators to match coding standard
  207. preg_match_all('(\n\s+([^=]+)=>)', $mapExport, $matches, PREG_SET_ORDER);
  208. $maxWidth = 0;
  209. foreach ($matches as $match) {
  210. $maxWidth = max($maxWidth, strlen($match[1]));
  211. }
  212. $mapExport = preg_replace_callback('(\n\s+([^=]+)=>)', function ($matches) use ($maxWidth) {
  213. return PHP_EOL . ' ' . $matches[1] . str_repeat(' ', $maxWidth - strlen($matches[1])) . '=>';
  214. }, $mapExport);
  215. // Trim the content
  216. $mapExport = trim($mapExport, "\n");
  217. // Append the map to the file, close the array and write a new line
  218. $content .= $mapExport . ';' . PHP_EOL;
  219. // Write the contents to disk
  220. file_put_contents($output, $content);
  221. if (!$usingStdout) {
  222. echo "Wrote templatemap file to '" . realpath($output) . "'" . PHP_EOL;
  223. }