/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Templating/TemplateNameParser.php
PHP | 82 lines | 51 code | 7 blank | 24 comment | 1 complexity | 693a79e98c9ff3d66b193bb3c7822ab6 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Bundle\FrameworkBundle\Templating;
- use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
- use Symfony\Component\HttpKernel\KernelInterface;
- /**
- * TemplateNameParser parsers template name from the short notation
- * "bundle:section:template.engine.format" to an array of
- * template parameters.
- *
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class TemplateNameParser extends BaseTemplateNameParser
- {
- protected $kernel;
- /**
- * Constructor.
- *
- * @param KernelInterface $kernel A KernelInterface instance
- */
- public function __construct(KernelInterface $kernel)
- {
- $this->kernel = $kernel;
- }
- /**
- * {@inheritdoc}
- */
- public function parse($name)
- {
- if (is_array($name)) {
- return $name;
- }
- // normalize name
- $name = str_replace(':/' , ':', preg_replace('#/{2,}#', '/', strtr($name, '\\', '/')));
- if (false !== strpos($name, '..')) {
- throw new \RuntimeException(sprintf('Template name "%s" contains invalid characters.', $name));
- }
- $parts = explode(':', $name);
- if (3 !== count($parts)) {
- throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.engine.format").', $name));
- }
- $elements = explode('.', $parts[2]);
- if (3 !== count($elements)) {
- throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.engine.format").', $name));
- }
- $parameters = array(
- 'bundle' => $parts[0],
- 'controller' => $parts[1],
- 'name' => $elements[0],
- 'format' => $elements[1],
- 'engine' => $elements[2],
- );
- if ($parameters['bundle']) {
- try {
- $this->kernel->getBundle($parameters['bundle']);
- } catch (\Exception $e) {
- throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid.', $name), 0, $e);
- }
- }
- return $parameters;
- }
- }