Skip to content

Commit

Permalink
update, some logic modify, more event support ...
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Jun 30, 2017
1 parent 4d46499 commit afddfbf
Show file tree
Hide file tree
Showing 21 changed files with 1,154 additions and 255 deletions.
3 changes: 3 additions & 0 deletions examples/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@ public function downCommand()
return 0;
}

/**
* show cursor move on the screen
*/
public function cursorCommand()
{
$this->write('hello, this in ' . __METHOD__);
Expand Down
76 changes: 29 additions & 47 deletions src/AbstractApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,28 @@

use inhere\console\io\Input;
use inhere\console\io\Output;
use inhere\console\traits\TraitInputOutput;
use inhere\console\traits\InputOutputTrait;
use inhere\console\traits\SimpleEventStaticTrait;

/**
* Class AbstractApp
* @package inhere\console
*/
abstract class AbstractApp
{
use TraitInputOutput;
use InputOutputTrait;
use SimpleEventStaticTrait;

// event name list
const ON_BEFORE_RUN = 'beforeRun';
const ON_AFTER_RUN = 'afterRun';
const ON_APP_STOP = 'appStop';
const ON_RUN_ERROR = 'runError';
const ON_BEFORE_EXEC = 'beforeExec';
const ON_AFTER_EXEC = 'afterExec';
const ON_EXEC_ERROR = 'execError';
const ON_STOP_RUN = 'stopRun';
const ON_NOT_FOUND = 'notFound';

/**
* @var array
*/
protected static $hooks = [
// 'appInit' => '',
'beforeRun' => '',
'afterRun' => '',
'appStop' => '',
'notFound' => '',
];

/**
* app config
* @var array
Expand Down Expand Up @@ -69,6 +64,11 @@ abstract class AbstractApp
*/
protected $commands = [];

/**
* @var string
*/
private $commandName;

/**
* App constructor.
* @param array $config
Expand All @@ -86,7 +86,7 @@ public function __construct(array $config = [], Input $input = null, Output $out

protected function init()
{
// ...
$this->commandName = $this->input->getCommand();
}

/**********************************************************
Expand All @@ -96,7 +96,6 @@ protected function init()
protected function prepareRun()
{
date_default_timezone_set($this->config('timeZone', 'UTC'));

// ...
}

Expand All @@ -109,17 +108,13 @@ public function run($exit = true)
$this->prepareRun();

// call 'onBeforeRun' service, if it is registered.
if ($cb = self::$hooks[self::ON_BEFORE_RUN]) {
$cb($this);
}
self::fire(self::ON_BEFORE_RUN, [$this]);

// do run ...
$returnCode = $this->doRun();

// call 'onAfterRun' service, if it is registered.
if ($cb = self::$hooks[self::ON_AFTER_RUN]) {
$cb($this);
}
self::fire(self::ON_AFTER_RUN, [$this]);

if ($exit) {
$this->stop((int)$returnCode);
Expand All @@ -137,9 +132,7 @@ abstract public function doRun();
public function stop($code = 0)
{
// call 'onAppStop' service, if it is registered.
if ($cb = self::$hooks[self::ON_APP_STOP]) {
$cb($this);
}
self::fire(self::ON_STOP_RUN, [$this]);

exit((int)$code);
}
Expand All @@ -160,7 +153,7 @@ public function controller(string $name, string $controller)
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
}

$this->checkName($name, true);
$this->validateName($name, true);

if (!class_exists($controller)) {
throw new \InvalidArgumentException("The console controller class [$controller] not exists!");
Expand Down Expand Up @@ -193,7 +186,7 @@ public function command(string $name, $handler)
throw new \InvalidArgumentException('Parameters are not allowed to is empty!');
}

$this->checkName($name);
$this->validateName($name);

// is an class name string
$this->commands[$name] = $handler;
Expand All @@ -211,25 +204,6 @@ public function commands(array $commands)
}
}

/**
* @return array
*/
public static function hooks(): array
{
return array_keys(self::$hooks);
}

/**
* @param $event
* @param callable $handler
*/
public function on(string $event, callable $handler)
{
if (isset(self::$hooks[$event])) {
self::$hooks[$event] = $handler;
}
}

/**
* @return array
*/
Expand Down Expand Up @@ -314,7 +288,7 @@ public function isDebug(): bool
* @param $name
* @param bool $isGroup
*/
protected function checkName(string $name, $isGroup = false)
protected function validateName(string $name, $isGroup = false)
{
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';

Expand All @@ -326,4 +300,12 @@ protected function checkName(string $name, $isGroup = false)
throw new \InvalidArgumentException("The command name [$name] is not allowed. It is a built in command.");
}
}

/**
* @return string
*/
public function getCommandName(): string
{
return $this->commandName;
}
}
105 changes: 92 additions & 13 deletions src/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@

