SymfonyFixturesLoader.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\Bundle\FixturesBundle\Loader;
  4. use Doctrine\Bundle\FixturesBundle\DependencyInjection\CompilerPass\FixturesCompilerPass;
  5. use Doctrine\Bundle\FixturesBundle\FixtureGroupInterface;
  6. use Doctrine\Common\DataFixtures\DependentFixtureInterface;
  7. use Doctrine\Common\DataFixtures\FixtureInterface;
  8. use LogicException;
  9. use ReflectionClass;
  10. use RuntimeException;
  11. use Symfony\Bridge\Doctrine\DataFixtures\ContainerAwareLoader;
  12. use function array_key_exists;
  13. use function array_values;
  14. use function get_class;
  15. use function sprintf;
  16. final class SymfonyFixturesLoader extends ContainerAwareLoader
  17. {
  18. /** @var FixtureInterface[] */
  19. private $loadedFixtures = [];
  20. /** @var array<string, array<string, bool>> */
  21. private $groupsFixtureMapping = [];
  22. /**
  23. * @internal
  24. */
  25. public function addFixtures(array $fixtures) : void
  26. {
  27. // Because parent::addFixture may call $this->createFixture
  28. // we cannot call $this->addFixture in this loop
  29. foreach ($fixtures as $fixture) {
  30. $class = get_class($fixture['fixture']);
  31. $this->loadedFixtures[$class] = $fixture['fixture'];
  32. $this->addGroupsFixtureMapping($class, $fixture['groups']);
  33. }
  34. // Now that all fixtures are in the $this->loadedFixtures array,
  35. // it is safe to call $this->addFixture in this loop
  36. foreach ($this->loadedFixtures as $fixture) {
  37. $this->addFixture($fixture);
  38. }
  39. }
  40. public function addFixture(FixtureInterface $fixture) : void
  41. {
  42. $class = get_class($fixture);
  43. $this->loadedFixtures[$class] = $fixture;
  44. $reflection = new ReflectionClass($fixture);
  45. $this->addGroupsFixtureMapping($class, [$reflection->getShortName()]);
  46. if ($fixture instanceof FixtureGroupInterface) {
  47. $this->addGroupsFixtureMapping($class, $fixture::getGroups());
  48. }
  49. parent::addFixture($fixture);
  50. }
  51. /**
  52. * Overridden to not allow new fixture classes to be instantiated.
  53. *
  54. * @param string $class
  55. */
  56. protected function createFixture($class) : FixtureInterface
  57. {
  58. /*
  59. * We don't actually need to create the fixture. We just
  60. * return the one that already exists.
  61. */
  62. if (! isset($this->loadedFixtures[$class])) {
  63. throw new LogicException(sprintf(
  64. 'The "%s" fixture class is trying to be loaded, but is not available. Make sure this class is defined as a service and tagged with "%s".',
  65. $class,
  66. FixturesCompilerPass::FIXTURE_TAG
  67. ));
  68. }
  69. return $this->loadedFixtures[$class];
  70. }
  71. /**
  72. * Returns the array of data fixtures to execute.
  73. *
  74. * @param string[] $groups
  75. *
  76. * @return FixtureInterface[]
  77. */
  78. public function getFixtures(array $groups = []) : array
  79. {
  80. $fixtures = parent::getFixtures();
  81. if (empty($groups)) {
  82. return $fixtures;
  83. }
  84. $filteredFixtures = [];
  85. foreach ($fixtures as $fixture) {
  86. foreach ($groups as $group) {
  87. $fixtureClass = get_class($fixture);
  88. if (isset($this->groupsFixtureMapping[$group][$fixtureClass])) {
  89. $filteredFixtures[$fixtureClass] = $fixture;
  90. continue 2;
  91. }
  92. }
  93. }
  94. foreach ($filteredFixtures as $fixture) {
  95. $this->validateDependencies($filteredFixtures, $fixture);
  96. }
  97. return array_values($filteredFixtures);
  98. }
  99. /**
  100. * Generates an array of the groups and their fixtures
  101. *
  102. * @param string[] $groups
  103. */
  104. private function addGroupsFixtureMapping(string $className, array $groups) : void
  105. {
  106. foreach ($groups as $group) {
  107. $this->groupsFixtureMapping[$group][$className] = true;
  108. }
  109. }
  110. /**
  111. * @param string[] $fixtures An array of fixtures with class names as keys
  112. *
  113. * @throws RuntimeException
  114. */
  115. private function validateDependencies(array $fixtures, FixtureInterface $fixture) : void
  116. {
  117. if (! $fixture instanceof DependentFixtureInterface) {
  118. return;
  119. }
  120. $dependenciesClasses = $fixture->getDependencies();
  121. foreach ($dependenciesClasses as $class) {
  122. if (! array_key_exists($class, $fixtures)) {
  123. throw new RuntimeException(sprintf('Fixture "%s" was declared as a dependency for fixture "%s", but it was not included in any of the loaded fixture groups.', $class, get_class($fixture)));
  124. }
  125. }
  126. }
  127. }