Skip to content

Commit a4f7831

Browse files
committed
refactoring match and dispatch logic
1 parent 5ef207b commit a4f7831

File tree

9 files changed

+238
-488
lines changed

9 files changed

+238
-488
lines changed

examples/alone

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ try {
1919
} catch (\Exception $e) {
2020
$message = \Toolkit\Cli\Color::apply('error', $e->getMessage());
2121

22-
echo sprintf("%s\nFile %s:%d\nTrace:\n%s\n",
22+
echo \sprintf("%s\nFile %s:%d\nTrace:\n%s\n",
2323
$message, $e->getFile(), $e->getLine(), $e->getTraceAsString()
2424
);
2525
}

examples/alone-app

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
use Inhere\Console\Examples\Controller\HomeController;
8+
use Toolkit\Cli\Color;
89

910
define('BASE_PATH', dirname(__DIR__));
1011

@@ -18,11 +19,13 @@ try {
1819
], $input);
1920

2021
$app->controller('home', HomeController::class);
21-
$code = $app->runAction('home', $input->getCommand(), false, true);
2222

23-
exit((int)$code);
23+
$subCmd = $input->getCommand();
24+
$result = $app->dispatch('home:' . $subCmd, true);
25+
26+
exit((int)$result);
2427
} catch (\Exception $e) {
25-
$message = \Toolkit\Cli\Color::apply('error', $e->getMessage());
28+
$message = Color::apply('error', $e->getMessage());
2629

2730
echo sprintf("%s\nFile %s:%d\nTrace:\n%s\n",
2831
$message, $e->getFile(), $e->getLine(), $e->getTraceAsString()

examples/commands.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@
4646
$app->controller(\Inhere\Console\Examples\Controller\InteractController::class);
4747

4848
// add alias for a group command.
49-
$app->addCommandAliases('home:test', 'h-test');
49+
$app->addAliases('home:test', 'h-test');

src/AbstractApplication.php

Lines changed: 3 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -87,21 +87,6 @@ abstract class AbstractApplication implements ApplicationInterface
8787
*/
8888
private $errorHandler;
8989

90-
/**
91-
* @var array Some metadata for command
92-
* - description
93-
*/
94-
private $commandsMeta = [];
95-
96-
/** @var array Save command aliases */
97-
private $commandAliases = [];
98-
99-
/** @var array The independent commands */
100-
protected $commands = [];
101-
102-
/** @var array The group commands(controller) */
103-
protected $controllers = [];
104-
10590
/**
10691
* Class constructor.
10792
* @param array $config
@@ -219,13 +204,6 @@ public function run(bool $exit = true)
219204
return $result;
220205
}
221206

222-
/**
223-
* dispatch command
224-
* @param string $command A command name
225-
* @return int|mixed
226-
*/
227-
abstract protected function dispatch(string $command);
228-
229207
protected function afterRun(): void
230208
{
231209
}
@@ -366,62 +344,20 @@ protected function filterSpecialCommand(string $command): bool
366344
return true;
367345
}
368346

369-
/**
370-
* @param $name
371-
* @param bool $isGroup
372-
* @throws \InvalidArgumentException
373-
*/
374-
protected function validateName(string $name, bool $isGroup = false): void
375-
{
376-
$pattern = $isGroup ? '/^[a-z][\w-]+$/' : '/^[a-z][\w-]*:?([a-z][\w-]+)?$/';
377-
378-
if (1 !== \preg_match($pattern, $name)) {
379-
throw new \InvalidArgumentException("The command name '$name' is must match: $pattern");
380-
}
381-
382-
if ($this->isInternalCommand($name)) {
383-
throw new \InvalidArgumentException("The command name '$name' is not allowed. It is a built in command.");
384-
}
385-
}
386-
387347
/**
388348
* @param string $name
389349
* @param string|array $aliases
390350
* @return $this
391351
*/
392-
public function addCommandAliases(string $name, $aliases): self
352+
public function addAliases(string $name, $aliases): self
393353
{
394-
if (!$name || !$aliases) {
395-
return $this;
396-
}
397-
398-
foreach ((array)$aliases as $alias) {
399-
if ($alias = trim($alias)) {
400-
$this->commandAliases[$alias] = $name;
401-
}
354+
if ($name && $aliases) {
355+
$this->router->setAlias($name, $aliases);
402356
}
403357

404358
return $this;
405359
}
406360

407-
/**
408-
* @param string $name
409-
* @return string
410-
*/
411-
protected function getRealCommandName(string $name): string
412-
{
413-
return $this->commandAliases[$name] ?? $name;
414-
}
415-
416-
/**
417-
* @param string $name
418-
* @return mixed|null
419-
*/
420-
public function findCommand(string $name)
421-
{
422-
return $this->commands[$name] ?? $this->controllers[$name] ?? null;
423-
}
424-
425361
/**
426362
* @param int $level
427363
* @param string $format
@@ -440,86 +376,6 @@ public function logf(int $level, string $format, ...$args): void
440376
* getter/setter methods
441377
**********************************************************/
442378

443-
/**
444-
* @return array
445-
*/
446-
public function getControllerNames(): array
447-
{
448-
return \array_keys($this->controllers);
449-
}
450-
451-
/**
452-
* @return array
453-
*/
454-
public function getCommandNames(): array
455-
{
456-
return \array_keys($this->commands);
457-
}
458-
459-
/**
460-
* @param array $controllers
461-
* @throws \InvalidArgumentException
462-
*/
463-
public function setControllers(array $controllers): void
464-
{
465-
foreach ($controllers as $name => $controller) {
466-
if (\is_int($name)) {
467-
$this->controller($controller);
468-
} else {
469-
$this->controller($name, $controller);
470-
}
471-
}
472-
}
473-
474-
/**
475-
* @return array
476-
*/
477-
public function getControllers(): array
478-
{
479-
return $this->controllers;
480-
}
481-
482-
/**
483-
* @param $name
484-
* @return bool
485-
*/
486-
public function isController(string $name): bool
487-
{
488-
return isset($this->controllers[$name]);
489-
}
490-
491-
/**
492-
* @param array $commands
493-
* @throws \InvalidArgumentException
494-
*/
495-
public function setCommands(array $commands): void
496-
{
497-
foreach ($commands as $name => $handler) {
498-
if (\is_int($name)) {
499-
$this->command($handler);
500-
} else {
501-
$this->command($name, $handler);
502-
}
503-
}
504-
}
505-
506-
/**
507-
* @return array
508-
*/
509-
public function getCommands(): array
510-
{
511-
return $this->commands;
512-
}
513-
514-
/**
515-
* @param $name
516-
* @return bool
517-
*/
518-
public function isCommand(string $name): bool
519-
{
520-
return isset($this->commands[$name]);
521-
}
522-
523379
/**
524380
* @return string|null
525381
*/
@@ -653,80 +509,6 @@ public function isProfile(): bool
653509
return (bool)$this->input->getOpt('profile', $this->getParam('profile'));
654510
}
655511

656-
/**
657-
* @param null|string $name
658-
* @return array
659-
*/
660-
public function getCommandAliases(string $name = null): array
661-
{
662-
if (!$name) {
663-
return $this->commandAliases;
664-
}
665-
666-
return \array_keys($this->commandAliases, $name, true);
667-
}
668-
669-
/**
670-
* @param array $commandAliases
671-
*/
672-
public function setCommandAliases(array $commandAliases): void
673-
{
674-
$this->commandAliases = $commandAliases;
675-
}
676-
677-
/**
678-
* @return array
679-
*/
680-
public function getCommandsMeta(): array
681-
{
682-
return $this->commandsMeta;
683-
}
684-
685-
/**
686-
* @param string $command
687-
* @param array $meta
688-
*/
689-
public function setCommandMeta(string $command, array $meta): void
690-
{
691-
if (isset($this->commandsMeta[$command])) {
692-
$this->commandsMeta[$command] = \array_merge($this->commandsMeta[$command], $meta);
693-
} else {
694-
$this->commandsMeta[$command] = $meta;
695-
}
696-
}
697-
698-
/**
699-
* @param string $command
700-
* @return array
701-
*/
702-
public function getCommandMeta(string $command): array
703-
{
704-
return $this->commandsMeta[$command] ?? [];
705-
}
706-
707-
/**
708-
* @param string $command
709-
* @param string $key
710-
* @param $value
711-
*/
712-
public function setCommandMetaValue(string $command, string $key, $value): void
713-
{
714-
if ($value !== null) {
715-
$this->commandsMeta[$command][$key] = $value;
716-
}
717-
}
718-
719-
/**
720-
* @param string $command
721-
* @param string $key
722-
* @param mixed $default
723-
* @return mixed
724-
*/
725-
public function getCommandMetaValue(string $command, string $key, $default = null)
726-
{
727-
return $this->commandsMeta[$command][$key] ?? $default;
728-
}
729-
730512
/**
731513
* @return ErrorHandlerInterface
732514
*/

0 commit comments

Comments
 (0)