From fb5d54db4ffa23abb9d39d885e4453ed91c98a52 Mon Sep 17 00:00:00 2001 From: Martin Brettschneider Date: Tue, 23 Aug 2022 12:36:27 +0200 Subject: [PATCH] Pass environment to seed from manager. --- src/Phinx/Migration/Manager.php | 6 ++++-- src/Phinx/Seed/AbstractSeed.php | 23 +++++++++++++++++++++++ src/Phinx/Seed/SeedInterface.php | 14 ++++++++++++++ tests/Phinx/Migration/ManagerTest.php | 6 +++--- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/Phinx/Migration/Manager.php b/src/Phinx/Migration/Manager.php index d1e735536..2aa1498af 100644 --- a/src/Phinx/Migration/Manager.php +++ b/src/Phinx/Migration/Manager.php @@ -602,7 +602,7 @@ public function rollback(string $environment, $target = null, bool $force = fals */ public function seed(string $environment, ?string $seed = null): void { - $seeds = $this->getSeeds(); + $seeds = $this->getSeeds($environment); if ($seed === null) { // run all seeders @@ -918,10 +918,11 @@ protected function orderSeedsByDependencies(array $seeds): array /** * Gets an array of database seeders. * + * @param string $environment Environment * @throws \InvalidArgumentException * @return \Phinx\Seed\SeedInterface[] */ - public function getSeeds(): array + public function getSeeds(string $environment): array { if ($this->seeds === null) { $phpFiles = $this->getSeedFiles(); @@ -958,6 +959,7 @@ public function getSeeds(): array } else { $seed = new $class(); } + $seed->setEnvironment($environment); $input = $this->getInput(); if ($input !== null) { $seed->setInput($input); diff --git a/src/Phinx/Seed/AbstractSeed.php b/src/Phinx/Seed/AbstractSeed.php index 988c1deff..7706d3013 100644 --- a/src/Phinx/Seed/AbstractSeed.php +++ b/src/Phinx/Seed/AbstractSeed.php @@ -24,6 +24,11 @@ */ abstract class AbstractSeed implements SeedInterface { + /** + * @var string + */ + protected $environment; + /** * @var \Phinx\Db\Adapter\AdapterInterface */ @@ -61,6 +66,24 @@ public function getDependencies(): array return []; } + /** + * @inheritDoc + */ + public function setEnvironment(string $environment) + { + $this->environment = $environment; + + return $this; + } + + /** + * @inheritDoc + */ + public function getEnvironment(): string + { + return $this->environment; + } + /** * @inheritDoc */ diff --git a/src/Phinx/Seed/SeedInterface.php b/src/Phinx/Seed/SeedInterface.php index 6e4686e42..6f5afc58f 100644 --- a/src/Phinx/Seed/SeedInterface.php +++ b/src/Phinx/Seed/SeedInterface.php @@ -42,6 +42,20 @@ public function run(): void; */ public function getDependencies(): array; + /** + * Sets the environment. + * + * @return $this + */ + public function setEnvironment(string $environment); + + /** + * Gets the environment. + * + * @return string + */ + public function getEnvironment(): string; + /** * Sets the database adapter. * diff --git a/tests/Phinx/Migration/ManagerTest.php b/tests/Phinx/Migration/ManagerTest.php index 9be62fe2a..cc2cc9df4 100644 --- a/tests/Phinx/Migration/ManagerTest.php +++ b/tests/Phinx/Migration/ManagerTest.php @@ -5450,7 +5450,7 @@ public function testExecuteANonExistentSeedWorksAsExpectedWithMixedNamespace() public function testOrderSeeds() { - $seeds = array_values($this->manager->getSeeds()); + $seeds = array_values($this->manager->getSeeds('mockenv')); $this->assertInstanceOf('UserSeeder', $seeds[0]); $this->assertInstanceOf('GSeeder', $seeds[1]); $this->assertInstanceOf('PostSeeder', $seeds[2]); @@ -5472,7 +5472,7 @@ public function testSeedWillNotBeExecuted() public function testGettingInputObject() { $migrations = $this->manager->getMigrations('mockenv'); - $seeds = $this->manager->getSeeds(); + $seeds = $this->manager->getSeeds('mockenv'); $inputObject = $this->manager->getInput(); $this->assertInstanceOf('\Symfony\Component\Console\Input\InputInterface', $inputObject); @@ -5487,7 +5487,7 @@ public function testGettingInputObject() public function testGettingOutputObject() { $migrations = $this->manager->getMigrations('mockenv'); - $seeds = $this->manager->getSeeds(); + $seeds = $this->manager->getSeeds('mockenv'); $outputObject = $this->manager->getOutput(); $this->assertInstanceOf('\Symfony\Component\Console\Output\OutputInterface', $outputObject);