Skip to content

Commit

Permalink
[improve]ServiceLocator と StatusReporter を作って Migrator から依存を引っ剥がす
Browse files Browse the repository at this point in the history
  • Loading branch information
ngyuki committed May 24, 2018
1 parent 5a63054 commit 9732967
Show file tree
Hide file tree
Showing 14 changed files with 190 additions and 119 deletions.
8 changes: 4 additions & 4 deletions src/Command/AbstractCommand.php
Expand Up @@ -8,14 +8,14 @@

use ngyuki\DbMigrate\Console\ConfigLoader;
use ngyuki\DbMigrate\Migrate\Logger;
use ngyuki\DbMigrate\Migrate\Migrator;
use ngyuki\DbMigrate\Migrate\ServiceLocator;

abstract class AbstractCommand extends Command
{
/**
* @var Migrator
* @var ServiceLocator
*/
protected $migrator;
protected $locator;

protected function configure()
{
Expand Down Expand Up @@ -45,6 +45,6 @@ protected function initialize(InputInterface $input, OutputInterface $output)
$loader = new ConfigLoader();
$config = $loader->load($configPath);

$this->migrator = Migrator::create(new Logger($output), $config, $dryRun);
$this->locator = new ServiceLocator(new Logger($output), $config, $dryRun);
}
}
2 changes: 1 addition & 1 deletion src/Command/ClearCommand.php
Expand Up @@ -15,6 +15,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->migrator->clear();
$this->locator->migrator->clear();
}
}
2 changes: 1 addition & 1 deletion src/Command/DownCommand.php
Expand Up @@ -15,6 +15,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->migrator->down();
$this->locator->migrator->down();
}
}
2 changes: 1 addition & 1 deletion src/Command/ExecCommand.php
Expand Up @@ -20,6 +20,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$directory = $input->getArgument('directory');

$this->migrator->exec($directory);
$this->locator->migrator->exec($directory);
}
}
4 changes: 2 additions & 2 deletions src/Command/MarkCommand.php
Expand Up @@ -31,9 +31,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($all) {
$this->migrator->markAllVersions();
$this->locator->migrator->markAllVersions();
} elseif (strlen($version)) {
$this->migrator->markVersion($version);
$this->locator->migrator->markVersion($version);
} else {
throw new \RuntimeException("Please specify one of --all, version.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/MigrateCommand.php
Expand Up @@ -18,6 +18,6 @@ protected function configure()
protected function execute(InputInterface $input, OutputInterface $output)
{
$target = $input->getArgument('target');
$this->migrator->migrate($target);
$this->locator->migrator->migrate($target);
}
}
4 changes: 2 additions & 2 deletions src/Command/RedoCommand.php
Expand Up @@ -15,7 +15,7 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->migrator->down();
$this->migrator->up();
$this->locator->migrator->down();
$this->locator->migrator->up();
}
}
2 changes: 1 addition & 1 deletion src/Command/StatusCommand.php
Expand Up @@ -14,6 +14,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->migrator->showStatus();
$this->locator->reporter->show();
}
}
4 changes: 2 additions & 2 deletions src/Command/UnmarkCommand.php
Expand Up @@ -31,9 +31,9 @@ protected function execute(InputInterface $input, OutputInterface $output)
}

if ($all) {
$this->migrator->unmarkAllVersions();
$this->locator->migrator->unmarkAllVersions();
} elseif (strlen($version)) {
$this->migrator->unmarkVersion($version);
$this->locator->migrator->unmarkVersion($version);
} else {
throw new \RuntimeException("Please specify one of --all, version.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/Command/UpCommand.php
Expand Up @@ -15,6 +15,6 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->migrator->up();
$this->locator->migrator->up();
}
}
56 changes: 0 additions & 56 deletions src/Migrate/Migrator.php
@@ -1,11 +1,8 @@
<?php
namespace ngyuki\DbMigrate\Migrate;

use ngyuki\DbMigrate\Adapter\AdapterFactory;
use ngyuki\DbMigrate\Adapter\AdapterInterface;
use ngyuki\DbMigrate\Executor\ExecutorManager;
use ngyuki\DbMigrate\Executor\PhpExecutor;
use ngyuki\DbMigrate\Executor\SqlExecutor;

