templatemap_generator.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. $help = <<< EOH
  9. Generate template maps.
  10. Usage:
  11. templatemap_generator.php [-h|--help] templatepath <files...>
  12. --help|-h Print this usage message.
  13. templatepath Path to templates relative to current working
  14. path; used to identify what to strip from
  15. template names. Must be a directory.
  16. <files...> List of files to include in the template
  17. map, relative to the current working path.
  18. The script assumes that paths included in the template map are relative
  19. to the current working directory.
  20. The script will output a PHP script that will return the template map
  21. on successful completion. You may save this to a file using standard
  22. piping operators; use ">" to write to/ovewrite a file, ">>" to append
  23. to a file (which may have unexpected and/or intended results; you will
  24. need to edit the file after generation to ensure it contains valid
  25. PHP).
  26. We recommend you then include the generated file within your module
  27. configuration:
  28. 'template_map' => include __DIR__ . '/template_map.config.php',
  29. If only the templatepath argument is provided, the script will look for
  30. all .phtml files under that directory, creating a map for you.
  31. If you want to specify a specific list of files -- for instance, if you
  32. are using an extension other than .phtml -- we recommend one of the
  33. following constructs:
  34. For any shell, you can pipe the results of `find`:
  35. $(find ../view -name '*.phtml')
  36. For zsh, or bash where you have enabled globstar (`shopt -s globstar` in
  37. either your bash profile or from within your terminal):
  38. ../view/**/*.phtml
  39. Examples:
  40. # Using only a templatepath argument, which will match any .phtml
  41. # files found under the provided path:
  42. $ cd module/Application/config/
  43. $ ../../../vendor/bin/templatemap_generator.php ../view > template_map.config.php
  44. # Create a template_map.config.php file in the Application module's
  45. # config directory, relative to the view directory, and only containing
  46. # .html.php files; overwrite any existing files:
  47. $ cd module/Application/config/
  48. $ ../../../vendor/bin/templatemap_generator.php ../view ../view/**/*.html.php > template_map.config.php
  49. # OR using find:
  50. $ ../../../vendor/bin/templatemap_generator.php \
  51. > ../view \
  52. > $(find ../view -name '*.html.php') > template_map.config.php
  53. EOH;
  54. // Called without arguments
  55. if ($argc < 2) {
  56. fwrite(STDERR, 'No arguments provided.' . PHP_EOL . PHP_EOL);
  57. fwrite(STDERR, $help . PHP_EOL);
  58. exit(2);
  59. }
  60. // Requested help
  61. if (in_array($argv[1], ['-h', '--help'], true)) {
  62. echo $help, PHP_EOL;
  63. exit(0);
  64. }
  65. // Invalid path argument
  66. if (! is_dir($argv[1])) {
  67. fwrite(STDERR, 'templatepath argument is not a directory.' . PHP_EOL . PHP_EOL);
  68. fwrite(STDERR, $help . PHP_EOL);
  69. exit(2);
  70. }
  71. $basePath = $argv[1];
  72. $files = ($argc < 3)
  73. ? findTemplateFilesInTemplatePath($basePath)
  74. : array_slice($argv, 2);
  75. // No files provided
  76. if (empty($files)) {
  77. fwrite(STDERR, 'No files specified.' . PHP_EOL . PHP_EOL);
  78. fwrite(STDERR, $help . PHP_EOL);
  79. exit(2);
  80. }
  81. $map = [];
  82. $realPath = realpath($basePath);
  83. $entries = array_map(function ($file) use ($basePath, $realPath) {
  84. $file = str_replace('\\', '/', $file);
  85. $template = (0 === strpos($file, $realPath))
  86. ? substr($file, strlen($realPath))
  87. : $file;
  88. $template = (0 === strpos($template, $basePath))
  89. ? substr($template, strlen($basePath))
  90. : $template;
  91. $template = preg_match('#(?P<template>.*?)\.[a-z0-9]+$#i', $template, $matches)
  92. ? $matches['template']
  93. : $template;
  94. $template = preg_replace('#^\.*/#', '', $template);
  95. return sprintf(" '%s' => __DIR__ . '/%s',", $template, $file);
  96. }, $files);
  97. echo '<' . "?php\nreturn [\n"
  98. . implode("\n", $entries) . "\n"
  99. . '];';
  100. exit(0);
  101. function findTemplateFilesInTemplatePath($templatePath)
  102. {
  103. $rdi = new RecursiveDirectoryIterator(
  104. $templatePath,
  105. RecursiveDirectoryIterator::FOLLOW_SYMLINKS | RecursiveDirectoryIterator::SKIP_DOTS
  106. );
  107. $rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::LEAVES_ONLY);
  108. $files = [];
  109. foreach ($rii as $file) {
  110. if (strtolower($file->getExtension()) != 'phtml') {
  111. continue;
  112. }
  113. $files[] = $file->getPathname();
  114. }
  115. return $files;
  116. }