|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace pjpawel\LightApi\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use pjpawel\LightApi\Command\Input\Stdin; |
|
7
|
|
|
use pjpawel\LightApi\Command\Internal\CacheClearCommand; |
|
8
|
|
|
use pjpawel\LightApi\Command\Internal\DebugCommandLoaderCommand; |
|
9
|
|
|
use pjpawel\LightApi\Command\Internal\DebugContainerCommand; |
|
10
|
|
|
use pjpawel\LightApi\Command\Internal\DebugCommand; |
|
11
|
|
|
use pjpawel\LightApi\Command\Internal\DebugRouterCommand; |
|
12
|
|
|
use pjpawel\LightApi\Command\Internal\KernelAwareCommand; |
|
13
|
|
|
use pjpawel\LightApi\Command\Internal\WarmUpCacheCommand; |
|
14
|
|
|
use pjpawel\LightApi\Command\Output\Stdout; |
|
15
|
|
|
use pjpawel\LightApi\Container\Awareness\ContainerAwareInterface; |
|
16
|
|
|
use pjpawel\LightApi\Container\ContainerLoader; |
|
17
|
|
|
use pjpawel\LightApi\Container\Definition\ClassDefinition; |
|
18
|
|
|
use pjpawel\LightApi\Kernel; |
|
19
|
|
|
use ReflectionClass; |
|
20
|
|
|
use ReflectionNamedType; |
|
21
|
|
|
|
|
22
|
|
|
class CommandsLoader |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
private const KERNEL_COMMANDS = [ |
|
26
|
|
|
'kernel:debug' => DebugCommand::class, |
|
27
|
|
|
'kernel:cache:warmup' => WarmUpCacheCommand::class, |
|
28
|
|
|
'kernel:cache:clear' => CacheClearCommand::class, |
|
29
|
|
|
'kernel:debug:container' => DebugContainerCommand::class, |
|
30
|
|
|
'kernel:debug:router' => DebugRouterCommand::class, |
|
31
|
|
|
'kernel:debug:command' => DebugCommandLoaderCommand::class |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var array<string,string> |
|
36
|
|
|
*/ |
|
37
|
|
|
public array $command = []; |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
public function __construct() |
|
41
|
|
|
{ |
|
42
|
|
|
$this->command = self::KERNEL_COMMANDS; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function registerCommand(string $commandName, string $className): void |
|
46
|
|
|
{ |
|
47
|
|
|
$this->command[$commandName] = $className; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $commandName |
|
52
|
|
|
* @param ContainerLoader $container |
|
53
|
|
|
* @return int |
|
54
|
|
|
*/ |
|
55
|
|
|
public function runCommandFromName(string $commandName, ContainerLoader $container, ?Kernel $kernel = null): int |
|
56
|
|
|
{ |
|
57
|
|
|
$stdout = new Stdout(); |
|
58
|
|
|
try { |
|
59
|
|
|
$className = $this->command[$commandName]; |
|
60
|
|
|
$reflectionClass = new ReflectionClass($className); |
|
61
|
|
|
$constructor = $reflectionClass->getConstructor(); |
|
62
|
|
|
if ($constructor !== null) { |
|
63
|
|
|
if ($container->has($className)) { |
|
64
|
|
|
/** @var ClassDefinition $classDefinition */ |
|
65
|
|
|
$classDefinition = $container->get($className); |
|
66
|
|
|
$args = $classDefinition->arguments; |
|
67
|
|
|
} else { |
|
68
|
|
|
$args = []; |
|
69
|
|
|
} |
|
70
|
|
|
foreach ($constructor->getParameters() as $parameter) { |
|
71
|
|
|
$parameterType = $parameter->getType(); |
|
72
|
|
|
if ($parameterType instanceof ReflectionNamedType) { |
|
73
|
|
|
$args[] = $container->get($parameterType->getName()); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} else { |
|
77
|
|
|
$args = []; |
|
78
|
|
|
} |
|
79
|
|
|
$stdin = new Stdin(); |
|
80
|
|
|
/** @var Command $command */ |
|
81
|
|
|
$command = $reflectionClass->newInstanceArgs($args); |
|
82
|
|
|
$command->prepare($stdin); |
|
83
|
|
|
/* Prepare input */ |
|
84
|
|
|
$stdin->load(); |
|
85
|
|
|
/* Inject services */ |
|
86
|
|
|
if (is_subclass_of($command, ContainerAwareInterface::class)) { |
|
87
|
|
|
$command->setContainer($container); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
/* If $command is KernelAwareCommand set Kernel */ |
|
90
|
|
|
if (!is_null($kernel) && is_subclass_of($command, KernelAwareCommand::class)) { |
|
91
|
|
|
$command->setKernel($kernel); |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
return $command->execute($stdin, $stdout); |
|
94
|
|
|
} catch (Exception $e) { |
|
95
|
|
|
$stdout->writeln([ |
|
96
|
|
|
'Exception thrown during command', |
|
97
|
|
|
$e->getMessage(), |
|
98
|
|
|
'file: ' . $e->getFile(), |
|
99
|
|
|
'line: ' . $e->getLine() |
|
100
|
|
|
]); |
|
101
|
|
|
return Command::FAILURE; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getCommandNameFromServer(): string |
|
106
|
|
|
{ |
|
107
|
|
|
return $_SERVER['argv'][0]; |
|
108
|
|
|
} |
|
109
|
|
|
} |