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

/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/TemplatePathsCacheWarmer.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 113 lines | 71 code | 13 blank | 29 comment | 4 complexity | 76f2c494ac81dfbd5e1742506ae2c9c3 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\CacheWarmer;
  11. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
  12. use Symfony\Component\HttpKernel\KernelInterface;
  13. use Symfony\Component\Finder\Finder;
  14. /**
  15. * Computes the association between template names and their paths on the disk.
  16. *
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. */
  19. class TemplatePathsCacheWarmer extends CacheWarmer
  20. {
  21. protected $kernel;
  22. protected $rootDir;
  23. /**
  24. * Constructor.
  25. *
  26. * @param KernelInterface $kernel A KernelInterface instance
  27. * @param string $rootDir The directory where global templates can be stored
  28. */
  29. public function __construct(KernelInterface $kernel, $rootDir)
  30. {
  31. $this->kernel = $kernel;
  32. $this->rootDir = $rootDir;
  33. }
  34. /**
  35. * Warms up the cache.
  36. *
  37. * @param string $cacheDir The cache directory
  38. */
  39. public function warmUp($cacheDir)
  40. {
  41. $templates = $this->computeTemplatePaths();
  42. $this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
  43. }
  44. /**
  45. * Checks whether this warmer is optional or not.
  46. *
  47. * @return Boolean always false
  48. */
  49. public function isOptional()
  50. {
  51. return false;
  52. }
  53. protected function computeTemplatePaths()
  54. {
  55. $prefix = '/Resources/views';
  56. $templates = array();
  57. foreach ($this->kernel->getBundles() as $name => $bundle) {
  58. if (!is_dir($dir = $bundle->getNormalizedPath().$prefix)) {
  59. continue;
  60. }
  61. $finder = new Finder();
  62. foreach ($finder->files()->followLinks()->in($dir) as $file) {
  63. if (false !== $template = $this->parseTemplateName($file, $prefix.'/', $bundle->getName())) {
  64. $resource = '@'.$template['bundle'].'/Resources/views/'.$template['controller'].'/'.$template['name'].'.'.$template['format'].'.'.$template['engine'];
  65. $templates[md5(serialize($template))] = $this->kernel->locateResource($resource, $this->rootDir);
  66. }
  67. }
  68. }
  69. if (is_dir($this->rootDir)) {
  70. $finder = new Finder();
  71. foreach ($finder->files()->followLinks()->in($this->rootDir) as $file) {
  72. if (false !== $template = $this->parseTemplateName($file, strtr($this->rootDir, '\\', '/').'/')) {
  73. $templates[md5(serialize($template))] = (string) $file;
  74. }
  75. }
  76. }
  77. return $templates;
  78. }
  79. protected function parseTemplateName($file, $prefix, $bundle = '')
  80. {
  81. $path = strtr($file->getPathname(), '\\', '/');
  82. list(, $tmp) = explode($prefix, $path, 2);
  83. $parts = explode('/', strtr($tmp, '\\', '/'));
  84. $elements = explode('.', array_pop($parts));
  85. if (3 !== count($elements)) {
  86. return false;
  87. }
  88. return array(
  89. 'bundle' => $bundle,
  90. 'controller' => implode('/', $parts),
  91. 'name' => $elements[0],
  92. 'format' => $elements[1],
  93. 'engine' => $elements[2],
  94. );
  95. }
  96. }