ExtensionCompilerPass.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  16. * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
  17. */
  18. class ExtensionCompilerPass implements CompilerPassInterface
  19. {
  20. /**
  21. * {@inheritDoc}
  22. */
  23. public function process(ContainerBuilder $container)
  24. {
  25. $universalExtensions = array();
  26. foreach ($container->findTaggedServiceIds('sonata.admin.extension') as $id => $tags) {
  27. foreach ($tags as $attributes) {
  28. $target = false;
  29. if (isset($attributes['target'])) {
  30. $target = $attributes['target'];
  31. }
  32. if (isset($attributes['global']) && $attributes['global']) {
  33. $universalExtensions[] = $id;
  34. }
  35. if (!$target || !$container->hasDefinition($target)) {
  36. continue;
  37. }
  38. $container
  39. ->getDefinition($target)
  40. ->addMethodCall('addExtension', array(new Reference($id)))
  41. ;
  42. }
  43. }
  44. $extensionConfig = $container->getParameter('sonata.admin.extension.map');
  45. $extensionMap = $this->flattenExtensionConfiguration($extensionConfig);
  46. foreach ($container->findTaggedServiceIds('sonata.admin') as $id => $attributes) {
  47. $admin = $container->getDefinition($id);
  48. foreach ($universalExtensions as $extension) {
  49. $admin->addMethodCall('addExtension', array(new Reference($extension)));
  50. }
  51. $extensions = $this->getExtensionsForAdmin($id, $admin, $container, $extensionMap);
  52. foreach ($extensions as $extension) {
  53. if(!$container->hasDefinition($extension)){
  54. throw new \InvalidArgumentException(sprintf('Unable to find extension service for id %s', $extension));
  55. }
  56. $admin->addMethodCall('addExtension', array(new Reference($extension)));
  57. }
  58. }
  59. }
  60. /**
  61. * @param string $id
  62. * @param Definition $admin
  63. * @param ContainerBuilder $container
  64. * @param array $extensionMap
  65. * @return array
  66. */
  67. protected function getExtensionsForAdmin($id, Definition $admin, ContainerBuilder $container, array $extensionMap)
  68. {
  69. $extensions = array();
  70. $class = $classReflection = $subjectReflection = null;
  71. $excludes = $extensionMap['excludes'];
  72. unset($extensionMap['excludes']);
  73. foreach ($extensionMap as $type => $subjects) {
  74. foreach ($subjects as $subject => $extensionList) {
  75. if('admins' == $type){
  76. if($id == $subject){
  77. $extensions = array_merge($extensions, $extensionList);
  78. }
  79. } else {
  80. $class = $this->getManagedClass($admin, $container);
  81. $classReflection = new \ReflectionClass($class);
  82. $subjectReflection = new \ReflectionClass($subject);
  83. }
  84. if('instanceof' == $type){
  85. if($subjectReflection->getName() == $classReflection->getName() || $classReflection->isSubclassOf($subject)){
  86. $extensions = array_merge($extensions, $extensionList);
  87. }
  88. }
  89. if('implements' == $type){
  90. if($classReflection->implementsInterface($subject)){
  91. $extensions = array_merge($extensions, $extensionList);
  92. }
  93. }
  94. if('extends' == $type){
  95. if($classReflection->isSubclassOf($subject)){
  96. $extensions = array_merge($extensions, $extensionList);
  97. }
  98. }
  99. }
  100. }
  101. if(isset($excludes[$id])){
  102. $extensions = array_diff($extensions, $excludes[$id]);
  103. }
  104. return $extensions;
  105. }
  106. /**
  107. * Resolves the class argument of the admin to an actual class (in case of %parameter%)
  108. *
  109. * @param Definition $admin
  110. * @param ContainerBuilder $container
  111. * @return string
  112. */
  113. protected function getManagedClass(Definition $admin, ContainerBuilder $container)
  114. {
  115. return $container->getParameterBag()->resolveValue($admin->getArgument(1));
  116. }
  117. /**
  118. * @param array $config
  119. * @return array
  120. */
  121. protected function flattenExtensionConfiguration(array $config)
  122. {
  123. $extensionMap = array(
  124. 'excludes' => array(),
  125. 'admins' => array(),
  126. 'implements' => array(),
  127. 'extends' => array(),
  128. 'instanceof' => array(),
  129. );
  130. foreach ($config as $extension => $options) {
  131. foreach ($options as $key => $value) {
  132. foreach ($value as $source) {
  133. if(!isset($extensionMap[$key][$source])){
  134. $extensionMap[$key][$source] = array();
  135. }
  136. array_push($extensionMap[$key][$source], $extension);
  137. }
  138. }
  139. }
  140. return $extensionMap;
  141. }
  142. }