AddDependencyCallsPass.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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\AdminBundle\DependencyInjection\Compiler;
  11. use Symfony\Component\DependencyInjection\Definition;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
  14. use Symfony\Component\DependencyInjection\Reference;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. /**
  17. * Add all dependencies to the Admin class, this avoid to write to many lines
  18. * in the configuration files.
  19. *
  20. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  21. */
  22. class AddDependencyCallsPass implements CompilerPassInterface
  23. {
  24. /**
  25. * {@inheritDoc}
  26. */
  27. public function process(ContainerBuilder $container)
  28. {
  29. $groups = $admins = $classes = array();
  30. $pool = $container->getDefinition('sonata.admin.pool');
  31. foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
  32. $definition = $container->getDefinition($id);
  33. $arguments = $definition->getArguments();
  34. if (strlen($arguments[0]) == 0) {
  35. $definition->replaceArgument(0, $id);
  36. }
  37. if (strlen($arguments[2]) == 0) {
  38. $definition->replaceArgument(2, 'SonataAdminBundle:CRUD');
  39. }
  40. $this->applyDefaults($definition, $attributes);
  41. $arguments = $definition->getArguments();
  42. if (preg_match('/%(.*)%/', $arguments[1], $matches)) {
  43. $class = $container->getParameter($matches[1]);
  44. } else {
  45. $class = $arguments[1];
  46. }
  47. $admins[] = $id;
  48. $classes[$class] = $id;
  49. $group_name = isset($attributes[0]['group']) ? $attributes[0]['group'] : 'default';
  50. if (!isset($groups[$group_name])) {
  51. $groups[$group_name] = array();
  52. }
  53. $groups[$group_name][$id] = array(
  54. 'show_in_dashboard' => (boolean)(isset($attributes[0]['show_in_dashboard']) ? $attributes[0]['show_in_dashboard'] : true)
  55. );
  56. }
  57. $pool->addMethodCall('setAdminServiceIds', array($admins));
  58. $pool->addMethodCall('setAdminGroups', array($groups));
  59. $pool->addMethodCall('setAdminClasses', array($classes));
  60. $routeLoader = $container->getDefinition('sonata.admin.route_loader');
  61. $routeLoader->replaceArgument(1, $admins);
  62. }
  63. /**
  64. * Apply the default values required by the AdminInterface to the Admin service definition
  65. *
  66. * @param \Symfony\Component\DependencyInjection\Definition $definition
  67. * @param array $attributes
  68. * @return \Symfony\Component\DependencyInjection\Definition
  69. */
  70. public function applyDefaults(Definition $definition, array $attributes = array())
  71. {
  72. $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  73. $manager_type = $attributes[0]['manager_type'];
  74. if (!$definition->hasMethodCall('setModelManager')) {
  75. $definition->addMethodCall('setModelManager', array(new Reference(sprintf('sonata.admin.manager.%s', $manager_type))));
  76. }
  77. if (!$definition->hasMethodCall('setFormContractor')) {
  78. $definition->addMethodCall('setFormContractor', array(new Reference(sprintf('sonata.admin.builder.%s_form', $manager_type))));
  79. }
  80. if (!$definition->hasMethodCall('setViewBuilder')) {
  81. $definition->addMethodCall('setViewBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_view', $manager_type))));
  82. }
  83. if (!$definition->hasMethodCall('setListBuilder')) {
  84. $definition->addMethodCall('setListBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_list', $manager_type))));
  85. }
  86. if (!$definition->hasMethodCall('setDatagridBuilder')) {
  87. $definition->addMethodCall('setDatagridBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_datagrid', $manager_type))));
  88. }
  89. if (!$definition->hasMethodCall('setTranslator')) {
  90. $definition->addMethodCall('setTranslator', array(new Reference('translator')));
  91. }
  92. if (!$definition->hasMethodCall('setConfigurationPool')) {
  93. $definition->addMethodCall('setConfigurationPool', array(new Reference('sonata.admin.pool')));
  94. }
  95. if (!$definition->hasMethodCall('setRouter')) {
  96. $definition->addMethodCall('setRouter', array(new Reference('router')));
  97. }
  98. if (!$definition->hasMethodCall('setValidator')) {
  99. $definition->addMethodCall('setValidator', array(new Reference('validator')));
  100. }
  101. if (!$definition->hasMethodCall('setSecurityHandler')) {
  102. $definition->addMethodCall('setSecurityHandler', array(new Reference('sonata.admin.security.handler')));
  103. }
  104. if (!$definition->hasMethodCall('setLabel')) {
  105. $label = isset($attributes[0]['label']) ? $attributes[0]['label'] : '-';
  106. $definition->addMethodCall('setLabel', array($label));
  107. }
  108. $definition->addMethodCall('configure');
  109. return $definition;
  110. }
  111. }