function it_falls_back_to_decorated_template_locator_if_themed_tempaltes_can_not_be_found(FileLocatorInterface $decoratedFileLocator, ThemeContextInterface $themeContext, TemplateLocatorInterface $templateLocator, TemplateReferenceInterface $template, ThemeInterface $theme) { $themeContext->getThemeHierarchy()->willReturn([$theme]); $templateLocator->locateTemplate($template, $theme)->willThrow(ResourceNotFoundException::class); $decoratedFileLocator->locate($template, Argument::cetera())->willReturn('/app/template/path'); $this->locate($template)->shouldReturn('/app/template/path'); }
function it_throws_resource_not_found_exception_if_the_location_found_in_cache_is_null(TemplateLocatorInterface $decoratedTemplateLocator, Cache $cache, TemplateReferenceInterface $template, ThemeInterface $theme) { $template->getLogicalName()->willReturn('Logical:Name'); $template->getPath()->willReturn('@Acme/template.html.twig'); $theme->getName()->willReturn('theme/name'); $cache->contains('Logical:Name|theme/name')->willReturn(true); $cache->fetch('Logical:Name|theme/name')->willReturn(null); $decoratedTemplateLocator->locateTemplate(Argument::cetera())->shouldNotBeCalled(); $this->shouldThrow(ResourceNotFoundException::class)->during('locateTemplate', [$template, $theme]); }
/** * {@inheritdoc} */ public function locateTemplate(TemplateReferenceInterface $template, ThemeInterface $theme) { $cacheKey = $this->getCacheKey($template, $theme); if ($this->cache->contains($cacheKey)) { $location = $this->cache->fetch($cacheKey); if (null === $location) { throw new ResourceNotFoundException($template->getPath(), $theme); } return $location; } return $this->decoratedTemplateLocator->locateTemplate($template, $theme); }
function it_builds_cache_by_warming_up_every_template_and_every_theme_together(TemplateFinderInterface $templateFinder, TemplateLocatorInterface $templateLocator, ThemeRepositoryInterface $themeRepository, Cache $cache, ThemeInterface $theme, TemplateReferenceInterface $firstTemplate, TemplateReferenceInterface $secondTemplate) { $templateFinder->findAllTemplates()->willReturn([$firstTemplate, $secondTemplate]); $themeRepository->findAll()->willReturn([$theme]); $theme->getName()->willReturn('theme/name'); $firstTemplate->getLogicalName()->willReturn('Logical:Name:First'); $secondTemplate->getLogicalName()->willReturn('Logical:Name:Second'); $templateLocator->locateTemplate($firstTemplate, $theme)->willReturn('/First/Theme/index.html.twig'); $templateLocator->locateTemplate($secondTemplate, $theme)->willThrow(ResourceNotFoundException::class); $cache->save('Logical:Name:First|theme/name', '/First/Theme/index.html.twig')->shouldBeCalled(); $cache->save('Logical:Name:Second|theme/name', null)->shouldBeCalled(); $this->warmUp(null); }
/** * {@inheritdoc} */ public function locate($template, $currentPath = null, $first = true) { if (!$template instanceof TemplateReferenceInterface) { throw new \InvalidArgumentException('The template must be an instance of TemplateReferenceInterface.'); } $themes = $this->themeHierarchyProvider->getThemeHierarchy($this->themeContext->getTheme()); foreach ($themes as $theme) { try { return $this->templateLocator->locateTemplate($template, $theme); } catch (ResourceNotFoundException $exception) { // Ignore if resource cannot be found in given theme. } } return $this->decoratedFileLocator->locate($template, $currentPath, $first); }
/** * @param TemplateReferenceInterface $template * @param ThemeInterface $theme */ private function warmUpThemeTemplate(TemplateReferenceInterface $template, ThemeInterface $theme) { try { $location = $this->templateLocator->locateTemplate($template, $theme); } catch (ResourceNotFoundException $exception) { $location = null; } $this->cache->save($this->getCacheKey($template, $theme), $location); }