BaseApplicationExtension.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\BaseApplicationBundle\DependencyInjection;
  11. use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
  12. use Symfony\Component\DependencyInjection\Resource\FileResource;
  13. use Symfony\Component\DependencyInjection\ContainerBuilder;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\Definition;
  16. use Symfony\Component\DependencyInjection\Extension\Extension;
  17. use Symfony\Component\Finder\Finder;
  18. /**
  19. * BaseApplicationExtension
  20. *
  21. *
  22. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  23. */
  24. class BaseApplicationExtension extends Extension
  25. {
  26. protected $configNamespaces = array(
  27. 'templates' => array(
  28. 'layout',
  29. 'ajax'
  30. )
  31. );
  32. /**
  33. * Loads the url shortener configuration.
  34. *
  35. * @param array $config An array of configuration settings
  36. * @param ContainerBuilder $container A ContainerBuilder instance
  37. */
  38. public function configLoad($configs, ContainerBuilder $container)
  39. {
  40. // loads config from external files
  41. $this->configLoadFiles($container);
  42. // setups parameters with values in config.yml, default values from external files used if not
  43. $this->configSetup($configs, $container);
  44. // register the twig extension
  45. $container
  46. ->register('twig.extension.base_application', 'Sonata\BaseApplicationBundle\Twig\Extension\BaseApplicationExtension')
  47. ->addTag('twig.extension');
  48. // registers crud action
  49. $definition = new Definition('Sonata\BaseApplicationBundle\Admin\Pool');
  50. $definition->addMethodCall('setContainer', array(new Reference('service_container')));
  51. foreach ($configs as $config) {
  52. foreach ($config['entities'] as $code => $configuration) {
  53. if (!isset($configuration['group'])) {
  54. $configuration['group'] = 'default';
  55. }
  56. if (!isset($configuration['label'])) {
  57. $configuration['label'] = $code;
  58. }
  59. $definition->addMethodCall('addConfiguration', array($code, $configuration));
  60. }
  61. }
  62. $container->setDefinition('base_application.admin.pool', $definition);
  63. $definition = new Definition('Sonata\BaseApplicationBundle\Route\AdminPoolLoader', array(new Reference('base_application.admin.pool')));
  64. $definition->addTag('routing.loader');
  65. $container->setDefinition('base_application.route_loader', $definition);
  66. }
  67. protected function configLoadFiles($container)
  68. {
  69. $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config');
  70. foreach ($this->configNamespaces as $ns => $params) {
  71. $loader->load(sprintf('%s.xml', $ns));
  72. }
  73. }
  74. protected function configSetup($configs, $container)
  75. {
  76. foreach ($configs as $config) {
  77. foreach ($this->configNamespaces as $ns => $params) {
  78. if (!isset($config[$ns])) {
  79. continue;
  80. }
  81. foreach ($config[$ns] as $type => $template) {
  82. if (!isset($config[$ns][$type])) {
  83. continue;
  84. }
  85. $container->setParameter(sprintf('base_application.templates.%s', $type), $template);
  86. }
  87. }
  88. }
  89. }
  90. /**
  91. * Returns the base path for the XSD files.
  92. *
  93. * @return string The XSD base path
  94. */
  95. public function getXsdValidationBasePath()
  96. {
  97. return __DIR__.'/../Resources/config/schema';
  98. }
  99. public function getNamespace()
  100. {
  101. return 'http://www.sonata-project.org/schema/dic/base-application';
  102. }
  103. public function getAlias()
  104. {
  105. return "base_application";
  106. }
  107. }