Skip to content

Commit

Permalink
Merge pull request #1244 from emptyhand/feature-ordered-seeds
Browse files Browse the repository at this point in the history
Add ordered seeds feature
  • Loading branch information
chinpei215 authored Dec 20, 2017
2 parents 7072a54 + 00ffb18 commit f0864db
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ before_script:
script:
- if [[ $DEFAULT = 1 ]]; then vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover; fi
- if [[ $PHPSTAN = 1 ]]; then composer require --dev phpstan/phpstan:^0.8 && vendor/bin/phpstan analyse -c phpstan.neon -l 4 src; fi
- if [[ $PHPCS = 1 ]]; then composer require --dev squizlabs/php_codesniffer:^3.0 && vendor/bin/phpcs -p -s src; fi
- if [[ $PHPCS = 1 ]]; then composer require --dev squizlabs/php_codesniffer:~3.1.1 && vendor/bin/phpcs -p -s src; fi

after_success:
- if [[ $DEFAULT = 1 ]]; then bash <(curl -s https://codecov.io/bash); fi
Expand Down
49 changes: 49 additions & 0 deletions src/Phinx/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,54 @@ public function setSeeds(array $seeds)
return $this;
}

/**
* Get seed dependencies instances from seed dependency array
*
* @param AbstractSeed $seed Seed
*
* @return AbstractSeed[]
*/
private function getSeedDependenciesInstances(AbstractSeed $seed)
{
$dependenciesInstances = [];
$dependencies = $seed->getDependencies();
if (!empty($dependencies)) {
foreach ($dependencies as $dependency) {
foreach ($this->seeds as $seed) {
if (get_class($seed) === $dependency) {
$dependenciesInstances[get_class($seed)] = $seed;
}
}
}
}

return $dependenciesInstances;
}

/**
* Order seeds by dependencies
*
* @param AbstractSeed[] $seeds Seeds
*
* @return AbstractSeed[]
*/
private function orderSeedsByDependencies(array $seeds)
{
$orderedSeeds = [];
foreach ($seeds as $seed) {
$key = get_class($seed);
$dependencies = $this->getSeedDependenciesInstances($seed);
if (!empty($dependencies)) {
$orderedSeeds[$key] = $seed;
$orderedSeeds = array_merge($this->orderSeedsByDependencies($dependencies), $orderedSeeds);
} else {
$orderedSeeds[$key] = $seed;
}
}

return $orderedSeeds;
}

/**
* Gets an array of database seeders.
*
Expand Down Expand Up @@ -823,6 +871,7 @@ public function getSeeds()
$this->setSeeds($seeds);
}

$this->seeds = $this->orderSeedsByDependencies($this->seeds);
return $this->seeds;
}

Expand Down
10 changes: 10 additions & 0 deletions src/Phinx/Seed/AbstractSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function run()
{
}

/**
* Return seeds dependencies.
*
* @return array
*/
public function getDependencies()
{
return [];
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Phinx/Migration/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5413,6 +5413,14 @@ public function testExecuteANonExistentSeedWorksAsExpectedWithMixedNamespace()
$this->assertContains('Foo\Bar\UserSeeder', $output);
}

public function testOrderSeeds()
{
$seeds = array_values($this->manager->getSeeds());
$this->assertInstanceOf('UserSeeder', $seeds[0]);
$this->assertInstanceOf('GSeeder', $seeds[1]);
$this->assertInstanceOf('PostSeeder', $seeds[2]);
}

public function testGettingInputObject()
{
$migrations = $this->manager->getMigrations();
Expand Down
8 changes: 8 additions & 0 deletions tests/Phinx/Migration/_files/seeds/PostSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public function run()
$posts->insert($data)
->save();
}

public function getDependencies()
{
return [
'UserSeeder',
'GSeeder',
];
}
}

0 comments on commit f0864db

Please sign in to comment.