ExtensionCompilerPass.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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->findDefinition($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. if (!class_exists($class)) {
  82. continue;
  83. }
  84. $classReflection = new \ReflectionClass($class);
  85. $subjectReflection = new \ReflectionClass($subject);
  86. }
  87. if ('instanceof' == $type) {
  88. if ($subjectReflection->getName() == $classReflection->getName() || $classReflection->isSubclassOf($subject)) {
  89. $extensions = array_merge($extensions, $extensionList);
  90. }
  91. }
  92. if ('implements' == $type) {
  93. if ($classReflection->implementsInterface($subject)) {
  94. $extensions = array_merge($extensions, $extensionList);
  95. }
  96. }
  97. if ('extends' == $type) {
  98. if ($classReflection->isSubclassOf($subject)) {
  99. $extensions = array_merge($extensions, $extensionList);
  100. }
  101. }
  102. }
  103. }
  104. if (isset($excludes[$id])) {
  105. $extensions = array_diff($extensions, $excludes[$id]);
  106. }
  107. return $extensions;
  108. }
  109. /**
  110. * Resolves the class argument of the admin to an actual class (in case of %parameter%)
  111. *
  112. * @param Definition $admin
  113. * @param ContainerBuilder $container
  114. * @return string
  115. */
  116. protected function getManagedClass(Definition $admin, ContainerBuilder $container)
  117. {
  118. return $container->getParameterBag()->resolveValue($admin->getArgument(1));
  119. }
  120. /**
  121. * @param array $config
  122. * @return array
  123. */
  124. protected function flattenExtensionConfiguration(array $config)
  125. {
  126. $extensionMap = array(
  127. 'excludes' => array(),
  128. 'admins' => array(),
  129. 'implements' => array(),
  130. 'extends' => array(),
  131. 'instanceof' => array(),
  132. );
  133. foreach ($config as $extension => $options) {
  134. foreach ($options as $key => $value) {
  135. foreach ($value as $source) {
  136. if (!isset($extensionMap[$key][$source])) {
  137. $extensionMap[$key][$source] = array();
  138. }
  139. array_push($extensionMap[$key][$source], $extension);
  140. }
  141. }
  142. }
  143. return $extensionMap;
  144. }
  145. }