TemplateNameParser.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\Templating;
  3. use Symfony\Component\Templating\TemplateNameParser as BaseTemplateNameParser;
  4. use Symfony\Component\HttpKernel\Kernel;
  5. /*
  6. * This file is part of the Symfony package.
  7. *
  8. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  9. *
  10. * For the full copyright and license information, please view the LICENSE
  11. * file that was distributed with this source code.
  12. */
  13. /**
  14. * TemplateNameParser parsers template name from the short notation
  15. * "bundle:section:template.renderer.format" to a template name
  16. * and an array of options.
  17. *
  18. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  19. */
  20. class TemplateNameParser extends BaseTemplateNameParser
  21. {
  22. protected $kernel;
  23. /**
  24. * Constructor.
  25. *
  26. * @param Kernel $kernel A Kernel instance
  27. */
  28. public function __construct(Kernel $kernel)
  29. {
  30. $this->kernel = $kernel;
  31. }
  32. /**
  33. * Parses a template to a template name and an array of options.
  34. *
  35. * @param string $name A template name
  36. * @param array $defaults An array of default options
  37. *
  38. * @return array An array composed of the template name and an array of options
  39. */
  40. public function parse($name, array $defaults = array())
  41. {
  42. $parts = explode(':', $name);
  43. if (3 !== count($parts)) {
  44. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.renderer.format").', $name));
  45. }
  46. $bundle = null;
  47. if ($parts[0]) {
  48. foreach ($this->kernel->getBundles() as $b) {
  49. if ($parts[0] !== $b->getName()) {
  50. continue;
  51. }
  52. foreach (array_keys($this->kernel->getBundleDirs()) as $prefix) {
  53. if (0 === $pos = strpos($b->getNamespace(), $prefix)) {
  54. $bundle = str_replace($prefix.'\\', '', $b->getNamespace());
  55. break 2;
  56. }
  57. }
  58. }
  59. if (null === $bundle) {
  60. throw new \InvalidArgumentException(sprintf('Unable to find a valid bundle name for template "%s".', $name));
  61. }
  62. }
  63. $options = array_replace(
  64. array(
  65. 'format' => '',
  66. ),
  67. $defaults,
  68. array(
  69. // bundle is used as part of the template path, so we need /
  70. 'bundle' => str_replace('\\', '/', $bundle),
  71. 'controller' => $parts[1],
  72. )
  73. );
  74. $elements = explode('.', $parts[2]);
  75. if (3 !== count($elements)) {
  76. throw new \InvalidArgumentException(sprintf('Template name "%s" is not valid (format is "bundle:section:template.renderer.format").', $name));
  77. }
  78. $parts[2] = $elements[0];
  79. $options['renderer'] = $elements[1];
  80. $options['format'] = $elements[2];
  81. return array($parts[2], $options);
  82. }
  83. }