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