PageRenderTime 49ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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