[Nostromo] Refactor config load

This commit is contained in:
Andrés Montañez 2017-01-29 19:25:03 -03:00
parent 91da979151
commit 0bae8e2b1b
18 changed files with 24 additions and 53 deletions

View file

@ -12,7 +12,6 @@ use Mage\MageApplication;
try {
$mage = new MageApplication('.mage.yml');
$mage->configure();
$mage->run();
} catch (Exception $exception) {
printf('Error: %s' . PHP_EOL, $exception->getMessage());

View file

@ -66,4 +66,12 @@ abstract class AbstractCommand extends Command
$utils = new Utils();
return $utils->getStageName($this->runtime->getStage());
}
/**
* Requires the configuration to be loaded
*/
protected function requireConfig()
{
$this->getApplication()->configure();
}
}

View file

@ -41,6 +41,8 @@ class DumpCommand extends AbstractCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');

View file

@ -42,6 +42,8 @@ class EnvironmentsCommand extends AbstractCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');

View file

@ -58,6 +58,8 @@ class DeployCommand extends AbstractCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');

View file

@ -51,6 +51,8 @@ class ListCommand extends AbstractCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$utils = new Utils();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');

View file

@ -52,6 +52,8 @@ class RollbackCommand extends DeployCommand
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->requireConfig();
$output->writeln('Starting <fg=blue>Magallanes</>');
$output->writeln('');

View file

@ -40,9 +40,11 @@ class MageApplication extends Application
*/
public function __construct($file)
{
$this->file = $file;
parent::__construct('Magallanes', Mage::VERSION);
$this->file = $file;
$dispatcher = new EventDispatcher();
$this->setDispatcher($dispatcher);
$dispatcher->addListener(ConsoleEvents::EXCEPTION, function (ConsoleExceptionEvent $event) {
$output = $event->getOutput();
@ -52,8 +54,8 @@ class MageApplication extends Application
$event->setException(new \LogicException('Caught exception', $exitCode, $event->getException()));
});
$this->setDispatcher($dispatcher);
parent::__construct('Magallanes', Mage::VERSION);
$this->runtime = $this->instantiateRuntime();
$this->loadBuiltInCommands();
}
/**
@ -85,12 +87,9 @@ class MageApplication extends Application
$logger->pushHandler(new StreamHandler($logfile));
}
$this->runtime = $this->instantiateRuntime();
$this->runtime->setConfiguration($config['magephp']);
$this->runtime->setLogger($logger);
$this->loadBuiltInCommands();
return true;
return;
}
throw new RuntimeException(sprintf('The file "%s" does not have a valid Magallanes configuration.', $this->file));

View file

@ -21,7 +21,6 @@ class DumpCommandTest extends TestCase
public function testConfigDumpTermination()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/basic.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('config:dump');

View file

@ -21,7 +21,6 @@ class EnvironmentsCommandTest extends TestCase
public function testConfigDumpTermination()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/basic.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('config:environments');

View file

@ -21,7 +21,6 @@ class DeployCommandMiscTasksTest extends TestCase
public function testSymfonyEnvironmentConfiguration()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/symfony-envconf.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -54,7 +53,6 @@ class DeployCommandMiscTasksTest extends TestCase
public function testComposerFlags()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/composer.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -85,7 +83,6 @@ class DeployCommandMiscTasksTest extends TestCase
public function testInvalidTaskName()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/invalid-task.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -101,7 +98,6 @@ class DeployCommandMiscTasksTest extends TestCase
public function testBrokenGitBranch()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/broken-git-branch.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -120,7 +116,6 @@ class DeployCommandMiscTasksTest extends TestCase
public function testBrokenGitCheckout()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/broken-git-branch.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -139,7 +134,6 @@ class DeployCommandMiscTasksTest extends TestCase
public function testBrokenGitUpdate()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/broken-git-branch.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');

View file

@ -21,7 +21,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithNoHosts()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/no-hosts.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -40,7 +39,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithSudo()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-sudo.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -79,7 +77,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithBranchOverwrite()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -117,8 +114,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithCustomTask()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-custom-task.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -151,7 +146,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithErrorTaskCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-error.yml');
$application->configure();
$application->getRuntime()->setReleaseId('20170101015120');
@ -193,7 +187,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithFailingPostDeployTaskCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-with-postdeploy-error.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -229,7 +222,6 @@ class DeployCommandMiscTest extends TestCase
public function testDeploymentWithSkippingTask()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-skipping.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');

View file

@ -21,7 +21,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentWithReleasesCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml');
$application->configure();
$application->getRuntime()->setReleaseId('20170101015120');
@ -72,7 +71,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentWithoutReleasesTarPrepare()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar1.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -88,7 +86,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentWithoutReleasesTarCopy()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar2.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -104,7 +101,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentWithoutReleasesTarCleanup()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-tar3.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -120,7 +116,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentFailCopyCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-fail-copy-tar.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -136,7 +131,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentWithoutReleasesForceRelease()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-force-release.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -152,7 +146,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentFailToExtract()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml');
$application->configure();
$application->getRuntime()->setReleaseId('20170101015120');
@ -195,7 +188,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentFailToCopy()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml');
$application->configure();
$application->getRuntime()->setReleaseId('20170101015120');
@ -237,7 +229,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentFailCleanup()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml');
$application->configure();
$application->getRuntime()->setReleaseId('20170101015120');
@ -291,7 +282,6 @@ class DeployCommandWithReleasesTest extends TestCase
public function testDeploymentFailMidway()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost.yml');
$application->configure();
$application->getRuntime()->setReleaseId('20170101015120');

View file

@ -21,7 +21,6 @@ class DeployCommandWithoutReleasesTest extends TestCase
public function testDeploymentWithoutReleasesCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');
@ -59,7 +58,6 @@ class DeployCommandWithoutReleasesTest extends TestCase
public function testDeploymentFailMidway()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/testhost-without-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('deploy');

View file

@ -21,7 +21,6 @@ class ListCommandTest extends TestCase
public function testListReleasesCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
@ -49,7 +48,6 @@ class ListCommandTest extends TestCase
public function testListReleasesWithInvalidEnvironment()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
@ -65,7 +63,6 @@ class ListCommandTest extends TestCase
public function testListReleasesWithoutReleases()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-without-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
@ -81,7 +78,6 @@ class ListCommandTest extends TestCase
public function testFailToGetCurrentRelease()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-fail-get-current.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
@ -97,7 +93,6 @@ class ListCommandTest extends TestCase
public function testNoReleasesAvailable()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-no-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
@ -112,7 +107,6 @@ class ListCommandTest extends TestCase
public function testFailGetReleases()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-fail-get-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');
@ -128,7 +122,6 @@ class ListCommandTest extends TestCase
public function testNoHosts()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-no-hosts.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:list');

View file

@ -21,7 +21,6 @@ class RollbackCommandTest extends TestCase
public function testRollbackReleaseCommands()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:rollback');
@ -49,7 +48,6 @@ class RollbackCommandTest extends TestCase
public function testRollbackReleaseWithInvalidEnvironment()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:rollback');
@ -65,7 +63,6 @@ class RollbackCommandTest extends TestCase
public function testRollbackReleaseWithoutReleases()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-without-releases.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:rollback');
@ -81,7 +78,6 @@ class RollbackCommandTest extends TestCase
public function testRollbackReleaseNotAvailable()
{
$application = new MageApplicationMockup(__DIR__ . '/../../../Resources/testhost-not-have-release.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('releases:rollback');

View file

@ -22,7 +22,6 @@ class VersionCommandTest extends TestCase
public function testVersionOutput()
{
$application = new MageApplicationMockup(__DIR__ . '/../../Resources/basic.yml');
$application->configure();
/** @var AbstractCommand $command */
$command = $application->find('version');

