AddDependencyCallsCompilerPass.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
  17. /**
  18. * Add all dependencies to the Admin class, this avoid to write to many lines
  19. * in the configuration files.
  20. *
  21. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  22. */
  23. class AddDependencyCallsCompilerPass implements CompilerPassInterface
  24. {
  25. /**
  26. * {@inheritDoc}
  27. */
  28. public function process(ContainerBuilder $container)
  29. {
  30. $settings = $this->fixSettings($container);
  31. $groups = $admins = $classes = array();
  32. $pool = $container->getDefinition('sonata.admin.pool');
  33. foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
  34. $definition = $container->getDefinition($id);
  35. $arguments = $definition->getArguments();
  36. if (strlen($arguments[0]) == 0) {
  37. $definition->replaceArgument(0, $id);
  38. }
  39. if (strlen($arguments[2]) == 0) {
  40. $definition->replaceArgument(2, 'SonataAdminBundle:CRUD');
  41. }
  42. $this->applyDefaults($container, $id, $attributes, $settings);
  43. $arguments = $definition->getArguments();
  44. if (preg_match('/%(.*)%/', $arguments[1], $matches)) {
  45. $class = $container->getParameter($matches[1]);
  46. } else {
  47. $class = $arguments[1];
  48. }
  49. $admins[] = $id;
  50. $classes[$class] = $id;
  51. $group_name = isset($attributes[0]['group']) ? $attributes[0]['group'] : 'default';
  52. if (!isset($groupDefaults[$group_name])) {
  53. $groupDefaults[$group_name] = array(
  54. 'label' => $group_name
  55. );
  56. }
  57. $groupDefaults[$group_name]['items'][] = $id;
  58. }
  59. if (isset($settings['dashboard_groups'])) {
  60. $groups = $settings['dashboard_groups'];
  61. foreach ($groups as $group_name => $group) {
  62. if(empty($group['items'])) {
  63. $groups[$group_name]['items'] = $groupDefaults[$group_name]['items'];
  64. }
  65. if(empty($group['label'])) {
  66. $groups[$group_name]['label'] = $groupDefaults[$group_name]['label'];
  67. }
  68. }
  69. }
  70. else {
  71. $groups = $groupDefaults;
  72. }
  73. $pool->addMethodCall('setAdminServiceIds', array($admins));
  74. $pool->addMethodCall('setAdminGroups', array($groups));
  75. $pool->addMethodCall('setAdminClasses', array($classes));
  76. $routeLoader = $container->getDefinition('sonata.admin.route_loader');
  77. $routeLoader->replaceArgument(1, $admins);
  78. }
  79. public function fixSettings($container)
  80. {
  81. $pool = $container->getDefinition('sonata.admin.pool');
  82. // not very clean but don't know how to do that for now
  83. $settings = false;
  84. $methods = $pool->getMethodCalls();
  85. foreach ($methods as $pos => $calls) {
  86. if ($calls[0] == '__hack__') {
  87. $settings = $calls[1];
  88. break;
  89. }
  90. }
  91. if ($settings) {
  92. unset($methods[$pos]);
  93. }
  94. $pool->setMethodCalls($methods);
  95. return $settings;
  96. }
  97. /**
  98. * Apply the default values required by the AdminInterface to the Admin service definition
  99. *
  100. * @param ContainerBuilder $container
  101. * @param interger $serviceId
  102. * @param array $attributes
  103. * @param array $settings
  104. * @return \Symfony\Component\DependencyInjection\Definition
  105. */
  106. public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = array(), array $settings = array())
  107. {
  108. $definition = $container->getDefinition($serviceId);
  109. $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  110. $manager_type = $attributes[0]['manager_type'];
  111. $addServices = isset($settings['admin_services'][$serviceId]) ? $settings['admin_services'][$serviceId] : false;
  112. $defaultAddServices = array(
  113. 'model_manager' => sprintf('sonata.admin.manager.%s', $manager_type),
  114. 'form_contractor' => sprintf('sonata.admin.builder.%s_form', $manager_type),
  115. 'show_builder' => sprintf('sonata.admin.builder.%s_show', $manager_type),
  116. 'list_builder' => sprintf('sonata.admin.builder.%s_list', $manager_type),
  117. 'datagrid_builder' => sprintf('sonata.admin.builder.%s_datagrid', $manager_type),
  118. 'translator' => 'translator',
  119. 'configuration_pool' => 'sonata.admin.pool',
  120. 'router' => 'router',
  121. 'validator' => 'validator',
  122. 'security_handler' => 'sonata.admin.security.handler'
  123. );
  124. foreach ($defaultAddServices as $attr => $addServiceId) {
  125. $method = 'set'.$this->camelize($attr);
  126. if(isset($addServices[$attr]) || !$definition->hasMethodCall($method)) {
  127. $definition->addMethodCall($method, array(new Reference(isset($addServices[$attr]) ? $addServices[$attr] : $addServiceId)));
  128. }
  129. }
  130. if (isset($service['label'])) {
  131. $label = $service['label'];
  132. } elseif (isset($attributes[0]['label'])) {
  133. $label = $attributes[0]['label'];
  134. } else {
  135. $label = '-';
  136. }
  137. $definition->addMethodCall('setLabel', array($label));
  138. $definition->addMethodCall('configure');
  139. return $definition;
  140. }
  141. /**
  142. * method taken from PropertyPath
  143. *
  144. * @param $property
  145. * @return mixed
  146. */
  147. protected function camelize($property)
  148. {
  149. return preg_replace(array('/(^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  150. }
  151. }