namespace inhere\console;

use inhere\console\helpers\Annotation;
use inhere\console\io\Input;
use inhere\console\io\InputDefinition;
use inhere\console\io\Output;
use inhere\console\traits\TraitInputOutput;
use inhere\console\traits\TraitInteract;
use inhere\console\traits\InputOutputTrait;
use inhere\console\traits\UserInteractTrait;
use inhere\console\utils\Annotation;

/**
* Class AbstractCommand
* @package inhere\console
*/
abstract class AbstractCommand
{
use TraitInputOutput;
use TraitInteract;
use InputOutputTrait;
use UserInteractTrait;

// command description message
// please use the const setting current controller/command description
Expand Down Expand Up @@ -57,33 +58,73 @@ abstract class AbstractCommand
'example' => true,
];

/**
* @var InputDefinition
*/
private $definition;

////// for strict mode //////

/**
* Command constructor.
* @param Input $input
* @param Output $output
* @param InputDefinition|null $definition
*/
public function __construct(Input $input, Output $output)
public function __construct(Input $input, Output $output, InputDefinition $definition = null)
{
$this->input = $input;
$this->output = $output;

if (null === $definition) {
$this->definition = new InputDefinition();
} else {
$this->definition = $definition;
$this->validate();
}
}

/**
* @return array
*/
protected function configure()
{
return [
// 'arguments' => [],
// 'options' => [],
];
}

public function validate()
{
$definition = $this->definition;
$givenArguments = $this->input->getArgs();

$missingArguments = array_filter(array_keys($definition->getArguments()), function ($name) use ($definition, $givenArguments) {
return !array_key_exists($name, $givenArguments) && $definition->argumentIsRequired($name);
});

if (count($missingArguments) > 0) {
throw new \RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));
}
}

/**
* @param string $arg
* run
*/
abstract public function run($arg = '');
abstract public function run();

/**
* @param string $action
* beforeRun
*/
protected function beforeRun($action)
protected function beforeRun()
{
}

/**
* @param string $action
* afterRun
*/
protected function afterRun($action)
protected function afterRun()
{
}

Expand Down Expand Up @@ -129,7 +170,11 @@ protected function showHelpByMethodAnnotation($method, $action = null)
if (isset(self::$allowTags[$tag])) {
// need multi align
if (self::$allowTags[$tag]) {
$msg = implode("\n ", array_filter(explode("\n", $msg), 'trim'));
$lines = array_map(function ($line) {
return trim($line);
}, explode("\n", $msg));

$msg = implode("\n ", array_filter($lines, 'trim'));
}

$tag = ucfirst($tag);
Expand Down Expand Up @@ -182,4 +227,38 @@ public static function setAllowTags(array $allowTags)
{
self::$allowTags = $allowTags;
}

/**
* @return string
*/
public static function getDescription(): string
{
return self::$description;
}

/**
* @param string $description
*/
public static function setDescription(string $description)
{
self::$description = $description;
}

/**
* @return InputDefinition
*/
public function getDefinition(): InputDefinition
{
return $this->definition;
}

/**
* @param InputDefinition $definition
*/
public function setDefinition(InputDefinition $definition)
{
$this->definition = $definition;
}


}
Loading

0 comments on commit afddfbf

Please sign in to comment.