AddDependencyCallsPass.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. $settings = $this->fixSettings($container);
  30. $groups = $admins = $classes = array();
  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, $settings);
  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. $routeLoader = $container->getDefinition('sonata.admin.route_loader');
  62. $routeLoader->replaceArgument(1, $admins);
  63. }
  64. /**
  65. * Apply the default values required by the AdminInterface to the Admin service definition
  66. *
  67. * @param \Symfony\Component\DependencyInjection\Definition $definition
  68. * @param array $attributes
  69. * @return \Symfony\Component\DependencyInjection\Definition
  70. */
  71. public function applyDefaults(Definition $definition, array $attributes = array(), array $settings)
  72. {
  73. $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  74. $manager_type = $attributes[0]['manager_type'];
  75. if (!$definition->hasMethodCall('setModelManager')) {
  76. $definition->addMethodCall('setModelManager', array(new Reference(sprintf('sonata.admin.manager.%s', $manager_type))));
  77. }
  78. if (!$definition->hasMethodCall('setFormContractor')) {
  79. $definition->addMethodCall('setFormContractor', array(new Reference(sprintf('sonata.admin.builder.%s_form', $manager_type))));
  80. }
  81. if (!$definition->hasMethodCall('setViewBuilder')) {
  82. $definition->addMethodCall('setViewBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_view', $manager_type))));
  83. }
  84. if (!$definition->hasMethodCall('setListBuilder')) {
  85. $definition->addMethodCall('setListBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_list', $manager_type))));
  86. }
  87. if (!$definition->hasMethodCall('setDatagridBuilder')) {
  88. $definition->addMethodCall('setDatagridBuilder', array(new Reference(sprintf('sonata.admin.builder.%s_datagrid', $manager_type))));
  89. }
  90. if (!$definition->hasMethodCall('setTranslator')) {
  91. $definition->addMethodCall('setTranslator', array(new Reference('translator')));
  92. }
  93. if (!$definition->hasMethodCall('setConfigurationPool')) {
  94. $definition->addMethodCall('setConfigurationPool', array(new Reference('sonata.admin.pool')));
  95. }
  96. if (!$definition->hasMethodCall('setRouter')) {
  97. $definition->addMethodCall('setRouter', array(new Reference('router')));
  98. }
  99. if (!$definition->hasMethodCall('setSecurityHandler')) {
  100. $definition->addMethodCall('setSecurityHandler', array(new Reference($settings['security_handler'])));
  101. }
  102. if (!$definition->hasMethodCall('setLabel')) {
  103. $label = isset($attributes[0]['label']) ? $attributes[0]['label'] : '-';
  104. $definition->addMethodCall('setLabel', array($label));
  105. }
  106. $definition->addMethodCall('configure');
  107. return $definition;
  108. }
  109. /**
  110. * @param ContainerBuilder $container
  111. * @return array
  112. */
  113. public function fixSettings(ContainerBuilder $container)
  114. {
  115. $pool = $container->getDefinition('sonata.admin.pool');
  116. // not very clean but don't know how to do that for now
  117. $settings = false;
  118. $methods = $pool->getMethodCalls();
  119. foreach ($methods as $pos => $calls) {
  120. if ($calls[0] == '__hack__') {
  121. $settings = $calls[1];
  122. break;
  123. }
  124. }
  125. if ($settings) {
  126. unset($methods[$pos]);
  127. }
  128. $pool->setMethodCalls($methods);
  129. return $settings;
  130. }
  131. }