src/Twig/Loader/DatabaseLoader.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Twig\Loader;
  3. use App\Entity\Template\Template;
  4. use Doctrine\ORM\EntityManagerInterface;
  5. use Twig_Error_Loader;
  6. use Twig_LoaderInterface;
  7. use Twig_Source;
  8. class DatabaseLoader implements Twig_LoaderInterface
  9. {
  10.     protected $repo;
  11.     public function __construct(EntityManagerInterface $em)
  12.     {
  13.         $this->repo $em->getRepository(Template::class);
  14.     }
  15.     public function getSourceContext($name)
  16.     {
  17.         if (false === $template $this->getTemplate($name)) {
  18.             throw new Twig_Error_Loader(sprintf('Template "%s" does not exist.'$name));
  19.         }
  20.         return new Twig_Source($template->getSource(), $name);
  21.     }
  22.     public function exists($name)
  23.     {
  24.         return (bool)$this->getTemplate($name);
  25.     }
  26.     public function getCacheKey($name)
  27.     {
  28.         return $name;
  29.     }
  30.     public function isFresh($name$time)
  31.     {
  32.         if (false === ($template $this->getTemplate($name))) {
  33.             return false;
  34.         }
  35.         return !$template->getLastUpdated() || $template->getLastUpdated()->getTimestamp() <= $time;
  36.     }
  37.     /**
  38.      * @param $name
  39.      * @return Template|null
  40.      */
  41.     protected function getTemplate($name)
  42.     {
  43.         return $this->repo->findOneBy(['filename' => $name]);
  44.     }
  45. }