Skip to content

Commit

Permalink
improved migration cli
Browse files Browse the repository at this point in the history
  • Loading branch information
reinvanoyen committed Nov 13, 2019
1 parent d5f6ea7 commit e37cc87
Show file tree
Hide file tree
Showing 24 changed files with 279 additions and 319 deletions.
2 changes: 1 addition & 1 deletion src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
class Application extends Container
{
const VERSION = '1.0.6';
const VERSION = '1.0.7';

/**
* @var bool $isBooted
Expand Down
14 changes: 4 additions & 10 deletions src/Console/Command/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public function __construct(ContainerInterface $app)
abstract protected function createSignature(Signature $signature): Signature;

/**
* Gets the name of the command
*
* @return string
* @throws \Exception
*/
Expand All @@ -62,8 +60,6 @@ final public function getName(): string
}

/**
* Gets the description of the command
*
* @return string
* @throws \Exception
*/
Expand All @@ -75,9 +71,8 @@ final public function getDescription(): string
}

/**
* Gets the signature of the command
*
* @return Signature
* @throws \Exception
*/
final public function getSignature(): Signature
{
Expand Down Expand Up @@ -111,17 +106,17 @@ final private function make()
/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->outputHelpMessage($output);
}

/**
* Runs the command
*
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
final public function run(InputInterface $input, OutputInterface $output)
{
Expand All @@ -146,9 +141,8 @@ final public function run(InputInterface $input, OutputInterface $output)
}

/**
* Output an auto-generated help message
*
* @param OutputInterface $output
* @throws \Exception
*/
public function outputHelpMessage(OutputInterface $output)
{
Expand Down
26 changes: 24 additions & 2 deletions src/Console/Command/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class Option
*/
private $description = '';

/**
* @var $default
*/
private $default = true;

/**
* Option constructor.
* @param string $name
Expand Down Expand Up @@ -81,13 +86,30 @@ public function getDescription(): string
}

/**
* Sets the description of the option
*
* @return bool
*/
public function getDefault()
{
return $this->default;
}

/**
* @param string $description
* @return $this
*/
public function setDescription(string $description)
{
$this->description = $description;
return $this;
}

/**
* @param $default
* @return $this
*/
public function setDefault($default)
{
$this->default = $default;
return $this;
}
}
30 changes: 24 additions & 6 deletions src/Console/Input/ConsoleInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,29 +62,47 @@ private function parse()
}
}

