CacheLoader.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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\Component\Templating\Loader;
  11. use Symfony\Component\Templating\Storage\Storage;
  12. use Symfony\Component\Templating\Storage\FileStorage;
  13. use Symfony\Component\Templating\TemplateReferenceInterface;
  14. /**
  15. * CacheLoader is a loader that caches other loaders responses
  16. * on the filesystem.
  17. *
  18. * This cache only caches on disk to allow PHP accelerators to cache the opcodes.
  19. * All other mechanism would imply the use of `eval()`.
  20. *
  21. * @author Fabien Potencier <fabien@symfony.com>
  22. */
  23. class CacheLoader extends Loader
  24. {
  25. protected $loader;
  26. protected $dir;
  27. /**
  28. * Constructor.
  29. *
  30. * @param LoaderInterface $loader A Loader instance
  31. * @param string $dir The directory where to store the cache files
  32. */
  33. public function __construct(LoaderInterface $loader, $dir)
  34. {
  35. $this->loader = $loader;
  36. $this->dir = $dir;
  37. }
  38. /**
  39. * Loads a template.
  40. *
  41. * @param TemplateReferenceInterface $template A template
  42. *
  43. * @return Storage|Boolean false if the template cannot be loaded, a Storage instance otherwise
  44. */
  45. public function load(TemplateReferenceInterface $template)
  46. {
  47. $key = md5($template->getLogicalName());
  48. $dir = $this->dir.DIRECTORY_SEPARATOR.substr($key, 0, 2);
  49. $file = substr($key, 2).'.tpl';
  50. $path = $dir.DIRECTORY_SEPARATOR.$file;
  51. if (is_file($path)) {
  52. if (null !== $this->debugger) {
  53. $this->debugger->log(sprintf('Fetching template "%s" from cache', $template->get('name')));
  54. }
  55. return new FileStorage($path);
  56. }
  57. if (false === $storage = $this->loader->load($template)) {
  58. return false;
  59. }
  60. $content = $storage->getContent();
  61. if (!is_dir($dir)) {
  62. mkdir($dir, 0777, true);
  63. }
  64. file_put_contents($path, $content);
  65. if (null !== $this->debugger) {
  66. $this->debugger->log(sprintf('Storing template "%s" in cache', $template->get('name')));
  67. }
  68. return new FileStorage($path);
  69. }
  70. /**
  71. * Returns true if the template is still fresh.
  72. *
  73. * @param TemplateReferenceInterface $template A template
  74. * @param integer $time The last modification time of the cached template (timestamp)
  75. *
  76. * @return Boolean
  77. */
  78. public function isFresh(TemplateReferenceInterface $template, $time)
  79. {
  80. return $this->loader->isFresh($template, $time);
  81. }
  82. }