View file

@ -21,7 +21,6 @@ class MageApplicationTest extends TestCase
public function testValidConfiguration()
{
$application = new MageApplication(__DIR__ . '/Resources/basic.yml');
$application->configure();
$this->assertTrue($application instanceof MageApplication);
}
@ -29,7 +28,6 @@ class MageApplicationTest extends TestCase
{
try {
$application = new MageApplication(__DIR__ . '/Resources/invalid.yml');
$application->configure();
} catch (Exception $exception) {
$this->assertTrue($exception instanceof RuntimeException);
$this->assertEquals(sprintf('The file "%s" does not have a valid Magallanes configuration.', __DIR__ . '/Resources/invalid.yml'), $exception->getMessage());
@ -40,7 +38,6 @@ class MageApplicationTest extends TestCase
{
try {
$application = new MageApplication(__DIR__ . '/Resources/invalid-yaml.yml');
$application->configure();
} catch (Exception $exception) {
$this->assertTrue($exception instanceof RuntimeException);
$this->assertEquals(sprintf('Error parsing the file "%s".', __DIR__ . '/Resources/invalid-yaml.yml'), $exception->getMessage());
@ -51,7 +48,6 @@ class MageApplicationTest extends TestCase
{
try {
$application = new MageApplication(__DIR__ . '/Resources/this-does-not-exists.yml');
$application->configure();
} catch (Exception $exception) {
$this->assertTrue($exception instanceof RuntimeException);
$this->assertEquals(sprintf('The file "%s" does not exists or is not readable.', __DIR__ . '/Resources/this-does-not-exists.yml'), $exception->getMessage());
@ -62,7 +58,6 @@ class MageApplicationTest extends TestCase
{
$application = new MageApplication(__DIR__ . '/Resources/basic.yml');
$application->setAutoExit(false);
$application->configure();
$this->assertTrue($application instanceof MageApplication);
$application->register('foo')->setCode(function () {