class Migrator
{
Expand All @@ -29,27 +26,6 @@ class Migrator
*/
private $collector;

/**
* @param Logger $logger
* @param Config $config
* @param bool $dryRun
* @return Migrator
*/
public static function create(Logger $logger, Config $config, $dryRun)
{
$adapter = (new AdapterFactory())->create($config->pdo, $logger, $dryRun);

$context = new MigrateContext($config, $logger, $adapter, $dryRun);

$executor = new ExecutorManager($config->workingDirectory);
$executor->add('.php', new PhpExecutor($context));
$executor->add('.sql', new SqlExecutor($adapter));

$collector = new MigrationCollector($adapter, $config->scriptDirectory);

return new Migrator($logger, $adapter, $executor, $collector);
}

public function __construct(
Logger $logger,
AdapterInterface $adapter,
Expand All @@ -62,38 +38,6 @@ public function __construct(
$this->collector = $collector;
}

/**
* マイグレーションの状態を表示
*/
public function showStatus()
{
$statuses = $this->collector->listStatuses();

if (count($statuses) == 0) {
$this->logger->log("migrate nothing");
return 0;
}

$code = 0;

foreach ($statuses as $version => $status) {
if ($status->isMissing()) {
$suffix = " (missing)";
} else {
$suffix = "";
}

if ($status->isApplied()) {
$this->logger->log("* {$version}{$suffix}");
} else {
$this->logger->log(" {$version}{$suffix}");
$code = 1;
}
}

return $code;
}

/**
* @param Status[] $migrations
* @param Status[] $up
Expand Down
82 changes: 82 additions & 0 deletions src/Migrate/ServiceLocator.php
@@ -0,0 +1,82 @@
<?php
namespace ngyuki\DbMigrate\Migrate;

use ngyuki\DbMigrate\Adapter\AdapterFactory;
use ngyuki\DbMigrate\Adapter\AdapterInterface;
use ngyuki\DbMigrate\Executor\ExecutorManager;
use ngyuki\DbMigrate\Executor\PhpExecutor;
use ngyuki\DbMigrate\Executor\SqlExecutor;
use Symfony\Component\Console\Exception\LogicException;

/**
* @property Logger $logger
* @property AdapterInterface $adapter
* @property ExecutorManager $executor
* @property MigrationCollector $collector
* @property StatusReporter $reporter
* @property Migrator $migrator
*/
class ServiceLocator
{
/**
* @var Logger
*/
private $logger;

/**
* @var AdapterInterface
*/
private $adapter;

/**
* @var ExecutorManager
*/
private $executor;

/**
* @var MigrationCollector
*/
private $collector;

/**
* @var StatusReporter
*/
private $reporter;

/**
* @var Migrator
*/
private $migrator;

public function __construct(Logger $logger, Config $config, $dryRun)
{
$adapter = (new AdapterFactory())->create($config->pdo, $logger, $dryRun);

$context = new MigrateContext($config, $logger, $adapter, $dryRun);

$executor = new ExecutorManager($config->workingDirectory);
$executor->add('.php', new PhpExecutor($context));
$executor->add('.sql', new SqlExecutor($adapter));

$collector = new MigrationCollector($adapter, $config->scriptDirectory);

$reporter = new StatusReporter($logger, $collector);

$migrator = new Migrator($logger, $adapter, $executor, $collector, $reporter);

$this->logger = $logger;
$this->adapter = $adapter;
$this->executor = $executor;
$this->collector = $collector;
$this->reporter = $reporter;
$this->migrator = $migrator;
}

public function __get($name)
{
if (!isset($this->$name)) {
throw new LogicException("service $name is not defined");
}
return $this->$name;
}
}
53 changes: 53 additions & 0 deletions src/Migrate/StatusReporter.php
@@ -0,0 +1,53 @@
<?php
namespace ngyuki\DbMigrate\Migrate;

class StatusReporter
{
/**
* @var Logger
*/
private $logger;

/**
* @var MigrationCollector
*/
private $collector;

public function __construct(Logger $logger, MigrationCollector $collector)
{
$this->logger = $logger;
$this->collector = $collector;
}

/**
* マイグレーションの状態を表示
*/
public function show()
{
$statuses = $this->collector->listStatuses();

if (count($statuses) == 0) {
$this->logger->log("migrate nothing");
return 0;
}

$code = 0;

foreach ($statuses as $version => $status) {
if ($status->isMissing()) {
$suffix = " (missing)";
} else {
$suffix = "";
}

if ($status->isApplied()) {
$this->logger->log("* {$version}{$suffix}");
} else {
$this->logger->log(" {$version}{$suffix}");
$code = 1;
}
}

return $code;
}
}

0 comments on commit 9732967

Please sign in to comment.