AddDependencyCallsPass.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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;
  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. //
  31. $pool = $container->getDefinition('sonata.admin.pool');
  32. foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
  33. $definition = $container->getDefinition($id);
  34. $arguments = $definition->getArguments();
  35. if (strlen($arguments[0]) == 0) {
  36. $definition->replaceArgument(0, $id);
  37. }
  38. if (strlen($arguments[2]) == 0) {
  39. $definition->replaceArgument(2, 'SonataAdminBundle:CRUD');
  40. }
  41. $this->applyDefaults($definition, $attributes);
  42. $arguments = $definition->getArguments();
  43. if (preg_match('/%(.*)%/', $arguments[1], $matches)) {
  44. $class = $container->getParameter($matches[1]);
  45. } else {
  46. $class = $arguments[1];
  47. }
  48. $admins[] = $id;
  49. $classes[$class] = $id;
  50. $group_name = isset($attributes[0]['group']) ? $attributes[0]['group'] : 'default';
  51. if (!isset($groups[$group_name])) {
  52. $groups[$group_name] = array();
  53. }
  54. $groups[$group_name][$id] = array(
  55. 'show_in_dashboard' => (boolean)(isset($attributes[0]['show_in_dashboard']) ? $attributes[0]['show_in_dashboard'] : true)
  56. );
  57. }
  58. $pool->addMethodCall('setAdminServiceIds', array($admins));
  59. $pool->addMethodCall('setAdminGroups', array($groups));
  60. $pool->addMethodCall('setAdminClasses', array($classes));
  61. //
  62. $routeLoader = $container->getDefinition('sonata.admin.route_loader');
  63. $routeLoader->addArgument($admins);
  64. }
  65. /**
  66. * Apply the default values required by the AdminInterface to the Admin service definition
  67. *
  68. * @param \Symfony\Component\DependencyInjection\Definition $definition
  69. * @param array $attributes
  70. * @return \Symfony\Component\DependencyInjection\Definition
  71. */
  72. public function applyDefaults(Definition $definition, array $attributes = array())
  73. {
  74. $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  75. $manager_type = $attributes[0]['manager_type'];
  76. if (!$definition->hasMethodCall('setModelManager')) {
  77. $definition->addMethodCall('setModelManager', array(new Reference(sprintf('sonata.admin.manager.%s', $manager_type))));
  78. }
  79. if (!$definition->hasMethodCall('setFormContractor')) {
  80. $definition->addMethodCall('setFormContractor', array(new Reference(sprintf('sonata.admin.builder.%s_form', $manager_type))));
  81. }
  82. if (!$definition->hasMethodCall('setListBuilder')) {
  83. $definition->addMethodCall('setListBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_list', $manager_type))));
  84. }
  85. if (!$definition->hasMethodCall('setDatagridBuilder')) {
  86. $definition->addMethodCall('setDatagridBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_datagrid', $manager_type))));
  87. }
  88. if (!$definition->hasMethodCall('setTranslator')) {
  89. $definition->addMethodCall('setTranslator', array(new Reference('translator')));
  90. }
  91. if (!$definition->hasMethodCall('setConfigurationPool')) {
  92. $definition->addMethodCall('setConfigurationPool', array(new Reference('sonata.admin.pool')));
  93. }
  94. if (!$definition->hasMethodCall('setRouter')) {
  95. $definition->addMethodCall('setRouter', array(new Reference('router')));
  96. }
  97. if (!$definition->hasMethodCall('setSecurityContext')) {
  98. $definition->addMethodCall('setSecurityContext', array(new Reference('security.context')));
  99. }
  100. if (!$definition->hasMethodCall('setLabel')) {
  101. $label = isset($attributes[0]['label']) ? $attributes[0]['label'] : '-';
  102. $definition->addMethodCall('setLabel', array($label));
  103. }
  104. $definition->addMethodCall('configure');
  105. return $definition;
  106. }
  107. }