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\DebugCommand; |
8
|
|
|
use pjpawel\LightApi\Command\Internal\KernelAwareCommand; |
9
|
|
|
use pjpawel\LightApi\Command\Output\Stdout; |
10
|
|
|
use pjpawel\LightApi\Container\ClassDefinition; |
11
|
|
|
use pjpawel\LightApi\Container\ContainerLoader; |
12
|
|
|
use pjpawel\LightApi\Container\LazyServiceInterface; |
13
|
|
|
use pjpawel\LightApi\Kernel; |
14
|
|
|
use ReflectionClass; |
15
|
|
|
use ReflectionNamedType; |
16
|
|
|
|
17
|
|
|
class CommandsLoader |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
private const KERNEL_COMMANDS = [ |
21
|
|
|
'kernel:debug' => DebugCommand::class |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array<string,string> |
26
|
|
|
*/ |
27
|
|
|
public array $command = []; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function __construct() |
31
|
|
|
{ |
32
|
|
|
$this->command = self::KERNEL_COMMANDS; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function registerCommand(string $commandName, string $className): void |
36
|
|
|
{ |
37
|
|
|
$this->command[$commandName] = $className; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $commandName |
42
|
|
|
* @param ContainerLoader $container |
43
|
|
|
* @return int |
44
|
|
|
*/ |
45
|
|
|
public function runCommandFromName(string $commandName, ContainerLoader $container, ?Kernel $kernel = null): int |
46
|
|
|
{ |
47
|
|
|
$stdout = new Stdout(); |
48
|
|
|
try { |
49
|
|
|
$className = $this->command[$commandName]; |
50
|
|
|
$reflectionClass = new ReflectionClass($className); |
51
|
|
|
$constructor = $reflectionClass->getConstructor(); |
52
|
|
|
if ($constructor !== null) { |
53
|
|
|
/** @var ClassDefinition $classDefinition */ |
54
|
|
|
$classDefinition = $container->get($className); |
55
|
|
|
$args = $classDefinition->arguments; |
56
|
|
|
foreach ($constructor->getParameters() as $parameter) { |
57
|
|
|
$parameterType = $parameter->getType(); |
58
|
|
|
if ($parameterType instanceof ReflectionNamedType) { |
59
|
|
|
$args[] = $container->get($parameterType->getName()); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
$args = []; |
64
|
|
|
} |
65
|
|
|
$stdin = new Stdin(); |
66
|
|
|
/** @var Command $command */ |
67
|
|
|
$command = $reflectionClass->newInstanceArgs($args); |
68
|
|
|
$command->prepare($stdin); |
69
|
|
|
/* Prepare input */ |
70
|
|
|
$stdin->load(); |
71
|
|
|
/* Inject services */ |
72
|
|
|
if (is_subclass_of($command, LazyServiceInterface::class)) { |
73
|
|
|
$command->setContainer($container->prepareContainerBag($command::getAllServices())); |
|
|
|
|
74
|
|
|
} |
75
|
|
|
/* If $command is KernelAwareCommand set Kernel */ |
76
|
|
|
if (!is_null($kernel) && is_subclass_of($command, KernelAwareCommand::class)) { |
77
|
|
|
$command->setKernel($kernel); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
return $command->execute($stdin, $stdout); |
80
|
|
|
} catch (Exception $e) { |
81
|
|
|
$stdout->writeln([ |
82
|
|
|
'Exception thrown during command', |
83
|
|
|
$e->getMessage(), |
84
|
|
|
'file: ' . $e->getFile(), |
85
|
|
|
'line: ' . $e->getLine() |
86
|
|
|
]); |
87
|
|
|
return Command::FAILURE; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getCommandNameFromServer(): string |
92
|
|
|
{ |
93
|
|
|
return $_SERVER['argv'][0]; |
94
|
|
|
} |
95
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.