From f04d92f42512f99faf478fb338028a0dd65552cc Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 22 Sep 2024 23:13:29 -0400 Subject: [PATCH] 4.next - Revise the migrations interfaces - Add methods that will allow symfony support to be dropped in the next major - Add better intergration with CakePHP abstractions. - Put some TODOs in for the next set of changes. --- src/Migration/Manager.php | 10 ++++++++ src/MigrationInterface.php | 47 ++++++++++++++++++++++------------ src/SeedInterface.php | 52 ++++++++++++++++++++++---------------- 3 files changed, 71 insertions(+), 38 deletions(-) diff --git a/src/Migration/Manager.php b/src/Migration/Manager.php index cb0faf18..b6a1021c 100644 --- a/src/Migration/Manager.php +++ b/src/Migration/Manager.php @@ -853,13 +853,17 @@ function ($phpFile) { $io->verbose("Constructing $class."); $config = $this->getConfig(); + // TODO Subset config and pass forward. + // Move this to the Migration/phinx shim $input = new ArrayInput([ '--plugin' => $config['plugin'] ?? null, '--source' => $config['source'] ?? null, '--connection' => $config->getConnection(), ]); + // TODO move this to the migration/phinx shim $output = new OutputAdapter($io); + // TODO constructor should take $io and $config // instantiate it $migration = new $class('default', $version, $input, $output); @@ -967,16 +971,20 @@ public function getSeeds(): array $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); foreach ($phpFiles as $filePath) { @@ -1003,6 +1011,7 @@ public function getSeeds(): array } else { $seed = new $class(); } + // TODO Replace with with setIo and setConfig $seed->setEnvironment('default'); $seed->setInput($input); $seed->setOutput($output); @@ -1027,6 +1036,7 @@ public function getSeeds(): array return []; } + // TODO remove this foreach ($this->seeds as $instance) { if (isset($input) && $instance instanceof AbstractSeed) { $instance->setInput($input); diff --git a/src/MigrationInterface.php b/src/MigrationInterface.php index 046a54db..2a8e505d 100644 --- a/src/MigrationInterface.php +++ b/src/MigrationInterface.php @@ -8,11 +8,13 @@ namespace Migrations; +use Cake\Console\ConsoleIo; use Cake\Database\Query; use Cake\Database\Query\DeleteQuery; use Cake\Database\Query\InsertQuery; use Cake\Database\Query\SelectQuery; use Cake\Database\Query\UpdateQuery; +use Migrations\Config\ConfigInterface; use Migrations\Db\Adapter\AdapterInterface; use Migrations\Db\Table; use Symfony\Component\Console\Input\InputInterface; @@ -61,32 +63,52 @@ public function setAdapter(AdapterInterface $adapter); public function getAdapter(): ?AdapterInterface; /** - * Sets the input object to be used in migration object + * Set the Console IO object to be used. * - * @param \Symfony\Component\Console\Input\InputInterface $input Input + * @param \Cake\Console\ConsoleIo $io The Io * @return $this */ - public function setInput(InputInterface $input); + public function setIo(ConsoleIo $io); /** - * Gets the input object to be used in migration object + * Get the Console IO object to be used. * - * @return \Symfony\Component\Console\Input\InputInterface|null + * @return \Cake\Console\ConsoleIo|null */ - public function getInput(): ?InputInterface; + public function getIo(): ?ConsoleIo; /** - * Sets the output object to be used in migration object + * Gets the config. * - * @param \Symfony\Component\Console\Output\OutputInterface $output Output + * @return \Migrations\Config\ConfigInterface + */ + public function getConfig(): ConfigInterface; + + /** + * Sets the config. + * + * @param \Migrations\Config\ConfigInterface $config Configuration Object * @return $this */ - public function setOutput(OutputInterface $output); + public function setConfig(ConfigInterface $config); + + /** + * Gets the input object to be used in migration object + * + * A new InputInterface will be generated each time `getOutput` is called. + * + * @return \Symfony\Component\Console\Input\InputInterface|null + * @deprecated 4.5.0 Use getIo() instead. + */ + public function getInput(): ?InputInterface; /** * Gets the output object to be used in migration object * + * A new OutputInterface will be generated each time `getOutput` is called. + * * @return \Symfony\Component\Console\Output\OutputInterface|null + * @deprecated 4.5.0 Use getIo() instead. */ public function getOutput(): ?OutputInterface; @@ -97,13 +119,6 @@ public function getOutput(): ?OutputInterface; */ public function getName(): string; - /** - * Gets the detected environment - * - * @return string - */ - public function getEnvironment(): string; - /** * Sets the migration version number. * diff --git a/src/SeedInterface.php b/src/SeedInterface.php index f4bb8d1c..d9788122 100644 --- a/src/SeedInterface.php +++ b/src/SeedInterface.php @@ -8,6 +8,8 @@ namespace Migrations; +use Cake\Console\ConsoleIo; +use Migrations\Config\ConfigInterface; use Migrations\Db\Adapter\AdapterInterface; use Migrations\Db\Table; use Symfony\Component\Console\Input\InputInterface; @@ -45,61 +47,67 @@ public function run(): void; public function getDependencies(): array; /** - * Sets the environment. + * Sets the database adapter. * + * @param \Migrations\Db\Adapter\AdapterInterface $adapter Database Adapter * @return $this */ - public function setEnvironment(string $environment); + public function setAdapter(AdapterInterface $adapter); /** - * Gets the environment. + * Gets the database adapter. * - * @return string + * @return \Migrations\Db\Adapter\AdapterInterface */ - public function getEnvironment(): string; + public function getAdapter(): AdapterInterface; /** - * Sets the database adapter. + * Set the Console IO object to be used. * - * @param \Migrations\Db\Adapter\AdapterInterface $adapter Database Adapter + * @param \Cake\Console\ConsoleIo $io The Io * @return $this */ - public function setAdapter(AdapterInterface $adapter); + public function setIo(ConsoleIo $io); /** - * Gets the database adapter. + * Get the Console IO object to be used. * - * @return \Migrations\Db\Adapter\AdapterInterface + * @return \Cake\Console\ConsoleIo|null */ - public function getAdapter(): AdapterInterface; + public function getIo(): ?ConsoleIo; /** - * Sets the input object to be used in migration object + * Gets the config. * - * @param \Symfony\Component\Console\Input\InputInterface $input Input - * @return $this + * @return \Migrations\Config\ConfigInterface */ - public function setInput(InputInterface $input); + public function getConfig(): ConfigInterface; /** - * Gets the input object to be used in migration object + * Sets the config. * - * @return \Symfony\Component\Console\Input\InputInterface + * @param \Migrations\Config\ConfigInterface $config Configuration Object + * @return $this */ - public function getInput(): InputInterface; + public function setConfig(ConfigInterface $config); /** - * Sets the output object to be used in migration object + * Gets the input object to be used in migration object * - * @param \Symfony\Component\Console\Output\OutputInterface $output Output - * @return $this + * 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 setOutput(OutputInterface $output); + 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;