Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[improve]ServiceLocator と StatusReporter を作って Migrator から依存を引っ剥がす
- Loading branch information
Showing
14 changed files
with
190 additions
and
119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
Oops, something went wrong.