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]); }
/** * Returns a full path for a given file. * * @param TemplateReferenceInterface $template A template * @param string $currentPath Unused * @param Boolean $first Unused * * @return string The full path for the file * * @throws \InvalidArgumentException When file is not found */ public function locate($template, $currentPath = null, $first = true) { $key = $template->getSignature(); if (isset($this->cache[$key])) { return $this->cache[$key]; } try { return $this->cache[$key] = $this->locator->locate($template->getPath(), $this->path); } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException(sprintf('Unable to find template "%s" in "%s".', $template, $this->path), 0, $e); } }
/** * Returns a full path for a given file. * * @param TemplateReferenceInterface $template A template * @param string $currentPath Unused * @param Boolean $first Unused * * @return string The full path for the file * * @throws \InvalidArgumentException When the template is not an instance of TemplateReferenceInterface * @throws \InvalidArgumentException When the template file can not be found */ public function locate($template, $currentPath = null, $first = true) { if (!$template instanceof TemplateReferenceInterface) { throw new \InvalidArgumentException("The template must be an instance of TemplateReferenceInterface."); } $key = $template->getLogicalName(); if (isset($this->cache[$key])) { return $this->cache[$key]; } try { return $this->cache[$key] = $this->locator->locate($template->getPath(), $currentPath); } catch (\InvalidArgumentException $e) { throw new \InvalidArgumentException(sprintf('Unable to find template "%s" : "%s".', $template, $e->getMessage()), 0, $e); } }
/** * Adds twig templates to the tracker. * * @param TemplateReferenceInterface $reference * @return Tracker */ private function addTemplate(TemplateReferenceInterface $reference) { if ($reference->get('engine') !== 'twig') { return $this; } if (false !== ($path = $this->resolvePath($reference->getPath()))) { $this->templates[] = $path; return $this->addPath($path); } // Can't resolve the template. This shouldn't happen, unless somebody placed a template in a very weird place. $this->untracked_templates[] = $reference->getPath(); return $this; }
/** * Get the realpath for the target TemplateReference object * @param TemplateReferenceInterface $template * @return string * @throws \InvalidArgumentException * @throws \RuntimeException */ protected function getTemplatePath(TemplateReferenceInterface $template) { $name = $template->getPath(); if ('@' !== substr($name, 0, 1)) { throw new \InvalidArgumentException(sprintf('A resource name must start with @ ("%s" given).', $name)); } if (false !== strpos($name, '..')) { throw new \RuntimeException(sprintf('File name "%s" contains invalid characters (..).', $name)); } $bundleName = substr($name, 1); $path = ''; if (false !== strpos($bundleName, '/')) { list($bundleName, $path) = explode('/', $bundleName, 2); } return $this->getApplication()->getKernel()->getBundle($bundleName)->getPath() . '/' . $path; }
/** * {@inheritdoc} */ public function locateTemplate(TemplateReferenceInterface $template, ThemeInterface $theme) { return $this->resourceLocator->locateResource($template->getPath(), $theme); }
function it_does_not_catch_exceptions_thrown_while_locating_template_to_resource_locator_even(ResourceLocatorInterface $resourceLocator, TemplateReferenceInterface $template, ThemeInterface $theme) { $template->getPath()->willReturn('@AcmeBundle/Resources/views/index.html.twig'); $resourceLocator->locateResource('@AcmeBundle/Resources/views/index.html.twig', $theme)->willThrow(ResourceNotFoundException::class); $this->shouldThrow(ResourceNotFoundException::class)->during('locateTemplate', [$template, $theme]); }
/** * Adds twig templates to the tracker. * * @param TemplateReferenceInterface $reference the reference to the twig template to be added. * @return Tracker this instance */ private function addTemplate(TemplateReferenceInterface $reference) { if ($reference->get('engine') !== 'twig') { return $this; } if (false !== ($path = $this->resolvePath($reference->getPath()))) { $this->templates[] = $path; return $this->addPath($path); } return $this; }