From 994cf958b8ef96b0a51a23741cbfd80c61a48707 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 4 Feb 2024 22:52:15 -0500 Subject: [PATCH 1/2] Add config switch for backend selection. Replace a clunky static variable with a method and deprecate the class. I was considering using this to splice in the new commands, but after thinking some more, it would be easier to replace the commands entirely. --- src/Command/MigrationsCommand.php | 10 ++++----- src/Migrations.php | 2 ++ src/MigrationsDispatcher.php | 35 ++++++++++++++++++------------- src/MigrationsPlugin.php | 17 +++++++++++++++ 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/src/Command/MigrationsCommand.php b/src/Command/MigrationsCommand.php index d9fb5626..47f5e5cd 100644 --- a/src/Command/MigrationsCommand.php +++ b/src/Command/MigrationsCommand.php @@ -51,9 +51,9 @@ public static function defaultName(): string if (parent::defaultName() === 'migrations') { return 'migrations'; } - // TODO this will need to be patched. - $command = new MigrationsDispatcher::$phinxCommands[static::$commandName](); - $name = $command->getName(); + $className = MigrationsDispatcher::getCommands()[static::$commandName]; + $command = new $className(); + $name = (string)$command->getName(); return 'migrations ' . $name; } @@ -78,8 +78,8 @@ public function getOptionParser(): ConsoleOptionParser return parent::getOptionParser(); } $parser = parent::getOptionParser(); - // Use new methods - $command = new MigrationsDispatcher::$phinxCommands[static::$commandName](); + $className = MigrationsDispatcher::getCommands()[static::$commandName]; + $command = new $className(); // Skip conversions for new commands. $parser->setDescription($command->getDescription()); diff --git a/src/Migrations.php b/src/Migrations.php index 3e977e89..fb486fff 100644 --- a/src/Migrations.php +++ b/src/Migrations.php @@ -29,6 +29,8 @@ /** * The Migrations class is responsible for handling migrations command * within an none-shell application. + * + * TODO(mark) This needs to be adapted to use the configure backend selection. */ class Migrations { diff --git a/src/MigrationsDispatcher.php b/src/MigrationsDispatcher.php index 8e0dbf57..c3ca2e35 100644 --- a/src/MigrationsDispatcher.php +++ b/src/MigrationsDispatcher.php @@ -19,26 +19,31 @@ /** * Used to register all supported subcommand in order to make * them executable by the Symfony Console component + * + * @deprecated 4.2.0 Will be removed alongsize phinx */ class MigrationsDispatcher extends Application { /** - * TODO convert this to a method so that config can be used. + * Get the map of command names to phinx commands. * - * @var array - * @psalm-var array|class-string<\Migrations\Command\Phinx\BaseCommand>> + * @return array + * @psalm-return array|class-string<\Migrations\Command\Phinx\BaseCommand>> */ - public static array $phinxCommands = [ - 'Create' => Phinx\Create::class, - 'Dump' => Phinx\Dump::class, - 'MarkMigrated' => Phinx\MarkMigrated::class, - 'Migrate' => Phinx\Migrate::class, - 'Rollback' => Phinx\Rollback::class, - 'Seed' => Phinx\Seed::class, - 'Status' => Phinx\Status::class, - 'CacheBuild' => Phinx\CacheBuild::class, - 'CacheClear' => Phinx\CacheClear::class, - ]; + public static function getCommands(): array + { + return [ + 'Create' => Phinx\Create::class, + 'Dump' => Phinx\Dump::class, + 'MarkMigrated' => Phinx\MarkMigrated::class, + 'Migrate' => Phinx\Migrate::class, + 'Rollback' => Phinx\Rollback::class, + 'Seed' => Phinx\Seed::class, + 'Status' => Phinx\Status::class, + 'CacheBuild' => Phinx\CacheBuild::class, + 'CacheClear' => Phinx\CacheClear::class, + ]; + } /** * Initialize the Phinx console application. @@ -49,7 +54,7 @@ public function __construct(string $version) { parent::__construct('Migrations plugin, based on Phinx by Rob Morgan.', $version); // Update this to use the methods - foreach (static::$phinxCommands as $value) { + foreach ($this->getCommands() as $value) { $this->add(new $value()); } $this->setCatchExceptions(false); diff --git a/src/MigrationsPlugin.php b/src/MigrationsPlugin.php index 8896475e..c09bc718 100644 --- a/src/MigrationsPlugin.php +++ b/src/MigrationsPlugin.php @@ -16,6 +16,8 @@ use Bake\Command\SimpleBakeCommand; use Cake\Console\CommandCollection; use Cake\Core\BasePlugin; +use Cake\Core\Configure; +use Cake\Core\PluginApplicationInterface; use Migrations\Command\MigrationsCacheBuildCommand; use Migrations\Command\MigrationsCacheClearCommand; use Migrations\Command\MigrationsCommand; @@ -59,6 +61,21 @@ class MigrationsPlugin extends BasePlugin MigrationsStatusCommand::class, ]; + /** + * Initialize configuration with defaults. + * + * @param \Cake\Core\PluginApplicationInterface $app The application. + * @return void + */ + public function bootstrap(PluginApplicationInterface $app): void + { + parent::bootstrap($app); + + if (!Configure::check('Migrations.backend')) { + Configure::write('Migrations.backend', 'phinx'); + } + } + /** * Add migrations commands. * From db593febaf470dee48a861ba6e1b9e299ce8b9cb Mon Sep 17 00:00:00 2001 From: Mark Story Date: Tue, 6 Feb 2024 23:23:56 -0500 Subject: [PATCH 2/2] Update psalm-baseline --- psalm-baseline.xml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 7f40f89a..0f869b2b 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,14 @@ + + + MigrationsDispatcher + MigrationsDispatcher::getCommands() + MigrationsDispatcher::getCommands() + \Migrations\MigrationsDispatcher + new MigrationsDispatcher(PHINX_VERSION) + + $phinxName