PageRenderTime 66ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/alexandretaz/maniac_divers
PHP | 240 lines | 177 code | 27 blank | 36 comment | 30 complexity | ac5491032509f74bc24413ad6b7c86ad MD5 | raw file
Possible License(s): BSD-3-Clause
  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-2012 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. * --output|-o [ <string> ] Where to write map file; if not provided,
  20. * assumes "template_map.php" in library directory
  21. * --append|-a Append to map file if it exists
  22. * --overwrite|-w Whether or not to overwrite existing map file
  23. */
  24. $zfLibraryPath = getenv('LIB_PATH') ? getenv('LIB_PATH') : __DIR__ . '/../library';
  25. if (is_dir($zfLibraryPath)) {
  26. // Try to load StandardAutoloader from library
  27. if (false === include($zfLibraryPath . '/Zend/Loader/StandardAutoloader.php')) {
  28. echo 'Unable to locate autoloader via library; aborting' . PHP_EOL;
  29. exit(2);
  30. }
  31. } else {
  32. // Try to load StandardAutoloader from include_path
  33. if (false === include('Zend/Loader/StandardAutoloader.php')) {
  34. echo 'Unable to locate autoloader via include_path; aborting' . PHP_EOL;
  35. exit(2);
  36. }
  37. }
  38. $libraryPath = getcwd();
  39. $viewPath = getcwd() . '/view';
  40. // Setup autoloading
  41. $loader = new StandardAutoloader(array('autoregister_zf' => true));
  42. $loader->register();
  43. $rules = array(
  44. 'help|h' => 'Get usage message',
  45. 'library|l-s' => 'Library to parse; if none provided, assumes current directory',
  46. 'view|v-s' => 'View path to parse; if none provided, assumes view as template directory',
  47. 'output|o-s' => 'Where to write map file; if not provided, assumes "template_map.php" in library directory',
  48. 'append|a' => 'Append to map file if it exists',
  49. 'overwrite|w' => 'Whether or not to overwrite existing map file',
  50. );
  51. try {
  52. $opts = new Console\Getopt($rules);
  53. $opts->parse();
  54. } catch (Console\Exception\RuntimeException $e) {
  55. echo $e->getUsageMessage();
  56. exit(2);
  57. }
  58. if ($opts->getOption('h')) {
  59. echo $opts->getUsageMessage();
  60. exit(0);
  61. }
  62. $relativePathForMap = '';
  63. if (isset($opts->l)) {
  64. if (!is_dir($opts->l)) {
  65. echo 'Invalid library directory provided' . PHP_EOL
  66. . PHP_EOL;
  67. echo $opts->getUsageMessage();
  68. exit(2);
  69. }
  70. $libraryPath = $opts->l;
  71. }
  72. $libraryPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($libraryPath));
  73. if (isset($opts->v)) {
  74. if (!is_dir($opts->v)) {
  75. echo 'Invalid view template directory provided' . PHP_EOL
  76. . PHP_EOL;
  77. echo $opts->getUsageMessage();
  78. exit(2);
  79. }
  80. $viewPath = $opts->v;
  81. }
  82. if (!is_dir($viewPath)) {
  83. printf('Invalid view path provided (%s)', $viewPath);
  84. echo PHP_EOL . PHP_EOL;
  85. echo $opts->getUsageMessage();
  86. exit(2);
  87. }
  88. $viewPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath($viewPath));
  89. $usingStdout = false;
  90. $appending = $opts->getOption('a');
  91. $output = $libraryPath . '/template_map.php';
  92. if (isset($opts->o)) {
  93. $output = $opts->o;
  94. if ('-' == $output) {
  95. $output = STDOUT;
  96. $usingStdout = true;
  97. } elseif (is_dir($output)) {
  98. echo 'Invalid output file provided' . PHP_EOL
  99. . PHP_EOL;
  100. echo $opts->getUsageMessage();
  101. exit(2);
  102. } elseif (!is_writeable(dirname($output))) {
  103. echo "Cannot write to '$output'; aborting." . PHP_EOL
  104. . PHP_EOL
  105. . $opts->getUsageMessage();
  106. exit(2);
  107. } elseif (file_exists($output) && !$opts->getOption('w') && !$appending) {
  108. echo "Template map file already exists at '$output'," . PHP_EOL
  109. . "but 'overwrite' or 'appending' flag was not specified; aborting." . PHP_EOL
  110. . PHP_EOL
  111. . $opts->getUsageMessage();
  112. exit(2);
  113. } else {
  114. // We need to add the $libraryPath into the relative path that is created in the template map file.
  115. $mapPath = str_replace(DIRECTORY_SEPARATOR, '/', realpath(dirname($output)));
  116. // Simple case: $libraryPathCompare is in $mapPathCompare
  117. if (strpos($libraryPath, $mapPath) === 0) {
  118. $relativePathForMap = substr($libraryPath, strlen($mapPath) + 1) . '/';
  119. } else {
  120. $libraryPathParts = explode('/', $libraryPath);
  121. $mapPathParts = explode('/', $mapPath);
  122. // Find the common part
  123. $count = count($mapPathParts);
  124. for ($i = 0; $i < $count; $i++) {
  125. if (!isset($libraryPathParts[$i]) || $libraryPathParts[$i] != $mapPathParts[$i]) {
  126. // Common part end
  127. break;
  128. }
  129. }
  130. // Add parent dirs for the subdirs of map
  131. $relativePathForMap = str_repeat('../', $count - $i);
  132. // Add library subdirs
  133. $count = count($libraryPathParts);
  134. for (; $i < $count; $i++) {
  135. $relativePathForMap .= $libraryPathParts[$i] . '/';
  136. }
  137. }
  138. }
  139. }
  140. if (!$usingStdout) {
  141. if ($appending) {
  142. echo "Appending to template file map '$output' for library in '$libraryPath'..." . PHP_EOL;
  143. } else {
  144. echo "Creating template file map for library in '$libraryPath'..." . PHP_EOL;
  145. }
  146. }
  147. $dirOrIterator = new RecursiveDirectoryIterator($viewPath, RecursiveDirectoryIterator::FOLLOW_SYMLINKS);
  148. $l = new RecursiveIteratorIterator($dirOrIterator);
  149. // Iterate over each element in the path, and create a map of
  150. // template name => filename, where the filename is relative to the view path
  151. $map = new stdClass;
  152. foreach ($l as $file) {
  153. if (!$file->isFile()) {
  154. continue;
  155. }
  156. $filename = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $file->getFilename());
  157. // Add in relative path to library
  158. $filename = $relativePathForMap . $filename;
  159. $baseName = $file->getBasename('.' . $file->getExtension());
  160. $mapName = str_replace($libraryPath . '/', '', str_replace(DIRECTORY_SEPARATOR, '/', $file->getPath()) . '/' . $baseName);
  161. $map->{$mapName} = $filename;
  162. }
  163. if ($appending) {
  164. $content = var_export((array) $map, true) . ';';
  165. // Prefix with __DIR__; modify the generated content
  166. $content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
  167. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  168. $content = str_replace("\\'", "'", $content);
  169. // Convert to an array and remove the first "array("
  170. $content = explode(PHP_EOL, $content);
  171. array_shift($content);
  172. // Load existing map file and remove the closing "bracket ");" from it
  173. $existing = file($output, FILE_IGNORE_NEW_LINES);
  174. array_pop($existing);
  175. // Merge
  176. $content = implode(PHP_EOL, array_merge($existing, $content));
  177. } else {
  178. // Create a file with the map.
  179. // Stupid syntax highlighters make separating < from PHP declaration necessary
  180. $content = '<' . "?php\n"
  181. . "// Generated by ZF2's ./bin/templatemap_generator.php\n"
  182. . 'return ' . var_export((array) $map, true) . ';';
  183. // Prefix with __DIR__; modify the generated content
  184. $content = preg_replace("#(=> ')#", "=> __DIR__ . '/", $content);
  185. // Fix \' strings from injected DIRECTORY_SEPARATOR usage in iterator_apply op
  186. $content = str_replace("\\'", "'", $content);
  187. }
  188. // Remove unnecessary double-backslashes
  189. $content = str_replace('\\\\', '\\', $content);
  190. // Exchange "array (" width "array("
  191. $content = str_replace('array (', 'array(', $content);
  192. // Align "=>" operators to match coding standard
  193. preg_match_all('(\n\s+([^=]+)=>)', $content, $matches, PREG_SET_ORDER);
  194. $maxWidth = 0;
  195. foreach ($matches as $match) {
  196. $maxWidth = max($maxWidth, strlen($match[1]));
  197. }
  198. $content = preg_replace('(\n\s+([^=]+)=>)e', "'\n \\1' . str_repeat(' ', " . $maxWidth . " - strlen('\\1')) . '=>'", $content);
  199. // Write the contents to disk
  200. file_put_contents($output, $content);
  201. if (!$usingStdout) {
  202. echo "Wrote templatemap file to '" . realpath($output) . "'" . PHP_EOL;
  203. }