AddDependencyCallsCompilerPass.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 = $groupDefaults = $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. $showInDashBord = (boolean)(isset($attributes[0]['show_in_dashboard']) ? $attributes[0]['show_in_dashboard'] : true);
  52. if (!$showInDashBord) {
  53. continue;
  54. }
  55. $group_name = isset($attributes[0]['group']) ? $attributes[0]['group'] : 'default';
  56. if (!isset($groupDefaults[$group_name])) {
  57. $groupDefaults[$group_name] = array(
  58. 'label' => $group_name
  59. );
  60. }
  61. $groupDefaults[$group_name]['items'][] = $id;
  62. }
  63. if (!empty($settings['dashboard_groups'])) {
  64. $groups = $settings['dashboard_groups'];
  65. foreach ($groups as $group_name => $group) {
  66. if (!isset($groupDefaults[$group_name])) {
  67. $groupDefaults[$group_name] = array(
  68. 'items' => array(),
  69. 'label' => $group_name
  70. );
  71. }
  72. if (empty($group['items'])) {
  73. $groups[$group_name]['items'] = $groupDefaults[$group_name]['items'];
  74. }
  75. if (empty($group['label'])) {
  76. $groups[$group_name]['label'] = $groupDefaults[$group_name]['label'];
  77. }
  78. if (!empty($groups[$group_name]['item_adds'])) {
  79. $groups[$group_name]['items'] = array_merge($groupDefaults[$group_name]['items'], $groups[$group_name]['item_adds']);
  80. }
  81. }
  82. }
  83. else {
  84. $groups = $groupDefaults;
  85. }
  86. $pool->addMethodCall('setAdminServiceIds', array($admins));
  87. $pool->addMethodCall('setAdminGroups', array($groups));
  88. $pool->addMethodCall('setAdminClasses', array($classes));
  89. $routeLoader = $container->getDefinition('sonata.admin.route_loader');
  90. $routeLoader->replaceArgument(1, $admins);
  91. }
  92. public function fixSettings($container)
  93. {
  94. $pool = $container->getDefinition('sonata.admin.pool');
  95. // not very clean but don't know how to do that for now
  96. $settings = false;
  97. $methods = $pool->getMethodCalls();
  98. foreach ($methods as $pos => $calls) {
  99. if ($calls[0] == '__hack__') {
  100. $settings = $calls[1];
  101. break;
  102. }
  103. }
  104. if ($settings) {
  105. unset($methods[$pos]);
  106. }
  107. $pool->setMethodCalls($methods);
  108. return $settings;
  109. }
  110. /**
  111. * Apply the default values required by the AdminInterface to the Admin service definition
  112. *
  113. * @param ContainerBuilder $container
  114. * @param interger $serviceId
  115. * @param array $attributes
  116. * @param array $settings
  117. * @return \Symfony\Component\DependencyInjection\Definition
  118. */
  119. public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = array(), array $settings = array())
  120. {
  121. $definition = $container->getDefinition($serviceId);
  122. $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
  123. $manager_type = $attributes[0]['manager_type'];
  124. $addServices = isset($settings['admin_services'][$serviceId]) ? $settings['admin_services'][$serviceId] : false;
  125. $defaultAddServices = array(
  126. 'model_manager' => sprintf('sonata.admin.manager.%s', $manager_type),
  127. 'form_contractor' => sprintf('sonata.admin.builder.%s_form', $manager_type),
  128. 'show_builder' => sprintf('sonata.admin.builder.%s_show', $manager_type),
  129. 'list_builder' => sprintf('sonata.admin.builder.%s_list', $manager_type),
  130. 'datagrid_builder' => sprintf('sonata.admin.builder.%s_datagrid', $manager_type),
  131. 'translator' => 'translator',
  132. 'configuration_pool' => 'sonata.admin.pool',
  133. 'router' => 'router',
  134. 'validator' => 'validator',
  135. 'security_handler' => 'sonata.admin.security.handler'
  136. );
  137. foreach ($defaultAddServices as $attr => $addServiceId) {
  138. $method = 'set'.$this->camelize($attr);
  139. if (isset($addServices[$attr]) || !$definition->hasMethodCall($method)) {
  140. $definition->addMethodCall($method, array(new Reference(isset($addServices[$attr]) ? $addServices[$attr] : $addServiceId)));
  141. }
  142. }
  143. if (isset($service['label'])) {
  144. $label = $service['label'];
  145. } elseif (isset($attributes[0]['label'])) {
  146. $label = $attributes[0]['label'];
  147. } else {
  148. $label = '-';
  149. }
  150. $definition->addMethodCall('setLabel', array($label));
  151. $definition->addMethodCall('configure');
  152. return $definition;
  153. }
  154. /**
  155. * method taken from PropertyPath
  156. *
  157. * @param $property
  158. * @return mixed
  159. */
  160. protected function camelize($property)
  161. {
  162. return preg_replace(array('/(^|_)+(.)/e', '/\.(.)/e'), array("strtoupper('\\2')", "'_'.strtoupper('\\1')"), $property);
  163. }
  164. }