Skip to content

Commit

Permalink
Add seeds ordering by dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Šarūnas Norkus committed Dec 13, 2017
1 parent 454238f commit 1b0f373
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Phinx/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,58 @@ public function setSeeds(array $seeds)
return $this;
}

/**
* Get seed dependencies instances from seed dependency array
*
* @param AbstractSeed $seed Seed
*
* @return AbstractSeed[]
*/
protected function getSeedDependenciesInstances(AbstractSeed $seed)
{
if (empty($this->seeds)) {
return [];
}

$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[]
*/
protected 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 +875,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

0 comments on commit 1b0f373

Please sign in to comment.