WebExtension.php 10 KB

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