// First we parse out the options
// Then we parse out the options
foreach ($this->getSignature()->getOptions() as $option) {

$definitions = [
'-'.$option->getName(),
'--'.$option->getName(),
];

if ($option->getAlias()) {
$definitions[] = '-'.$option->getAlias();
$definitions[] = '--'.$option->getAlias();
if ($alias = $option->getAlias()) {
$definitions[] = '-'.$alias;
$definitions[] = '--'.$alias;
}

foreach ($definitions as $definition) {
$optionPosition = array_search($definition, $this->rawArguments);

if ($optionPosition !== false) {

// We found the option in the list of given arguments
if (
isset($this->rawArguments[$optionPosition + 1]) &&
substr($this->rawArguments[$optionPosition + 1], 0, strlen('-')) !== '-'
) {
// We also found a value for the option
// We remove the value of the option from the argument list
$optionValue = $this->rawArguments[$optionPosition + 1];
$this->setOption($option->getName(), $optionValue);
array_splice($this->rawArguments, $optionPosition + 1, 1);

} else {

$this->setOption($option->getName(), $option->getDefault());
}

array_splice($this->rawArguments, $optionPosition, 1);
$this->setOption($option->getName());
break;
}
}
}

// Now we loop all arguments and me sure they are present
// Now we loop all arguments and make sure they are present
foreach ($this->getSignature()->getArguments() as $position => $argument) {
if (isset($this->rawArguments[$position])) {
$this->setArgument($argument->getName(), $this->rawArguments[$position]);
Expand Down
4 changes: 2 additions & 2 deletions src/Console/Input/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ final public function getOption(string $name)
* Sets an option
*
* @param string $name
* @param bool $value
* @param mixed $value
*/
public function setOption(string $name, $value = true)
public function setOption(string $name, $value)
{
$this->options[$name] = $value;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ class Kernel extends Command implements KernelInterface
private $registeredCommands = [];

/**
* Handle the incoming input
*
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
public function handle(InputInterface $input, OutputInterface $output)
{
Expand Down Expand Up @@ -61,12 +60,13 @@ protected function createSignature(Signature $signature): Signature
{
return $signature->setName('oak')
->addOption(Option::create('version', 'v')->setDescription('Display the version of Oak framework'))
;
;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @throws \Exception
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Contracts/Console/InputInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function setArgument(string $name, $value);

public function getOptions();
public function getOption(string $name);
public function setOption(string $name, $value = true);
public function setOption(string $name, $value);

public function hasSubCommand(): bool;
public function getSubCommand();
Expand Down
58 changes: 58 additions & 0 deletions src/Migration/Console/DowndateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Oak\Migration\Console;

use Oak\Console\Command\Command;
use Oak\Console\Command\Option;
use Oak\Console\Command\Signature;
use Oak\Contracts\Console\InputInterface;
use Oak\Contracts\Console\OutputInterface;
use Oak\Contracts\Container\ContainerInterface;
use Oak\Migration\MigrationManager;

class DowndateCommand extends Command
{
/**
* @var MigrationManager $manager
*/
private $manager;

/**
* MigrationManagerCommand constructor.
* @param MigrationManager $manager
* @param ContainerInterface $app
*/
public function __construct(MigrationManager $manager, ContainerInterface $app)
{
$this->manager = $manager;
parent::__construct($app);
}

protected function createSignature(Signature $signature): Signature
{
return $signature
->setName('downdate')
->addOption(
Option::create('migrator', 'm')
->setDescription('Specify a specific migrator')
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
foreach ($this->manager->getMigrators() as $migrator) {

if (
! ($migratorName = $input->getOption('migrator')) ||
$migrator->getName() === $migratorName
) {
$migrator->downdate();

if ($migratorName) {
break;
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Oak\Migration\Console\Manager;
namespace Oak\Migration\Console;

use Oak\Console\Command\Command;
use Oak\Console\Command\Signature;
Expand All @@ -9,7 +9,7 @@
use Oak\Contracts\Container\ContainerInterface;
use Oak\Migration\MigrationManager;

class MigrateAllCommand extends Command
class ListCommand extends Command
{
/**
* @var MigrationManager $manager
Expand All @@ -30,15 +30,19 @@ public function __construct(MigrationManager $manager, ContainerInterface $app)
protected function createSignature(Signature $signature): Signature
{
return $signature
->setName('migrate-all')
;
->setName('list')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
if (! count($this->manager->getMigrators())) {
$output->writeLine('No migrators registered', OutputInterface::TYPE_ERROR);
return;
}

foreach ($this->manager->getMigrators() as $migrator) {
$output->writeLine('Start '.$migrator->getName().' migrator ('.$migrator->getVersion().'/'.$migrator->getMaxVersion().')', OutputInterface::TYPE_INFO);
$migrator->migrate();
$output->writeLine($migrator->getName().' ('.$migrator->getVersion().'/'.$migrator->getMaxVersion().')');
}
}
}
19 changes: 0 additions & 19 deletions src/Migration/Console/Manager/MigrationManagerCommand.php

This file was deleted.

58 changes: 58 additions & 0 deletions src/Migration/Console/MigrateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Oak\Migration\Console;

use Oak\Console\Command\Command;
use Oak\Console\Command\Option;
use Oak\Console\Command\Signature;
use Oak\Contracts\Console\InputInterface;
use Oak\Contracts\Console\OutputInterface;
use Oak\Contracts\Container\ContainerInterface;
use Oak\Migration\MigrationManager;

class MigrateCommand extends Command
{
/**
* @var MigrationManager $manager
*/
private $manager;

/**
* MigrationManagerCommand constructor.
* @param MigrationManager $manager
* @param ContainerInterface $app
*/
public function __construct(MigrationManager $manager, ContainerInterface $app)
{
$this->manager = $manager;
parent::__construct($app);
}

protected function createSignature(Signature $signature): Signature
{
return $signature
->setName('migrate')
->addOption(
Option::create('migrator', 'm')
->setDescription('Specify a specific migrator')
)
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
foreach ($this->manager->getMigrators() as $migrator) {

if (
! ($migratorName = $input->getOption('migrator')) ||
$migrator->getName() === $migratorName
) {
$migrator->migrate();

if ($migratorName) {
break;
}
}
}
}
}
Loading

0 comments on commit e37cc87

Please sign in to comment.