SonataBaseApplicationExtension.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Reference;
  14. use Symfony\Component\DependencyInjection\Definition;
  15. use Symfony\Component\DependencyInjection\Extension\Extension;
  16. use Symfony\Component\Config\FileLocator;
  17. use Symfony\Component\Config\Resource\FileResource;
  18. use Symfony\Component\Config\Definition\Processor;
  19. use Symfony\Component\Finder\Finder;
  20. /**
  21. * SonataBaseApplicationExtension
  22. *
  23. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  24. * @author Michael Williams <michael.williams@funsational.com>
  25. */
  26. class SonataBaseApplicationExtension extends Extension
  27. {
  28. protected $configNamespaces = array(
  29. 'templates' => array(
  30. 'layout',
  31. 'ajax'
  32. )
  33. );
  34. /**
  35. * Parses the configuration to setup the admin controllers and setup routing
  36. * information. Format is following:
  37. *
  38. * sonata_base_application:
  39. * entities:
  40. * post:
  41. * label: post
  42. * group: posts
  43. * class: Funsational\SimpleBlogBundle\Admin\Entity\PostAdmin
  44. * entity: Funsational\SimpleBlogBundle\Entity\Post
  45. * controller: Funsational\SimpleBlogBundle\Controller\PostAdminController
  46. * children:
  47. * comment:
  48. * label: comment
  49. * group: comments
  50. * class: Funsational\SimpleBlogBundle\Admin\Entity\CommentAdmin
  51. * entity: Funsational\SimpleBlogBundle\Entity\Comment
  52. * controller: Funsational\SimpleBlogBundle\Controller\CommentAdminController
  53. * options:
  54. * show_in_dashboard: true
  55. *
  56. * @param array $config An array of configuration settings
  57. * @param ContainerBuilder $container A ContainerBuilder instance
  58. */
  59. public function load(array $configs, ContainerBuilder $container)
  60. {
  61. $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
  62. $loader->load('templates.xml');
  63. $configuration = new Configuration();
  64. $processor = new Processor();
  65. $config = $processor->process($configuration->getConfigTree($container->getParameter('kernel.debug')), $configs);
  66. // setups parameters with values in config.yml, default values from external files used if not
  67. $this->configSetupTemplates($config, $container);
  68. // register the twig extension
  69. $container
  70. ->register('twig.extension.sonata_base_application', 'Sonata\BaseApplicationBundle\Twig\Extension\SonataBaseApplicationExtension')
  71. ->addTag('twig.extension');
  72. // register form builder
  73. $definition = new Definition('Sonata\BaseApplicationBundle\Builder\FormBuilder', array(new Reference('form.field_factory'), new Reference('form.context'), new Reference('validator')));
  74. $container->setDefinition('sonata_base_application.builder.orm_form', $definition);
  75. // register list builder
  76. $definition = new Definition('Sonata\BaseApplicationBundle\Builder\ListBuilder');
  77. $container->setDefinition('sonata_base_application.builder.orm_list', $definition);
  78. // register filter builder
  79. $definition = new Definition('Sonata\BaseApplicationBundle\Builder\DatagridBuilder');
  80. $container->setDefinition('sonata_base_application.builder.orm_datagrid', $definition);
  81. // registers crud action
  82. $definition = new Definition('Sonata\BaseApplicationBundle\Admin\Pool');
  83. $definition->addMethodCall('setContainer', array(new Reference('service_container')));
  84. foreach ($config['entities'] as $code => $configuration) {
  85. $definition->addMethodCall('addConfiguration', array($code, $configuration));
  86. }
  87. $container->setDefinition('sonata_base_application.admin.pool', $definition);
  88. $definition = new Definition('Sonata\BaseApplicationBundle\Route\AdminPoolLoader', array(new Reference('sonata_base_application.admin.pool')));
  89. $definition->addTag('routing.loader');
  90. $container->setDefinition('sonata_base_application.route_loader', $definition);
  91. }
  92. protected function configSetupTemplates($config, $container)
  93. {
  94. foreach ($this->configNamespaces as $ns => $params) {
  95. if (!isset($config[$ns])) {
  96. continue;
  97. }
  98. foreach ($config[$ns] as $type => $template) {
  99. if (!isset($config[$ns][$type])) {
  100. continue;
  101. }
  102. $container->setParameter(sprintf('sonata_base_application.templates.%s', $type), $template);
  103. }
  104. }
  105. }
  106. /**
  107. * Returns the base path for the XSD files.
  108. *
  109. * @return string The XSD base path
  110. */
  111. public function getXsdValidationBasePath()
  112. {
  113. return __DIR__.'/../Resources/config/schema';
  114. }
  115. public function getNamespace()
  116. {
  117. return 'http://www.sonata-project.org/schema/dic/base-application';
  118. }
  119. public function getAlias()
  120. {
  121. return "sonata_base_application";
  122. }
  123. }