build_bootstrap.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. * This file is part of Mustache.php.
  5. *
  6. * (c) 2012 Justin Hileman
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. /**
  12. * A shell script to create a single-file class cache of the entire Mustache
  13. * library:
  14. *
  15. * $ bin/build_bootstrap.php
  16. *
  17. * ... will create a `mustache.php` bootstrap file in the project directory,
  18. * containing all Mustache library classes. This file can then be included in
  19. * your project, rather than requiring the Mustache Autoloader.
  20. */
  21. $baseDir = realpath(dirname(__FILE__).'/..');
  22. require $baseDir.'/src/Mustache/Autoloader.php';
  23. Mustache_Autoloader::register();
  24. // delete the old file
  25. $file = $baseDir.'/mustache.php';
  26. if (file_exists($file)) {
  27. unlink($file);
  28. }
  29. // and load the new one
  30. SymfonyClassCollectionLoader::load(array(
  31. '\Mustache_Engine',
  32. '\Mustache_Compiler',
  33. '\Mustache_Context',
  34. '\Mustache_Exception',
  35. '\Mustache_Exception_InvalidArgumentException',
  36. '\Mustache_Exception_LogicException',
  37. '\Mustache_Exception_RuntimeException',
  38. '\Mustache_Exception_SyntaxException',
  39. '\Mustache_Exception_UnknownFilterException',
  40. '\Mustache_Exception_UnknownHelperException',
  41. '\Mustache_Exception_UnknownTemplateException',
  42. '\Mustache_HelperCollection',
  43. '\Mustache_LambdaHelper',
  44. '\Mustache_Loader',
  45. '\Mustache_Loader_ArrayLoader',
  46. '\Mustache_Loader_CascadingLoader',
  47. '\Mustache_Loader_FilesystemLoader',
  48. '\Mustache_Loader_InlineLoader',
  49. '\Mustache_Loader_MutableLoader',
  50. '\Mustache_Loader_StringLoader',
  51. '\Mustache_Logger',
  52. '\Mustache_Logger_AbstractLogger',
  53. '\Mustache_Logger_StreamLogger',
  54. '\Mustache_Parser',
  55. '\Mustache_Template',
  56. '\Mustache_Tokenizer',
  57. ), dirname($file), basename($file, '.php'));
  58. /**
  59. * SymfonyClassCollectionLoader.
  60. *
  61. * Based heavily on the Symfony ClassCollectionLoader component, with all
  62. * the unnecessary bits removed.
  63. *
  64. * @license http://www.opensource.org/licenses/MIT
  65. *
  66. * @author Fabien Potencier <fabien@symfony.com>
  67. */
  68. class SymfonyClassCollectionLoader
  69. {
  70. static private $loaded;
  71. /**
  72. * Loads a list of classes and caches them in one big file.
  73. *
  74. * @param array $classes An array of classes to load
  75. * @param string $cacheDir A cache directory
  76. * @param string $name The cache name prefix
  77. * @param string $extension File extension of the resulting file
  78. *
  79. * @throws InvalidArgumentException When class can't be loaded
  80. */
  81. static public function load(array $classes, $cacheDir, $name, $extension = '.php')
  82. {
  83. // each $name can only be loaded once per PHP process
  84. if (isset(self::$loaded[$name])) {
  85. return;
  86. }
  87. self::$loaded[$name] = true;
  88. $content = '';
  89. foreach ($classes as $class) {
  90. if (!class_exists($class) && !interface_exists($class) && (!function_exists('trait_exists') || !trait_exists($class))) {
  91. throw new InvalidArgumentException(sprintf('Unable to load class "%s"', $class));
  92. }
  93. $r = new ReflectionClass($class);
  94. $content .= preg_replace(array('/^\s*<\?php/', '/\?>\s*$/'), '', file_get_contents($r->getFileName()));
  95. }
  96. $cache = $cacheDir.'/'.$name.$extension;
  97. self::writeCacheFile($cache, self::stripComments('<?php '.$content));
  98. }
  99. /**
  100. * Writes a cache file.
  101. *
  102. * @param string $file Filename
  103. * @param string $content Temporary file content
  104. *
  105. * @throws RuntimeException when a cache file cannot be written
  106. */
  107. static private function writeCacheFile($file, $content)
  108. {
  109. $tmpFile = tempnam(dirname($file), basename($file));
  110. if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $file)) {
  111. chmod($file, 0666 & ~umask());
  112. return;
  113. }
  114. throw new RuntimeException(sprintf('Failed to write cache file "%s".', $file));
  115. }
  116. /**
  117. * Removes comments from a PHP source string.
  118. *
  119. * We don't use the PHP php_strip_whitespace() function
  120. * as we want the content to be readable and well-formatted.
  121. *
  122. * @param string $source A PHP string
  123. *
  124. * @return string The PHP string with the comments removed
  125. */
  126. static private function stripComments($source)
  127. {
  128. if (!function_exists('token_get_all')) {
  129. return $source;
  130. }
  131. $output = '';
  132. foreach (token_get_all($source) as $token) {
  133. if (is_string($token)) {
  134. $output .= $token;
  135. } elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  136. $output .= $token[1];
  137. }
  138. }
  139. // replace multiple new lines with a single newline
  140. $output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
  141. return $output;
  142. }
  143. }