WebExtension.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
  3. use Symfony\Components\DependencyInjection\Extension\Extension;
  4. use Symfony\Components\DependencyInjection\Loader\XmlFileLoader;
  5. use Symfony\Components\DependencyInjection\Resource\FileResource;
  6. use Symfony\Components\DependencyInjection\ContainerBuilder;
  7. use Symfony\Components\DependencyInjection\Reference;
  8. use Symfony\Components\DependencyInjection\Definition;
  9. /*
  10. * This file is part of the Symfony framework.
  11. *
  12. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  13. *
  14. * This source file is subject to the MIT license that is bundled
  15. * with this source code in the file LICENSE.
  16. */
  17. /**
  18. * WebExtension.
  19. *
  20. * @package Symfony
  21. * @subpackage Bundle_FrameworkBundle
  22. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  23. */
  24. class WebExtension extends Extension
  25. {
  26. protected $resources = array(
  27. 'templating' => 'templating.xml',
  28. 'web' => 'web.xml',
  29. 'routing' => 'routing.xml',
  30. // validation.xml conflicts with the naming convention for XML
  31. // validation mapping files, so call it validator.xml
  32. 'validation' => 'validator.xml',
  33. );
  34. protected $bundleDirs = array();
  35. protected $bundles = array();
  36. public function __construct(array $bundleDirs, array $bundles)
  37. {
  38. $this->bundleDirs = $bundleDirs;
  39. $this->bundles = $bundles;
  40. }
  41. /**
  42. * Loads the web configuration.
  43. *
  44. * @param array $config An array of configuration settings
  45. * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance
  46. */
  47. public function configLoad($config, ContainerBuilder $container)
  48. {
  49. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  50. if (!$container->hasDefinition('controller_manager')) {
  51. $loader->load($this->resources['web']);
  52. }
  53. if (isset($config['ide']) && 'textmate' === $config['ide']) {
  54. $container->setParameter('debug.file_link_format', 'txmt://open?url=file://%%f&line=%%l');
  55. }
  56. if (isset($config['router'])) {
  57. if (!$container->hasDefinition('router')) {
  58. $loader->load($this->resources['routing']);
  59. }
  60. $container->setParameter('routing.resource', $config['router']['resource']);
  61. }
  62. if (isset($config['toolbar']) && $config['toolbar']) {
  63. $config['profiler'] = true;
  64. }
  65. if (isset($config['profiler'])) {
  66. if ($config['profiler']) {
  67. if (!$container->hasDefinition('profiler')) {
  68. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  69. $loader->load('profiling.xml');
  70. $loader->load('collectors.xml');
  71. }
  72. } elseif ($container->hasDefinition('profiler')) {
  73. $container->getDefinition('profiling')->clearAnnotations();
  74. }
  75. }
  76. // toolbar need to be registered after the profiler
  77. if (isset($config['toolbar'])) {
  78. if ($config['toolbar']) {
  79. if (!$container->hasDefinition('debug.toolbar')) {
  80. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  81. $loader->load('toolbar.xml');
  82. }
  83. } elseif ($container->hasDefinition('debug.toolbar')) {
  84. $container->getDefinition('debug.toolbar')->clearAnnotations();
  85. }
  86. }
  87. if (isset($config['validation']['enabled'])) {
  88. if ($config['validation']['enabled']) {
  89. if (!$container->hasDefinition('validator')) {
  90. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  91. $loader->load($this->resources['validation']);
  92. }
  93. $xmlMappingFiles = array();
  94. $yamlMappingFiles = array();
  95. $messageFiles = array();
  96. // default entries by the framework
  97. $xmlMappingFiles[] = __DIR__.'/../../../Components/Form/Resources/config/validation.xml';
  98. $messageFiles[] = __DIR__ . '/../../../Components/Validator/Resources/i18n/messages.en.xml';
  99. $messageFiles[] = __DIR__ . '/../../../Components/Form/Resources/i18n/messages.en.xml';
  100. foreach ($this->bundles as $className) {
  101. $tmp = dirname(str_replace('\\', '/', $className));
  102. $namespace = str_replace('/', '\\', dirname($tmp));
  103. $bundle = basename($tmp);
  104. foreach ($this->bundleDirs as $dir) {
  105. if (file_exists($file = $dir.'/'.$bundle.'/Resources/config/validation.xml')) {
  106. $xmlMappingFiles[] = realpath($file);
  107. }
  108. if (file_exists($file = $dir.'/'.$bundle.'/Resources/config/validation.yml')) {
  109. $yamlMappingFiles[] = realpath($file);
  110. }
  111. // TODO do we really want the message files of all cultures?
  112. foreach (glob($dir.'/'.$bundle.'/Resources/i18n/messages.*.xml') as $file) {
  113. $messageFiles[] = realpath($file);
  114. }
  115. }
  116. }
  117. $xmlFilesLoader = new Definition(
  118. $container->getParameter('validator.mapping.loader.xml_files_loader.class'),
  119. array($xmlMappingFiles)
  120. );
  121. $yamlFilesLoader = new Definition(
  122. $container->getParameter('validator.mapping.loader.yaml_files_loader.class'),
  123. array($yamlMappingFiles)
  124. );
  125. $container->setDefinition('validator.mapping.loader.xml_files_loader', $xmlFilesLoader);
  126. $container->setDefinition('validator.mapping.loader.yaml_files_loader', $yamlFilesLoader);
  127. $container->setParameter('validator.message_interpolator.files', $messageFiles);
  128. foreach ($xmlMappingFiles as $file) {
  129. $container->addResource(new FileResource($file));
  130. }
  131. foreach ($yamlMappingFiles as $file) {
  132. $container->addResource(new FileResource($file));
  133. }
  134. foreach ($messageFiles as $file) {
  135. $container->addResource(new FileResource($file));
  136. }
  137. if (isset($config['validation']['annotations']) && $config['validation']['annotations'] === true) {
  138. $annotationLoader = new Definition($container->getParameter('validator.mapping.loader.annotation_loader.class'));
  139. $container->setDefinition('validator.mapping.loader.annotation_loader', $annotationLoader);
  140. $loader = $container->getDefinition('validator.mapping.loader.loader_chain');
  141. $arguments = $loader->getArguments();
  142. array_unshift($arguments[0], new Reference('validator.mapping.loader.annotation_loader'));
  143. $loader->setArguments($arguments);
  144. }
  145. } elseif ($container->hasDefinition('validator')) {
  146. $container->getDefinition('validator')->clearAnnotations();
  147. }
  148. }
  149. }
  150. /**
  151. * Loads the templating configuration.
  152. *
  153. * @param array $config An array of configuration settings
  154. * @param \Symfony\Components\DependencyInjection\ContainerBuilder $container A ContainerBuilder instance
  155. */
  156. public function templatingLoad($config, ContainerBuilder $container)
  157. {
  158. if (!$container->hasDefinition('templating')) {
  159. $loader = new XmlFileLoader($container, __DIR__.'/../Resources/config');
  160. $loader->load($this->resources['templating']);
  161. }
  162. if (array_key_exists('escaping', $config)) {
  163. $container->setParameter('templating.output_escaper', $config['escaping']);
  164. }
  165. if (array_key_exists('assets_version', $config)) {
  166. $container->setParameter('templating.assets.version', $config['assets_version']);
  167. }
  168. // path for the filesystem loader
  169. if (isset($config['path'])) {
  170. $container->setParameter('templating.loader.filesystem.path', $config['path']);
  171. }
  172. // loaders
  173. if (isset($config['loader'])) {
  174. $loaders = array();
  175. $ids = is_array($config['loader']) ? $config['loader'] : array($config['loader']);
  176. foreach ($ids as $id) {
  177. $loaders[] = new Reference($id);
  178. }
  179. if (1 === count($loaders)) {
  180. $container->setAlias('templating.loader', (string) $loaders[0]);
  181. } else {
  182. $container->getDefinition('templating.loader.chain')->addArgument($loaders);
  183. $container->setAlias('templating.loader', 'templating.loader.chain');
  184. }
  185. }
  186. // cache?
  187. $container->setParameter('templating.loader.cache.path', null);
  188. if (isset($config['cache'])) {
  189. // wrap the loader with some cache
  190. $container->setDefinition('templating.loader.wrapped', $container->findDefinition('templating.loader'));
  191. $container->setDefinition('templating.loader', $container->getDefinition('templating.loader.cache'));
  192. $container->setParameter('templating.loader.cache.path', $config['cache']);
  193. }
  194. }
  195. /**
  196. * Returns the base path for the XSD files.
  197. *
  198. * @return string The XSD base path
  199. */
  200. public function getXsdValidationBasePath()
  201. {
  202. return __DIR__.'/../Resources/config/schema';
  203. }
  204. public function getNamespace()
  205. {
  206. return 'http://www.symfony-project.org/schema/dic/symfony';
  207. }
  208. public function getAlias()
  209. {
  210. return 'web';
  211. }
  212. }