Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add and use a ShimAdapter #751

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions src/Migration/Environment.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
use Migrations\Db\Adapter\AdapterFactory;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Db\Adapter\PhinxAdapter;
use Migrations\SeedInterface;
use Phinx\Migration\MigrationInterface;
use Phinx\Seed\SeedInterface;
use RuntimeException;

class Environment
Expand Down Expand Up @@ -131,15 +131,13 @@ public function executeMigration(MigrationInterface $migration, string $directio
/**
* Executes the specified seeder on this environment.
*
* @param \Phinx\Seed\SeedInterface $seed Seed
* @param \Migrations\SeedInterface $seed Seed
* @return void
*/
public function executeSeed(SeedInterface $seed): void
{
$adapter = $this->getAdapter();
$phinxAdapter = new PhinxAdapter($adapter);

$seed->setAdapter($phinxAdapter);
$seed->setAdapter($adapter);
if (method_exists($seed, SeedInterface::INIT)) {
$seed->{SeedInterface::INIT}();
}
Expand Down
78 changes: 27 additions & 51 deletions src/Migration/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
use Exception;
use InvalidArgumentException;
use Migrations\Config\ConfigInterface;
use Migrations\SeedInterface;
use Migrations\Shim\OutputAdapter;
use Migrations\Shim\SeedAdapter;
use Phinx\Migration\AbstractMigration;
use Phinx\Migration\MigrationInterface;
use Phinx\Seed\AbstractSeed;
use Phinx\Seed\SeedInterface;
use Phinx\Seed\SeedInterface as PhinxSeedInterface;
use Phinx\Util\Util;
use Psr\Container\ContainerInterface;
use RuntimeException;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;

class Manager
{
Expand Down Expand Up @@ -53,7 +52,7 @@ class Manager
protected ?array $migrations = null;

/**
* @var \Phinx\Seed\SeedInterface[]|null
* @var \Migrations\SeedInterface[]|null
*/
protected ?array $seeds = null;

Expand Down Expand Up @@ -475,7 +474,7 @@ public function executeMigration(MigrationInterface $migration, string $directio
/**
* Execute a seeder against the specified environment.
*
* @param \Phinx\Seed\SeedInterface $seed Seed
* @param \Migrations\SeedInterface $seed Seed
* @return void
*/
public function executeSeed(SeedInterface $seed): void
Expand Down Expand Up @@ -523,7 +522,7 @@ protected function printMigrationStatus(MigrationInterface $migration, string $s
/**
* Print Seed Status
*
* @param \Phinx\Seed\SeedInterface $seed Seed
* @param \Migrations\SeedInterface $seed Seed
* @param string $status Status of the seed
* @param string|null $duration Duration the seed took the be executed
* @return void
Expand Down Expand Up @@ -901,7 +900,7 @@ protected function getMigrationFiles(): array
/**
* Sets the database seeders.
*
* @param \Phinx\Seed\SeedInterface[] $seeds Seeders
* @param \Migrations\SeedInterface[] $seeds Seeders
* @return $this
*/
public function setSeeds(array $seeds)
Expand All @@ -914,8 +913,8 @@ public function setSeeds(array $seeds)
/**
* Get seed dependencies instances from seed dependency array
*
* @param \Phinx\Seed\SeedInterface $seed Seed
* @return \Phinx\Seed\SeedInterface[]
* @param \Migrations\SeedInterface $seed Seed
* @return \Migrations\SeedInterface[]
*/
protected function getSeedDependenciesInstances(SeedInterface $seed): array
{
Expand All @@ -924,8 +923,9 @@ protected function getSeedDependenciesInstances(SeedInterface $seed): array
if (!empty($dependencies) && !empty($this->seeds)) {
foreach ($dependencies as $dependency) {
foreach ($this->seeds as $seed) {
if (get_class($seed) === $dependency) {
$dependenciesInstances[get_class($seed)] = $seed;
$name = $seed->getName();
if ($name === $dependency) {
$dependenciesInstances[$name] = $seed;
}
}
}
Expand All @@ -937,14 +937,15 @@ protected function getSeedDependenciesInstances(SeedInterface $seed): array
/**
* Order seeds by dependencies
*
* @param \Phinx\Seed\SeedInterface[] $seeds Seeds
* @return \Phinx\Seed\SeedInterface[]
* @param \Migrations\SeedInterface[] $seeds Seeds
* @return \Migrations\SeedInterface[]
*/
protected function orderSeedsByDependencies(array $seeds): array
{
$orderedSeeds = [];
foreach ($seeds as $seed) {
$orderedSeeds[get_class($seed)] = $seed;
$name = $seed->getName();
$orderedSeeds[$name] = $seed;
$dependencies = $this->getSeedDependenciesInstances($seed);
if (!empty($dependencies)) {
$orderedSeeds = array_merge($this->orderSeedsByDependencies($dependencies), $orderedSeeds);
Expand All @@ -958,7 +959,7 @@ protected function orderSeedsByDependencies(array $seeds): array
* Gets an array of database seeders.
*
* @throws \InvalidArgumentException
* @return \Phinx\Seed\SeedInterface[]
* @return \Migrations\SeedInterface[]
*/
public function getSeeds(): array
{
Expand All @@ -967,25 +968,11 @@ public function getSeeds(): array

// filter the files to only get the ones that match our naming scheme
$fileNames = [];
/** @var \Phinx\Seed\SeedInterface[] $seeds */
/** @var \Migrations\SeedInterface[] $seeds */
$seeds = [];

$config = $this->getConfig();
// TODO Subset config and pass forward.
// TODO move this to the migration/phinx shim
$optionDef = new InputDefinition([
new InputOption('plugin', mode: InputOption::VALUE_OPTIONAL, default: ''),
new InputOption('connection', mode: InputOption::VALUE_OPTIONAL, default: ''),
new InputOption('source', mode: InputOption::VALUE_OPTIONAL, default: ''),
]);
// TODO move this to the migration/phinx shim
$input = new ArrayInput([
'--plugin' => $config['plugin'] ?? null,
'--source' => $config['source'] ?? null,
'--connection' => $config->getConnection(),
], $optionDef);
// TODO move this to the migration/phinx shim
$output = new OutputAdapter($this->io);
$io = $this->getIo();

foreach ($phpFiles as $filePath) {
if (Util::isValidSeedFileName(basename($filePath))) {
Expand All @@ -1005,24 +992,20 @@ public function getSeeds(): array
}

// instantiate it
/** @var \Phinx\Seed\AbstractSeed $seed */
/** @var \Phinx\Seed\AbstractSeed|\Migrations\SeedInterface $seed */
if (isset($this->container)) {
$seed = $this->container->get($class);
} else {
$seed = new $class();
}
// TODO Replace with with setIo and setConfig
$seed->setEnvironment('default');
$seed->setInput($input);
$seed->setOutput($output);

if (!($seed instanceof AbstractSeed)) {
throw new InvalidArgumentException(sprintf(
'The class "%s" in file "%s" must extend \Phinx\Seed\AbstractSeed',
$class,
$filePath
));
// Shim phinx seeds so that the rest of migrations
// can be isolated from phinx.
if ($seed instanceof PhinxSeedInterface) {
$seed = new SeedAdapter($seed);
}
/** @var \Migrations\SeedInterface $seed */
$seed->setIo($io);
$seed->setConfig($config);

$seeds[$class] = $seed;
}
Expand All @@ -1036,13 +1019,6 @@ public function getSeeds(): array
return [];
}

// TODO remove this
foreach ($this->seeds as $instance) {
if (isset($input) && $instance instanceof AbstractSeed) {
$instance->setInput($input);
}
}

return $this->seeds;
}

Expand Down
36 changes: 12 additions & 24 deletions src/SeedInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
use Migrations\Config\ConfigInterface;
use Migrations\Db\Adapter\AdapterInterface;
use Migrations\Db\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Seed interface
Expand Down Expand Up @@ -79,9 +77,9 @@ public function getIo(): ?ConsoleIo;
/**
* Gets the config.
*
* @return \Migrations\Config\ConfigInterface
* @return \Migrations\Config\ConfigInterface|null
*/
public function getConfig(): ConfigInterface;
public function getConfig(): ?ConfigInterface;

/**
* Sets the config.
Expand All @@ -91,26 +89,6 @@ public function getConfig(): ConfigInterface;
*/
public function setConfig(ConfigInterface $config);

/**
* Gets the input object to be used in migration object
*
* A new InputInteface will be generated each time `getOutput` is called.
*
* @return \Symfony\Component\Console\Input\InputInterface
* @deprecated 4.5.0 Use getIo() instead.
*/
public function getInput(): InputInterface;

/**
* Gets the output object to be used in migration object
*
* A new OutputInteface will be generated each time `getOutput` is called.
*
* @return \Symfony\Component\Console\Output\OutputInterface
* @deprecated 4.5.0 Use getIo() instead.
*/
public function getOutput(): OutputInterface;

/**
* Gets the name.
*
Expand Down Expand Up @@ -195,4 +173,14 @@ public function table(string $tableName, array $options): Table;
* @return bool
*/
public function shouldExecute(): bool;

/**
* Gives the ability to a seeder to call another seeder.
* This is particularly useful if you need to run the seeders of your applications in a specific sequences,
* for instance to respect foreign key constraints.
*
* @param string $seeder Name of the seeder to call from the current seed
* @return void
*/
public function call(string $seeder): void;
}
Loading
Loading