FrameworkExtension.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\ContainerBuilder;
  12. use Symfony\Component\DependencyInjection\Definition;
  13. use Symfony\Component\DependencyInjection\Parameter;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  16. use Symfony\Component\Config\Resource\FileResource;
  17. use Symfony\Component\Finder\Finder;
  18. use Symfony\Component\HttpKernel\DependencyInjection\Extension;
  19. use Symfony\Component\Config\FileLocator;
  20. use Symfony\Component\Config\Definition\Processor;
  21. /**
  22. * FrameworkExtension.
  23. *
  24. * @author Fabien Potencier <fabien@symfony.com>
  25. * @author Jeremy Mikola <jmikola@gmail.com>
  26. */
  27. class FrameworkExtension extends Extension
  28. {
  29. /**
  30. * Responds to the app.config configuration parameter.
  31. *
  32. * @param array $configs
  33. * @param ContainerBuilder $container
  34. */
  35. public function load(array $configs, ContainerBuilder $container)
  36. {
  37. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  38. $loader->load('web.xml');
  39. $loader->load('form.xml');
  40. $loader->load('services.xml');
  41. // A translator must always be registered (as support is included by
  42. // default in the Form component). If disabled, an identity translator
  43. // will be used and everything will still work as expected.
  44. $loader->load('translation.xml');
  45. if ($container->getParameter('kernel.debug')) {
  46. $loader->load('debug.xml');
  47. $container->setDefinition('event_dispatcher', $container->findDefinition('debug.event_dispatcher'));
  48. $container->setAlias('debug.event_dispatcher', 'event_dispatcher');
  49. }
  50. $processor = new Processor();
  51. $configuration = new Configuration($container->getParameter('kernel.debug'));
  52. $config = $processor->processConfiguration($configuration, $configs);
  53. $container->setParameter('kernel.cache_warmup', $config['cache_warmer']);
  54. if (isset($config['charset'])) {
  55. $container->setParameter('kernel.charset', $config['charset']);
  56. }
  57. $container->setParameter('kernel.secret', $config['secret']);
  58. if (isset($config['error_handler'])) {
  59. if (false === $config['error_handler']) {
  60. $container->getDefinition('error_handler')->setMethodCalls(array());
  61. } else {
  62. $container
  63. ->getDefinition('error_handler')->addMethodCall('register', array())
  64. ->replaceArgument(0, $config['error_handler'])
  65. ;
  66. }
  67. }
  68. $container->getDefinition('exception_listener')->replaceArgument(0, $config['exception_controller']);
  69. if (!empty($config['test'])) {
  70. $loader->load('test.xml');
  71. if (isset($config['session'])) {
  72. $config['session']['storage_id'] = 'session.storage.filesystem';
  73. }
  74. }
  75. if (isset($config['csrf_protection'])) {
  76. $this->registerCsrfProtectionConfiguration($config['csrf_protection'], $container);
  77. }
  78. if (isset($config['esi'])) {
  79. $this->registerEsiConfiguration($config['esi'], $loader);
  80. }
  81. if (isset($config['profiler'])) {
  82. $this->registerProfilerConfiguration($config['profiler'], $container, $loader);
  83. }
  84. if (isset($config['router'])) {
  85. $this->registerRouterConfiguration($config['router'], $container, $loader);
  86. }
  87. if (isset($config['session'])) {
  88. $this->registerSessionConfiguration($config['session'], $container, $loader);
  89. }
  90. if (isset($config['templating'])) {
  91. $this->registerTemplatingConfiguration($config['templating'], $config['ide'], $container, $loader);
  92. }
  93. if (isset($config['translator'])) {
  94. $this->registerTranslatorConfiguration($config['translator'], $container);
  95. }
  96. if (isset($config['validation'])) {
  97. $this->registerValidationConfiguration($config['validation'], $container, $loader);
  98. }
  99. $this->addClassesToCompile(array(
  100. 'Symfony\\Component\\HttpFoundation\\ParameterBag',
  101. 'Symfony\\Component\\HttpFoundation\\HeaderBag',
  102. 'Symfony\\Component\\HttpFoundation\\Request',
  103. 'Symfony\\Component\\HttpFoundation\\Response',
  104. 'Symfony\\Component\\HttpFoundation\\ResponseHeaderBag',
  105. 'Symfony\\Component\\EventDispatcher\\EventDispatcherInterface',
  106. 'Symfony\\Component\\EventDispatcher\\EventDispatcher',
  107. 'Symfony\\Component\\EventDispatcher\\Event',
  108. 'Symfony\\Component\\EventDispatcher\\EventSubscriberInterface',
  109. 'Symfony\\Component\\HttpKernel\\HttpKernel',
  110. 'Symfony\\Component\\HttpKernel\\ResponseListener',
  111. 'Symfony\\Component\\HttpKernel\\Controller\\ControllerResolver',
  112. 'Symfony\\Component\\HttpKernel\\Controller\\ControllerResolverInterface',
  113. 'Symfony\\Component\\HttpKernel\\Event\\KernelEvent',
  114. 'Symfony\\Component\\HttpKernel\\Event\\FilterControllerEvent',
  115. 'Symfony\\Component\\HttpKernel\\Event\\FilterResponseEvent',
  116. 'Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent',
  117. 'Symfony\\Component\\HttpKernel\\Event\\GetResponseForControllerResultEvent',
  118. 'Symfony\\Component\\HttpKernel\\Event\\GetResponseForExceptionEvent',
  119. 'Symfony\\Component\\HttpKernel\\Events',
  120. 'Symfony\\Bundle\\FrameworkBundle\\RequestListener',
  121. 'Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerNameParser',
  122. 'Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerResolver',
  123. 'Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller',
  124. 'Symfony\\Bundle\\FrameworkBundle\\ContainerAwareEventDispatcher',
  125. ));
  126. }
  127. /**
  128. * Loads the CSRF protection configuration.
  129. *
  130. * @param array $config A CSRF protection configuration array
  131. * @param ContainerBuilder $container A ContainerBuilder instance
  132. */
  133. private function registerCsrfProtectionConfiguration(array $config, ContainerBuilder $container)
  134. {
  135. $container
  136. ->getDefinition('form.type_extension.csrf')
  137. ->replaceArgument(0, $config['enabled'])
  138. ->replaceArgument(1, $config['field_name'])
  139. ;
  140. }
  141. /**
  142. * Loads the ESI configuration.
  143. *
  144. * @param array $config An ESI configuration array
  145. * @param XmlFileLoader $loader An XmlFileLoader instance
  146. */
  147. private function registerEsiConfiguration(array $config, XmlFileLoader $loader)
  148. {
  149. if (!empty($config['enabled'])) {
  150. $loader->load('esi.xml');
  151. }
  152. }
  153. /**
  154. * Loads the profiler configuration.
  155. *
  156. * @param array $config A profiler configuration array
  157. * @param ContainerBuilder $container A ContainerBuilder instance
  158. * @param XmlFileLoader $loader An XmlFileLoader instance
  159. */
  160. private function registerProfilerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
  161. {
  162. $loader->load('profiling.xml');
  163. $loader->load('collectors.xml');
  164. $container->getDefinition('profiler_listener')
  165. ->replaceArgument(2, $config['only_exceptions'])
  166. ->replaceArgument(3, $config['only_master_requests'])
  167. ;
  168. // Choose storage class based on the DSN
  169. $supported = array(
  170. 'sqlite' => 'Symfony\Component\HttpKernel\Profiler\SqliteProfilerStorage',
  171. 'mysql' => 'Symfony\Component\HttpKernel\Profiler\MysqlProfilerStorage',
  172. );
  173. list($class, ) = explode(':', $config['dsn']);
  174. if (!isset($supported[$class])) {
  175. throw new \LogicException(sprintf('Driver "%s" is not supported for the profiler.', $class));
  176. }
  177. $container->getDefinition('profiler.storage')
  178. ->replaceArgument(0, $config['dsn'])
  179. ->replaceArgument(1, $config['username'])
  180. ->replaceArgument(2, $config['password'])
  181. ->replaceArgument(3, $config['lifetime'])
  182. ->setClass($supported[$class])
  183. ;
  184. if (isset($config['matcher'])) {
  185. if (isset($config['matcher']['service'])) {
  186. $container->setAlias('profiler.request_matcher', $config['matcher']['service']);
  187. } elseif (isset($config['matcher']['ip']) || isset($config['matcher']['path'])) {
  188. $definition = $container->register('profiler.request_matcher', 'Symfony\\Component\\HttpFoundation\\RequestMatcher');
  189. $definition->setPublic(false);
  190. if (isset($config['matcher']['ip'])) {
  191. $definition->addMethodCall('matchIp', array($config['matcher']['ip']));
  192. }
  193. if (isset($config['matcher']['path'])) {
  194. $definition->addMethodCall('matchPath', array($config['matcher']['path']));
  195. }
  196. }
  197. }
  198. }
  199. /**
  200. * Loads the router configuration.
  201. *
  202. * @param array $config A router configuration array
  203. * @param ContainerBuilder $container A ContainerBuilder instance
  204. * @param XmlFileLoader $loader An XmlFileLoader instance
  205. */
  206. private function registerRouterConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
  207. {
  208. $loader->load('routing.xml');
  209. $router = $container->findDefinition('router.real');
  210. $router->replaceArgument(1, $config['resource']);
  211. if (isset($config['type'])) {
  212. $argument = $router->getArgument(2);
  213. $argument['resource_type'] = $config['type'];
  214. $router->replaceArgument(2, $argument);
  215. }
  216. if ($config['cache_warmer']) {
  217. $container->getDefinition('router.cache_warmer')->addTag('kernel.cache_warmer');
  218. $container->setAlias('router', 'router.cached');
  219. }
  220. $def = $container->getDefinition('request_listener');
  221. $def->replaceArgument(2, $config['http_port']);
  222. $def->replaceArgument(3, $config['https_port']);
  223. $this->addClassesToCompile(array(
  224. 'Symfony\\Component\\Routing\\RouterInterface',
  225. 'Symfony\\Component\\Routing\\Matcher\\UrlMatcherInterface',
  226. 'Symfony\\Component\\Routing\\Matcher\\UrlMatcher',
  227. 'Symfony\\Component\\Routing\\Generator\\UrlGeneratorInterface',
  228. 'Symfony\\Component\\Routing\\Generator\\UrlGenerator',
  229. $container->findDefinition('router')->getClass(),
  230. ));
  231. }
  232. /**
  233. * Loads the session configuration.
  234. *
  235. * @param array $config A session configuration array
  236. * @param ContainerBuilder $container A ContainerBuilder instance
  237. * @param XmlFileLoader $loader An XmlFileLoader instance
  238. */
  239. private function registerSessionConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
  240. {
  241. $loader->load('session.xml');
  242. // session
  243. $session = $container->getDefinition('session');
  244. if (!empty($config['auto_start'])) {
  245. $session->addMethodCall('start');
  246. }
  247. $session->replaceArgument(1, $config['default_locale']);
  248. // session storage
  249. $container->setAlias('session.storage', $config['storage_id']);
  250. $options = array();
  251. foreach (array('name', 'lifetime', 'path', 'domain', 'secure', 'httponly') as $key) {
  252. if (isset($config[$key])) {
  253. $options[$key] = $config[$key];
  254. }
  255. }
  256. $container->setParameter('session.storage.options', $options);
  257. $this->addClassesToCompile(array(
  258. 'Symfony\\Component\\HttpFoundation\\SessionStorage\\SessionStorageInterface',
  259. $container->getDefinition('session')->getClass(),
  260. ));
  261. if ($container->hasDefinition($config['storage_id'])) {
  262. $this->addClassesToCompile(array(
  263. $container->findDefinition('session.storage')->getClass(),
  264. ));
  265. }
  266. }
  267. /**
  268. * Loads the templating configuration.
  269. *
  270. * @param array $config A templating configuration array
  271. * @param string $ide
  272. * @param ContainerBuilder $container A ContainerBuilder instance
  273. * @param XmlFileLoader $loader An XmlFileLoader instance
  274. */
  275. private function registerTemplatingConfiguration(array $config, $ide, ContainerBuilder $container, XmlFileLoader $loader)
  276. {
  277. $loader->load('templating.xml');
  278. $loader->load('templating_php.xml');
  279. $links = array(
  280. 'textmate' => 'txmt://open?url=file://%f&line=%l',
  281. 'macvim' => 'mvim://open?url=file://%f&line=%l',
  282. );
  283. $container
  284. ->getDefinition('templating.helper.code')
  285. ->replaceArgument(0, str_replace('%', '%%', isset($links[$ide]) ? $links[$ide] : $ide))
  286. ;
  287. if ($container->getParameter('kernel.debug')) {
  288. $loader->load('templating_debug.xml');
  289. }
  290. $packages = array();
  291. foreach ($config['packages'] as $name => $package) {
  292. $packages[$name] = new Definition('%templating.asset_package.class%', array(
  293. $package['base_urls'],
  294. $package['version'],
  295. ));
  296. }
  297. $container
  298. ->getDefinition('templating.helper.assets')
  299. ->replaceArgument(1, isset($config['assets_base_urls']) ? $config['assets_base_urls'] : array())
  300. ->replaceArgument(2, $config['assets_version'])
  301. ->replaceArgument(3, $packages)
  302. ;
  303. if (!empty($config['loaders'])) {
  304. $loaders = array_map(function($loader) { return new Reference($loader); }, $config['loaders']);
  305. // Use a delegation unless only a single loader was registered
  306. if (1 === count($loaders)) {
  307. $container->setAlias('templating.loader', (string) reset($loaders));
  308. } else {
  309. $container->getDefinition('templating.loader.chain')->addArgument($loaders);
  310. $container->setAlias('templating.loader', 'templating.loader.chain');
  311. }
  312. }
  313. if (isset($config['cache'])) {
  314. // Wrap the existing loader with cache (must happen after loaders are registered)
  315. $container->setDefinition('templating.loader.wrapped', $container->findDefinition('templating.loader'));
  316. $loaderCache = $container->getDefinition('templating.loader.cache');
  317. $loaderCache->replaceArgument(1, $config['cache']);
  318. $container->setDefinition('templating.loader', $loaderCache);
  319. }
  320. if ($config['cache_warmer']) {
  321. $container
  322. ->getDefinition('templating.cache_warmer.template_paths')
  323. ->addTag('kernel.cache_warmer', array('priority' => 20))
  324. ;
  325. $container->setAlias('templating.locator', 'templating.locator.cached');
  326. } else {
  327. $container->setAlias('templating.locator', 'templating.locator.uncached');
  328. }
  329. $this->addClassesToCompile(array(
  330. 'Symfony\\Bundle\\FrameworkBundle\\Templating\\EngineInterface',
  331. 'Symfony\\Component\\Templating\\TemplateNameParserInterface',
  332. 'Symfony\\Component\\Templating\\TemplateNameParser',
  333. 'Symfony\\Component\\Templating\\EngineInterface',
  334. 'Symfony\\Component\\Config\\FileLocatorInterface',
  335. 'Symfony\\Component\\Templating\\TemplateReferenceInterface',
  336. 'Symfony\\Component\\Templating\\TemplateReference',
  337. 'Symfony\\Bundle\\FrameworkBundle\\Templating\\TemplateReference',
  338. 'Symfony\\Bundle\\FrameworkBundle\\Templating\\TemplateNameParser',
  339. $container->findDefinition('templating.locator')->getClass(),
  340. ));
  341. if (in_array('php', $config['engines'], true)) {
  342. $this->addClassesToCompile(array(
  343. 'Symfony\\Component\\Templating\\PhpEngine',
  344. 'Symfony\\Component\\Templating\\Loader\\LoaderInterface',
  345. 'Symfony\\Component\\Templating\\Storage\\Storage',
  346. 'Symfony\\Component\\Templating\\Storage\\FileStorage',
  347. 'Symfony\\Bundle\\FrameworkBundle\\Templating\\PhpEngine',
  348. 'Symfony\\Bundle\\FrameworkBundle\\Templating\\Loader\\FilesystemLoader',
  349. ));
  350. }
  351. $container->setParameter('templating.engines', $config['engines']);
  352. $engines = array_map(function($engine) { return new Reference('templating.engine.'.$engine); }, $config['engines']);
  353. // Use a delegation unless only a single engine was registered
  354. if (1 === count($engines)) {
  355. $container->setAlias('templating', (string) reset($engines));
  356. } else {
  357. $container->getDefinition('templating.engine.delegating')->replaceArgument(1, $engines);
  358. $container->setAlias('templating', 'templating.engine.delegating');
  359. }
  360. }
  361. /**
  362. * Loads the translator configuration.
  363. *
  364. * @param array $config A translator configuration array
  365. * @param ContainerBuilder $container A ContainerBuilder instance
  366. */
  367. private function registerTranslatorConfiguration(array $config, ContainerBuilder $container)
  368. {
  369. if (!empty($config['enabled'])) {
  370. // Use the "real" translator instead of the identity default
  371. $container->setDefinition('translator', $translator = $container->findDefinition('translator.real'));
  372. $translator->addMethodCall('setFallbackLocale', array($config['fallback']));
  373. // Discover translation directories
  374. $dirs = array();
  375. foreach ($container->getParameter('kernel.bundles') as $bundle) {
  376. $reflection = new \ReflectionClass($bundle);
  377. if (is_dir($dir = dirname($reflection->getFilename()).'/Resources/translations')) {
  378. $dirs[] = $dir;
  379. }
  380. }
  381. if (is_dir($dir = $container->getParameter('kernel.root_dir').'/translations')) {
  382. $dirs[] = $dir;
  383. }
  384. // Register translation resources
  385. if ($dirs) {
  386. $finder = new Finder();
  387. $finder->files()->filter(function (\SplFileInfo $file) { return 2 === substr_count($file->getBasename(), '.'); })->in($dirs);
  388. foreach ($finder as $file) {
  389. // filename is domain.locale.format
  390. list($domain, $locale, $format) = explode('.', $file->getBasename());
  391. $translator->addMethodCall('addResource', array($format, (string) $file, $locale, $domain));
  392. }
  393. }
  394. }
  395. }
  396. /**
  397. * Loads the validator configuration.
  398. *
  399. * @param array $config A validation configuration array
  400. * @param ContainerBuilder $container A ContainerBuilder instance
  401. * @param XmlFileLoader $loader An XmlFileLoader instance
  402. */
  403. private function registerValidationConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
  404. {
  405. if (empty($config['enabled'])) {
  406. return;
  407. }
  408. $loader->load('validator.xml');
  409. $container
  410. ->getDefinition('validator.mapping.loader.xml_files_loader')
  411. ->replaceArgument(0, $this->getValidatorXmlMappingFiles($container))
  412. ;
  413. $container
  414. ->getDefinition('validator.mapping.loader.yaml_files_loader')
  415. ->replaceArgument(0, $this->getValidatorYamlMappingFiles($container))
  416. ;
  417. if (isset($config['annotations'])) {
  418. $namespaces = array('assert' => 'Symfony\\Component\\Validator\\Constraints\\');
  419. // Register prefixes for constraint namespaces
  420. if (!empty($config['annotations']['namespaces'])) {
  421. $namespaces = array_merge($namespaces, $config['annotations']['namespaces']);
  422. }
  423. // Register annotation loader
  424. $container
  425. ->getDefinition('validator.mapping.loader.annotation_loader')
  426. ->replaceArgument(0, $namespaces)
  427. ;
  428. $loaderChain = $container->getDefinition('validator.mapping.loader.loader_chain');
  429. $arguments = $loaderChain->getArguments();
  430. array_unshift($arguments[0], new Reference('validator.mapping.loader.annotation_loader'));
  431. $loaderChain->setArguments($arguments);
  432. }
  433. if (isset($config['cache'])) {
  434. $container->getDefinition('validator.mapping.class_metadata_factory')
  435. ->replaceArgument(1, new Reference('validator.mapping.cache.'.$config['cache']));
  436. $container->setParameter(
  437. 'validator.mapping.cache.prefix',
  438. 'validator_'.md5($container->getParameter('kernel.root_dir'))
  439. );
  440. }
  441. }
  442. private function getValidatorXmlMappingFiles(ContainerBuilder $container)
  443. {
  444. $files = array(__DIR__.'/../../../Component/Form/Resources/config/validation.xml');
  445. $container->addResource(new FileResource($files[0]));
  446. foreach ($container->getParameter('kernel.bundles') as $bundle) {
  447. $reflection = new \ReflectionClass($bundle);
  448. if (file_exists($file = dirname($reflection->getFilename()).'/Resources/config/validation.xml')) {
  449. $files[] = realpath($file);
  450. $container->addResource(new FileResource($file));
  451. }
  452. }
  453. return $files;
  454. }
  455. private function getValidatorYamlMappingFiles(ContainerBuilder $container)
  456. {
  457. $files = array();
  458. foreach ($container->getParameter('kernel.bundles') as $bundle) {
  459. $reflection = new \ReflectionClass($bundle);
  460. if (file_exists($file = dirname($reflection->getFilename()).'/Resources/config/validation.yml')) {
  461. $files[] = realpath($file);
  462. $container->addResource(new FileResource($file));
  463. }
  464. }
  465. return $files;
  466. }
  467. /**
  468. * Returns the base path for the XSD files.
  469. *
  470. * @return string The XSD base path
  471. */
  472. public function getXsdValidationBasePath()
  473. {
  474. return __DIR__.'/../Resources/config/schema';
  475. }
  476. public function getNamespace()
  477. {
  478. return 'http://symfony.com/schema/dic/symfony';
  479. }
  480. }