src/WellCommerce/Bundle/AppBundle/Service/Theme/Locator/TemplateLocator.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=0);
  3. /*
  4.  * WellCommerce Foundation
  5.  *
  6.  * This file is part of the WellCommerce package.
  7.  *
  8.  * (c) Adam Piotrowski <adam@wellcommerce.org>, Adrian Potepa <adrian@wellcommerce.org>
  9.  *
  10.  * For the full copyright and license information,
  11.  * please view the LICENSE file that was distributed with this source code.
  12.  */
  13. namespace WellCommerce\Bundle\AppBundle\Service\Theme\Locator;
  14. use Symfony\Bundle\FrameworkBundle\Templating\Loader\TemplateLocator as BaseTemplateLocator;
  15. use Symfony\Component\Config\FileLocatorInterface;
  16. use Symfony\Component\Templating\TemplateReferenceInterface;
  17. /**
  18.  * Class TemplateLocator
  19.  *
  20.  * @author  Adam Piotrowski <adam@wellcommerce.org>
  21.  *
  22.  * Created on base of the LiipAppBundle <https://github.com/liip/LiipAppBundle>
  23.  *
  24.  * Special thanks goes to its authors and contributors
  25.  */
  26. class TemplateLocator extends BaseTemplateLocator
  27. {
  28.     /**
  29.      * @return FileLocatorInterface
  30.      */
  31.     public function getLocator()
  32.     {
  33.         return $this->locator;
  34.     }
  35.     
  36.     protected function getCacheKey($template)
  37.     {
  38.         return $template->getLogicalName();
  39.     }
  40.     
  41.     /**
  42.      * Returns a full path for a given file.
  43.      *
  44.      * @param string|TemplateReferenceInterface $template
  45.      * @param null                              $currentPath
  46.      * @param bool                              $first
  47.      *
  48.      * @return string The full path for the file
  49.      */
  50.     public function locate($template$currentPath null$first true)
  51.     {
  52.         if (!$template instanceof TemplateReferenceInterface) {
  53.             throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface.");
  54.         }
  55.         
  56.         $key $this->getCacheKey($template);
  57.         
  58.         if (!isset($this->cache[$key])) {
  59.             try {
  60.                 $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath);
  61.             } catch (\InvalidArgumentException $e) {
  62.                 throw new \InvalidArgumentException(sprintf(
  63.                     'Unable to find template "%s" in "%s".',
  64.                     $template,
  65.                     $e->getMessage()
  66.                 ));
  67.             }
  68.         }
  69.         
  70.         return $this->cache[$key];
  71.     }
  72. }