private function fallback($name) { if ($this->fallback) { return $this->fallback->parse($name); } else { return parent::parse($name); } }
protected function setUp() { parent::setUp(); $this->locator = $this->getMock('Symfony\\Component\\Config\\FileLocatorInterface'); $this->parser = $this->getMock('Symfony\\Component\\Templating\\TemplateNameParserInterface'); $this->loader = new FilesystemLoader($this->locator, $this->parser); $this->parser->expects($this->once())->method('parse')->with('name.format.engine')->will($this->returnValue(new TemplateReference('', '', 'name', 'format', 'engine'))); }
/** * {@inheritdoc} */ protected function load($name) { $template = $this->nameParser->parse($name); if (!file_exists($path = $template->getPath())) { throw new InvalidArgumentException(sprintf('The template "%s" does not exist.', $name)); } return parent::load($path); }
/** * Resolve path to absolute if any bundle is mentioned * @param string $path * @return string */ private function resolvePath($path) { try { return $this->locator->locate($this->templateNameParser->parse($path)); } catch (\InvalidArgumentException $e) { // happens when path is not bundle relative return $this->webRoot . '/' . $path; } }
/** * {@inheritdoc} * * It also supports \Twig_Template as name parameter. */ public function supports($name) { if ($name instanceof \Twig_Template) { return true; } $template = $this->parser->parse($name); return 'twig' === $template->get('engine'); }
/** * {@inheritdoc} */ public function supports(string $template) : bool { if ($template instanceof Twig_Template) { return true; } $reference = $this->parser->parse($template); return $reference->get('engine') === 'twig'; }
/** * @param TemplateReferenceInterface|string $template * * @return string */ private function findTemplate($template) { $logicalName = (string) $template; if (isset($this->cache[$logicalName])) { return $this->cache[$logicalName]; } $template = $this->templateNameParser->parse($template); $file = $this->templateLocator->locate($template); return $this->cache[$logicalName] = $file; }
/** * {@inheritdoc} */ public function parse($name) { if ($name instanceof TemplateReferenceInterface) { return $name; } elseif (isset($this->cache[$name])) { return $this->cache[$name]; } if (!preg_match('/^(?:@([^\\/]*)|)(?:\\/(.+))?\\/(.+)\\.([^\\.]+)\\.([^\\.]+)$/', $name, $matches)) { return $this->decoratedParser->parse($name); } $template = new TemplateReference($matches[1] ? $matches[1] . 'Bundle' : '', $matches[2], $matches[3], $matches[4], $matches[5]); if ($template->get('bundle')) { try { $this->kernel->getBundle($template->get('bundle')); } catch (\Exception $e) { throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e); } } return $this->cache[$name] = $template; }
public function parse(\Twig_Token $token) { if ($this->templateNameParser && is_array($this->enabledBundles)) { // check the bundle $templateRef = null; try { $templateRef = $this->templateNameParser->parse($this->parser->getStream()->getFilename()); } catch (\RuntimeException $e) { // this happens when the filename isn't a Bundle:* url // and it contains ".." } catch (\InvalidArgumentException $e) { // this happens when the filename isn't a Bundle:* url // but an absolute path instead } $bundle = $templateRef instanceof TemplateReference ? $templateRef->get('bundle') : null; if ($bundle && !in_array($bundle, $this->enabledBundles)) { throw new InvalidBundleException($bundle, "the {% {$this->getTag()} %} tag", $templateRef->getLogicalName(), $this->enabledBundles); } } return parent::parse($token); }
/** * Returns the type of a resource * * @param string $name the name of the resource * @return string type of the resource * * @throws \RuntimeException if the resource could not be loaded */ protected function getResourceType($name) { if (!isset($this->types[$name])) { $template = $this->parser->parse($name); if ($template instanceof TemplateReferenceInterface) { $this->types[$name] = $template->get('engine'); } } if (!isset($this->types[$name])) { throw new \RuntimeException("Could not load the given resource."); } return $this->types[$name]; }
protected function findTemplate($name) { if (!is_array($name)) { $name = $this->parser->parse($name); } $key = md5(serialize($name)); if (isset($this->cache[$key])) { return $this->cache[$key]; } if (false == ($file = $this->locator->locate($name))) { throw new \RuntimeException(sprintf('Unable to find template "%s".', json_encode($name))); } return $this->cache[$key] = $file; }
function it_delegates_unknown_paths_to_decorated_parser(TemplateNameParserInterface $decoratedParser, TemplateReferenceInterface $templateReference) { $decoratedParser->parse('Bundle/Not/namespaced.html.twig')->willReturn($templateReference); $this->parse('Bundle/Not/namespaced.html.twig')->shouldReturn($templateReference); }