diff --git a/Command/LoadSetsCommand.php b/Command/LoadSetsCommand.php index 7e0b160..ef6bef4 100644 --- a/Command/LoadSetsCommand.php +++ b/Command/LoadSetsCommand.php @@ -74,26 +74,9 @@ protected function execute(InputInterface $input, OutputInterface $output) $schemaTool->createSchema(); } - // Store all loaded references in this array, so they can be used by other FixtureSets. - $references = array(); - - foreach ($sets as $file) { - $output->write("Loading file '$file' ... "); - - // The file should return a FixtureSetInterface - $set = $this->loadSet($file); - - if (!$set || !($set instanceof FixtureSetInterface)) { - throw new \InvalidArgumentException("File '$file' does not return a FixtureSetInterface."); - } - - $entities = $manager->load($set, $references); + $fixtureSets = $this->loadFixtureSetsFromFiles($sets); - // Only reusing loaded entities. Internal references are ignored because of intended private state. - $references = array_merge($references, $entities); - - $output->writeln("loaded " . count($entities) . " entities ... done."); - } + $this->loadFixtureSets($fixtureSets, $manager, $output); } protected function loadSet($file) @@ -127,4 +110,55 @@ protected function findSetsByDefaultNaming() { // Return paths to sets. return array_keys(iterator_to_array($finder)); } + + private function loadFixtureSetsFromFiles($sets) + { + $fixtureSets = array(); + + foreach ($sets as $file) { + + // The file should return a FixtureSetInterface + $set = $this->loadSet($file); + + if (!$set || !($set instanceof FixtureSetInterface)) { + throw new \InvalidArgumentException("File '$file' does not return a FixtureSetInterface."); + } + + $fixtureSets[$file] = $set; + } + + return $this->orderFixtureSets($fixtureSets); + } + + private function orderFixtureSets($fixtureSets) + { + uasort($fixtureSets, function(FixtureSetInterface $setA, FixtureSetInterface $setB) { + $a = $setA->getOrder(); + $b = $setB->getOrder(); + if ($a == $b) { + return 0; + } + return ($a < $b) ? -1 : 1; + }); + + return $fixtureSets; + } + + private function loadFixtureSets($fixtureSets, $manager, $output) + { + // Store all loaded references in this array, so they can be used by other FixtureSets. + $references = array(); + + foreach ($fixtureSets as $file => $set) { + + $output->write("Loading file '$file' ... "); + + $entities = $manager->load($set, $references); + + // Only reusing loaded entities. Internal references are ignored because of intended private state. + $references = array_merge($references, $entities); + + $output->writeln("loaded " . count($entities) . " entities ... done."); + } + } } diff --git a/Fixtures/FixtureSet.php b/Fixtures/FixtureSet.php index 8a736ca..767fa25 100644 --- a/Fixtures/FixtureSet.php +++ b/Fixtures/FixtureSet.php @@ -41,6 +41,7 @@ public function __construct(array $options = array()) 'seed' => 1, 'do_drop' => false, 'do_persist' => true, + 'order' => 1, ); $this->options = array_merge( $defaultOptions, @@ -135,4 +136,20 @@ public function setSeed($seed) { $this->options['seed'] = is_null($seed) ? null : (integer)$seed; } + + /** + * @return int + */ + public function getOrder() + { + return $this->options['order']; + } + + /** + * @param int $order + */ + public function setOrder($order) + { + $this->options['order'] = is_null($order) ? 1 : (integer)$order; + } } diff --git a/Fixtures/FixtureSetInterface.php b/Fixtures/FixtureSetInterface.php index 91b79dc..53286e9 100644 --- a/Fixtures/FixtureSetInterface.php +++ b/Fixtures/FixtureSetInterface.php @@ -53,4 +53,9 @@ public function getLocale(); * @return int|null */ public function getSeed(); + + /** + * @return int + */ + public function getOrder(); } diff --git a/README.md b/README.md index 541021d..232b89a 100644 --- a/README.md +++ b/README.md @@ -203,6 +203,8 @@ $ php app/console h4cc_alice_fixtures:load:sets ``` The order in which the bundles are loaded is defined by the order in which they are defined in `AppKernel`. +A customization to that can be done using the "order" value in your fixture sets. +Default is '1' and lower order values will be taken first. Example for a Preconfigured FixtureSet: @@ -215,6 +217,7 @@ $set = new h4cc\AliceFixturesBundle\Fixtures\FixtureSet(array( 'seed' => 42, 'do_drop' => true, 'do_persist' => true, + 'order' => 5 )); $set->addFile(__DIR__.'/Users.yml', 'yaml'); diff --git a/Tests/Fixture/FixtureSetTest.php b/Tests/Fixture/FixtureSetTest.php index b0e9f76..4d37aac 100644 --- a/Tests/Fixture/FixtureSetTest.php +++ b/Tests/Fixture/FixtureSetTest.php @@ -40,11 +40,13 @@ public function testConstructor() 'seed' => 42, 'do_drop' => true, 'do_persist' => false, + 'order' => 1337 ) ); $this->assertEquals('de_DE', $set->getLocale()); $this->assertEquals(42, $set->getSeed()); + $this->assertEquals(1337, $set->getOrder()); $this->assertEquals(true, $set->getDoDrop()); $this->assertEquals(false, $set->getDoPersist()); $this->assertEquals(array(), $set->getFiles()); @@ -56,6 +58,7 @@ public function testSetter() $set->setLocale('de_DE'); $set->setSeed(42); + $set->setOrder(1337); $set->setDoDrop(true); $set->setDoPersist(false); $set->addFile('/foo', 'bar'); @@ -63,6 +66,7 @@ public function testSetter() $this->assertEquals('de_DE', $set->getLocale()); $this->assertEquals(42, $set->getSeed()); + $this->assertEquals(1337, $set->getOrder()); $this->assertEquals(true, $set->getDoDrop()); $this->assertEquals(false, $set->getDoPersist()); $this->assertEquals(