SymfonyTemplatingExtension.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Symfony\Components\DependencyInjection\Loader\Extension;
  3. use Symfony\Components\DependencyInjection\Loader\LoaderExtension;
  4. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  5. use Symfony\Components\DependencyInjection\BuilderConfiguration;
  6. use Symfony\Components\DependencyInjection\Reference;
  7. /*
  8. * This file is part of the symfony framework.
  9. *
  10. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  11. *
  12. * This source file is subject to the MIT license that is bundled
  13. * with this source code in the file LICENSE.
  14. */
  15. /**
  16. * SymfonyTemplatingExtension is an extension for the Symfony Templating Component.
  17. *
  18. * @package symfony
  19. * @subpackage dependency_injection
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class SymfonyTemplatingExtension extends LoaderExtension
  23. {
  24. protected $resources = array(
  25. 'templating' => 'templating-1.0.xml',
  26. );
  27. protected $defaultHelpers = array();
  28. protected $alias;
  29. public function setAlias($alias)
  30. {
  31. $this->alias = $alias;
  32. }
  33. public function setDefaultHelpers(array $defaultHelpers)
  34. {
  35. $this->defaultHelpers = $defaultHelpers;
  36. }
  37. /**
  38. * Loads the templating configuration.
  39. *
  40. * Usage example:
  41. *
  42. * <symfony:templating path="/path/to/templates" cache="/path/to/cache">
  43. * <symfony:loader>symfony.templating.loader.filesystem</symfony:loader>
  44. * <symfony:helpers>
  45. * symfony.templating.helper.javascripts
  46. * symfony.templating.helper.stylesheets
  47. * </symfony:helpers>
  48. * </symfony:templating>
  49. *
  50. * @param array $config A configuration array
  51. *
  52. * @return BuilderConfiguration A BuilderConfiguration instance
  53. */
  54. public function templatingLoad($config)
  55. {
  56. $configuration = new BuilderConfiguration();
  57. $loader = new XmlFileLoader(__DIR__.'/xml/symfony');
  58. $configuration->merge($loader->load($this->resources['templating']));
  59. // path for the filesystem loader
  60. if (isset($config['path']))
  61. {
  62. $configuration->setParameter('symfony.templating.loader.filesystem.path', $config['path']);
  63. }
  64. // loaders
  65. if (isset($config['loader']))
  66. {
  67. $loaders = array();
  68. $ids = is_array($config['loader']) ? $config['loader'] : array($config['loader']);
  69. foreach ($ids as $id)
  70. {
  71. $loaders[] = new Reference($id);
  72. }
  73. }
  74. else
  75. {
  76. $loaders = array(
  77. new Reference('symfony.templating.loader.filesystem'),
  78. );
  79. }
  80. if (1 === count($loaders))
  81. {
  82. $configuration->setAlias('symfony.templating.loader', (string) $loaders[0]);
  83. }
  84. else
  85. {
  86. $configuration->getDefinition('symfony.templating.loader.chain')->addArgument($loaders);
  87. $configuration->setAlias('symfony.templating.loader', 'symfony.templating.loader.chain');
  88. }
  89. // helpers
  90. if (array_key_exists('helpers', $config))
  91. {
  92. $helpers = array();
  93. foreach (explode("\n", $config['helpers']) as $helper)
  94. {
  95. if ($helper)
  96. {
  97. $helpers[] = new Reference(trim($helper));
  98. }
  99. }
  100. }
  101. else
  102. {
  103. $helpers = $this->defaultHelpers;
  104. }
  105. $configuration->getDefinition('symfony.templating.helperset')->addArgument($helpers);
  106. // cache?
  107. if (isset($config['cache']))
  108. {
  109. // wrap the loader with some cache
  110. $configuration->setDefinition('symfony.templating.loader.wrapped', $configuration->findDefinition('symfony.templating.loader'));
  111. $configuration->setDefinition('symfony.templating.loader', $configuration->getDefinition('symfony.templating.loader.cache'));
  112. $configuration->setParameter('symfony.templating.loader.cache.path', $config['cache']);
  113. }
  114. $configuration->setAlias('templating', null !== $this->alias ? $this->alias : 'symfony.templating.engine');
  115. return $configuration;
  116. }
  117. /**
  118. * Returns the namespace to be used for this extension (XML namespace).
  119. *
  120. * @return string The XML namespace
  121. */
  122. public function getNamespace()
  123. {
  124. return 'http://www.symfony-project.org/schema/dic/symfony';
  125. }
  126. /**
  127. * Returns the recommanded alias to use in XML.
  128. *
  129. * This alias is also the mandatory prefix to use when using YAML.
  130. *
  131. * @return string The alias
  132. */
  133. public function getAlias()
  134. {
  135. return 'symfony';
  136. }
  137. }