TemplatePathsCacheWarmer.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
  12. use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer;
  13. /**
  14. * Computes the association between template names and their paths on the disk.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class TemplatePathsCacheWarmer extends CacheWarmer
  19. {
  20. protected $finder;
  21. protected $locator;
  22. public function __construct(TemplateFinderInterface $finder, TemplateLocator $locator)
  23. {
  24. $this->finder = $finder;
  25. $this->locator = $locator;
  26. }
  27. /**
  28. * Warms up the cache.
  29. *
  30. * @param string $cacheDir The cache directory
  31. */
  32. public function warmUp($cacheDir)
  33. {
  34. $templates = array();
  35. foreach ($this->finder->findAllTemplates() as $template) {
  36. $templates[$template->getLogicalName()] = $this->locator->locate($template);
  37. }
  38. $this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
  39. }
  40. /**
  41. * Checks whether this warmer is optional or not.
  42. *
  43. * @return bool always true
  44. */
  45. public function isOptional()
  46. {
  47. return true;
  48. }
  49. }