BaseApplicationExtension.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 Bundle\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($config, 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($config, $container);
  44. // register the twig extension
  45. $container
  46. ->register('twig.extension.base_application', 'Bundle\Sonata\BaseApplicationBundle\Twig\Extension\BaseApplicationExtension')
  47. ->addTag('twig.extension');
  48. // registers crud action
  49. $definition = new Definition('Bundle\Sonata\BaseApplicationBundle\Admin\Pool');
  50. $definition->addMethodCall('setContainer', array(new Reference('service_container')));
  51. foreach($config['entities'] as $code => $configuration) {
  52. if(!isset($configuration['group'])) {
  53. $configuration['group'] = 'default';
  54. }
  55. if(!isset($configuration['label'])) {
  56. $configuration['label'] = $code;
  57. }
  58. $definition->addMethodCall('addConfiguration', array($code, $configuration));
  59. }
  60. $container->setDefinition('base_application.admin.pool', $definition);
  61. $definition = new Definition('Bundle\Sonata\BaseApplicationBundle\Route\AdminPoolLoader', array(new Reference('base_application.admin.pool')));
  62. $definition->addTag('routing.loader');
  63. $container->setDefinition('base_application.route_loader', $definition);
  64. }
  65. protected function configLoadFiles($container)
  66. {
  67. $loader = new XmlFileLoader($container, __DIR__ . '/../Resources/config');
  68. foreach ($this->configNamespaces as $ns => $params) {
  69. $loader->load(sprintf('%s.xml', $ns));
  70. }
  71. }
  72. protected function configSetup($config, $container)
  73. {
  74. foreach ($this->configNamespaces as $ns => $params) {
  75. if (!isset($config[$ns])) {
  76. continue;
  77. }
  78. foreach ($config[$ns] as $type => $template) {
  79. if (!isset($config[$ns][$type])) {
  80. continue;
  81. }
  82. $container->setParameter(sprintf('base_application.templates.%s', $type), $template);
  83. }
  84. }
  85. }
  86. /**
  87. * Returns the base path for the XSD files.
  88. *
  89. * @return string The XSD base path
  90. */
  91. public function getXsdValidationBasePath()
  92. {
  93. return __DIR__.'/../Resources/config/schema';
  94. }
  95. public function getNamespace()
  96. {
  97. return 'http://www.sonata-project.org/schema/dic/base-application';
  98. }
  99. public function getAlias()
  100. {
  101. return "base_application";
  102. }
  103. }