Skip to content

Commit

Permalink
Merge pull request #686 from cakephp/config-integration
Browse files Browse the repository at this point in the history
Add config switch for backend selection.
  • Loading branch information
markstory authored Feb 7, 2024
2 parents 92a6064 + db593fe commit 08e253b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
9 changes: 9 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.13.1@086b94371304750d1c673315321a55d15fc59015">
<file src="src/Command/MigrationsCommand.php">
<DeprecatedClass>
<code>MigrationsDispatcher</code>
<code>MigrationsDispatcher::getCommands()</code>
<code>MigrationsDispatcher::getCommands()</code>
<code>\Migrations\MigrationsDispatcher</code>
<code>new MigrationsDispatcher(PHINX_VERSION)</code>
</DeprecatedClass>
</file>
<file src="src/Command/Phinx/Create.php">
<PossiblyUndefinedArrayOffset>
<code>$phinxName</code>
Expand Down
10 changes: 5 additions & 5 deletions src/Command/MigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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());
Expand Down
2 changes: 2 additions & 0 deletions src/Migrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
35 changes: 20 additions & 15 deletions src/MigrationsDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
* @psalm-var array<string, class-string<\Phinx\Console\Command\AbstractCommand>|class-string<\Migrations\Command\Phinx\BaseCommand>>
* @return array<string, string>
* @psalm-return array<string, class-string<\Phinx\Console\Command\AbstractCommand>|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.
Expand All @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions src/MigrationsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 08e253b

Please sign in to comment.