Skip to content

Commit 5233c1f

Browse files
authored
Merge pull request #328 from bowphp/refactor/code-base
refactor: refonte console command runner strategy
2 parents 4689ea5 + 0d81989 commit 5233c1f

25 files changed

Lines changed: 93 additions & 76 deletions

src/Console/Argument.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ class Argument
3838
*/
3939
private ?string $command = null;
4040

41+
/**
42+
* The first param argument
43+
*
44+
* @var ?string
45+
*/
46+
private ?string $raw_command = null;
47+
4148
/**
4249
* The command first argument
4350
* php bow command:[action]
@@ -104,14 +111,26 @@ private function formatParameters(): void
104111
*/
105112
private function initCommand(string $param): void
106113
{
107-
if (!preg_match('/^[a-z-]+[a-z]+:[a-z-]+[a-z]+$/', $param)) {
114+
$this->raw_command = $param;
115+
116+
if (!preg_match('/^[a-z-]+[a-z]+(:[a-z-]+[a-z]+){1,}$/', $param)) {
108117
$this->command = $param;
109118
$this->action = null;
110119
} else {
111120
[$this->command, $this->action] = explode(':', $param);
112121
}
113122
}
114123

124+
/**
125+
* Get commands
126+
*
127+
* @return ?string
128+
*/
129+
public function getRawCommand(): ?string
130+
{
131+
return $this->raw_command;
132+
}
133+
115134
/**
116135
* Retrieves a parameter
117136
*

src/Console/Command.php

Lines changed: 42 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -38,44 +38,48 @@ class Command extends AbstractCommand
3838
*
3939
* @var array
4040
*/
41-
private array $command = [
41+
private array $commands = [
4242
"clear" => ClearCommand::class,
4343
"migration" => MigrationCommand::class,
44-
"seeder" => SeederCommand::class,
45-
"add" => [
46-
"controller" => ControllerCommand::class,
47-
"configuration" => ConfigurationCommand::class,
48-
"exception" => ExceptionCommand::class,
49-
"middleware" => MiddlewareCommand::class,
50-
"migration" => MigrationCommand::class,
51-
"model" => ModelCommand::class,
52-
"seeder" => SeederCommand::class,
53-
"service" => ServiceCommand::class,
54-
"validation" => ValidationCommand::class,
55-
"event" => AppEventCommand::class,
56-
"listener" => EventListenerCommand::class,
57-
"producer" => ProducerCommand::class,
58-
"command" => ConsoleCommand::class,
59-
"message" => MessagingCommand::class,
60-
],
61-
"generator" => [
62-
"key" => GenerateKeyCommand::class,
63-
"resource" => GenerateResourceControllerCommand::class,
64-
"session-table" => GenerateSessionCommand::class,
65-
"queue-table" => GenerateQueueCommand::class,
66-
"cache-table" => GenerateCacheCommand::class,
67-
"notification-table" => GenerateNotificationCommand::class,
68-
],
69-
"runner" => [
70-
"console" => ReplCommand::class,
71-
"server" => ServerCommand::class,
72-
"worker" => WorkerCommand::class,
73-
],
74-
"flush" => [
75-
"worker" => WorkerCommand::class,
76-
],
44+
"migrate" => MigrationCommand::class,
45+
"seed" => SeederCommand::class,
46+
"serve" => ServerCommand::class,
47+
"add:controller" => ControllerCommand::class,
48+
"add:configuration" => ConfigurationCommand::class,
49+
"add:exception" => ExceptionCommand::class,
50+
"add:middleware" => MiddlewareCommand::class,
51+
"add:migration" => MigrationCommand::class,
52+
"add:model" => ModelCommand::class,
53+
"add:seeder" => SeederCommand::class,
54+
"add:service" => ServiceCommand::class,
55+
"add:validation" => ValidationCommand::class,
56+
"add:event" => AppEventCommand::class,
57+
"add:listener" => EventListenerCommand::class,
58+
"add:producer" => ProducerCommand::class,
59+
"add:command" => ConsoleCommand::class,
60+
"add:message" => MessagingCommand::class,
61+
"run:console" => ReplCommand::class,
62+
"run:server" => ServerCommand::class,
63+
"run:worker" => WorkerCommand::class,
64+
"flush:worker" => WorkerCommand::class,
65+
"generate:key" => GenerateKeyCommand::class,
66+
"generate:resource" => GenerateResourceControllerCommand::class,
67+
"generate:session-table" => GenerateSessionCommand::class,
68+
"generate:queue-table" => GenerateQueueCommand::class,
69+
"generate:cache-table" => GenerateCacheCommand::class,
70+
"generate:notification-table" => GenerateNotificationCommand::class,
7771
];
7872

73+
/**
74+
* Get the commands
75+
*
76+
* @return array
77+
*/
78+
public function getCommands(): array
79+
{
80+
return $this->commands;
81+
}
82+
7983
/**
8084
* The call command
8185
*
@@ -87,24 +91,16 @@ class Command extends AbstractCommand
8791
*/
8892
public function call(string $command, string $action, ...$rest): mixed
8993
{
90-
$classes = $this->command[$command] ?? null;
94+
$class = $this->commands[$command] ?? null;
9195

92-
if (is_null($classes)) {
96+
if (is_null($class)) {
9397
$this->throwFailsCommand("The command $command not found !");
9498
}
9599

96-
if ($command == "add" || $command == "generator") {
97-
$method = "generate";
98-
} elseif ($command == "runner") {
100+
if (!preg_match('/^(clear|migrate|migration):/', $command)) {
99101
$method = "run";
100102
} else {
101-
$method = Str::camel($action);
102-
}
103-
104-
if (is_array($classes)) {
105-
$class = $classes[$action];
106-
} else {
107-
$class = $classes;
103+
$method = $action;
108104
}
109105

110106
$instance = new $class($this->setting, $this->arg);

src/Console/Command/AppEventCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class AppEventCommand extends AbstractCommand
1616
* @param string $event
1717
* @return void
1818
*/
19-
public function generate(string $event): void
19+
public function run(string $event): void
2020
{
2121
$generator = new Generator(
2222
$this->setting->getEventDirectory(),

src/Console/Command/ClearCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class ClearCommand extends AbstractCommand
1515
* @param string $action
1616
* @return void
1717
*/
18-
public function make(string $action): void
18+
public function run(string $action): void
1919
{
2020
if (!in_array($action, ['view', 'cache', 'session', 'log', 'all'])) {
2121
$this->throwFailsCommand('Clear target not valid', 'clear help');

src/Console/Command/ConfigurationCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ConfigurationCommand extends AbstractCommand
1717
* @param string $configuration
1818
* @return void
1919
*/
20-
public function generate(string $configuration): void
20+
public function run(string $configuration): void
2121
{
2222
$generator = new Generator(
2323
$this->setting->getPackageDirectory(),

src/Console/Command/ConsoleCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ConsoleCommand extends AbstractCommand
1616
* @param string $service
1717
* @return void
1818
*/
19-
public function generate(string $service): void
19+
public function run(string $service): void
2020
{
2121
$generator = new Generator(
2222
$this->setting->getCommandDirectory(),

src/Console/Command/ControllerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ControllerCommand extends AbstractCommand
1616
* @param string $controller
1717
* @return void
1818
*/
19-
public function generate(string $controller): void
19+
public function run(string $controller): void
2020
{
2121
$generator = new Generator(
2222
$this->setting->getControllerDirectory(),

src/Console/Command/EventListenerCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class EventListenerCommand extends AbstractCommand
1616
* @param string $event
1717
* @return void
1818
*/
19-
public function generate(string $event): void
19+
public function run(string $event): void
2020
{
2121
$generator = new Generator(
2222
$this->setting->getEventListenerDirectory(),

src/Console/Command/ExceptionCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ExceptionCommand extends AbstractCommand
1616
* @param string $exception
1717
* @return void
1818
*/
19-
public function generate(string $exception): void
19+
public function run(string $exception): void
2020
{
2121
$generator = new Generator(
2222
$this->setting->getExceptionDirectory(),

src/Console/Command/GenerateCacheCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class GenerateCacheCommand extends AbstractCommand
1616
*
1717
* @return void
1818
*/
19-
public function generate(): void
19+
public function run(): void
2020
{
2121
$create_at = date("YmdHis");
2222
$filename = sprintf("Version%s%sTable", $create_at, ucfirst(Str::camel('caches')));

0 commit comments

Comments
 (0)