TemplatePathsCacheWarmer.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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\Component\HttpKernel\CacheWarmer\CacheWarmer;
  12. use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator;
  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. /**
  23. * Constructor.
  24. *
  25. * @param TemplateFinderInterface $finder A template finder
  26. * @param TemplateLocator $locator The template locator
  27. */
  28. public function __construct(TemplateFinderInterface $finder, TemplateLocator $locator)
  29. {
  30. $this->finder = $finder;
  31. $this->locator = $locator;
  32. }
  33. /**
  34. * Warms up the cache.
  35. *
  36. * @param string $cacheDir The cache directory
  37. */
  38. public function warmUp($cacheDir)
  39. {
  40. $templates = array();
  41. foreach ($this->finder->findAllTemplates() as $template) {
  42. $templates[$template->getSignature()] = $this->locator->locate($template);
  43. }
  44. $this->writeCacheFile($cacheDir.'/templates.php', sprintf('<?php return %s;', var_export($templates, true)));
  45. }
  46. /**
  47. * Checks whether this warmer is optional or not.
  48. *
  49. * @return Boolean always false
  50. */
  51. public function isOptional()
  52. {
  53. return false;
  54. }
  55. }