1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace pjpawel\LightApi\Command; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use pjpawel\LightApi\Command\Input\Stdin; |
7
|
|
|
use pjpawel\LightApi\Command\Output\Stdout; |
8
|
|
|
use pjpawel\LightApi\Container\ClassDefinition; |
9
|
|
|
use pjpawel\LightApi\Container\ContainerLoader; |
10
|
|
|
use pjpawel\LightApi\Container\LazyServiceInterface; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use ReflectionNamedType; |
13
|
|
|
|
14
|
|
|
class CommandsLoader |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var array<string,string> |
19
|
|
|
*/ |
20
|
|
|
public array $command = []; |
21
|
|
|
public bool $loaded = true; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
public function registerCommand(string $commandName, string $className): void |
25
|
|
|
{ |
26
|
|
|
$this->command[$commandName] = $className; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param string $commandName |
31
|
|
|
* @param ContainerLoader $container |
32
|
|
|
* @return int |
33
|
|
|
*/ |
34
|
|
|
public function runCommandFromName(string $commandName, ContainerLoader $container): int |
35
|
|
|
{ |
36
|
|
|
$stdout = new Stdout(); |
37
|
|
|
try { |
38
|
|
|
$className = $this->command[$commandName]; |
39
|
|
|
$reflectionClass = new ReflectionClass($className); |
40
|
|
|
$constructor = $reflectionClass->getConstructor(); |
41
|
|
|
if ($constructor !== null) { |
42
|
|
|
/** @var ClassDefinition $classDefinition */ |
43
|
|
|
$classDefinition = $container->get($className); |
44
|
|
|
$args = $classDefinition->arguments; |
45
|
|
|
foreach ($constructor->getParameters() as $parameter) { |
46
|
|
|
$parameterType = $parameter->getType(); |
47
|
|
|
if ($parameterType instanceof ReflectionNamedType) { |
48
|
|
|
$args[] = $container->get($parameterType->getName()); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
} else { |
52
|
|
|
$args = []; |
53
|
|
|
} |
54
|
|
|
$stdin = new Stdin(); |
55
|
|
|
/** @var Command $command */ |
56
|
|
|
$command = $reflectionClass->newInstanceArgs($args); |
57
|
|
|
$command->prepare($stdin); |
58
|
|
|
/* Prepare input */ |
59
|
|
|
$stdin->load(); |
60
|
|
|
/* Inject services */ |
61
|
|
|
if (is_subclass_of($command, LazyServiceInterface::class)) { |
62
|
|
|
$command->setContainer($container->prepareContainerBag($command::getAllServices())); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
return $command->execute($stdin, $stdout); |
65
|
|
|
} catch (Exception $e) { |
66
|
|
|
$stdout->writeln([ |
67
|
|
|
'Exception thrown during command', |
68
|
|
|
$e->getMessage(), |
69
|
|
|
'file: ' . $e->getFile(), |
70
|
|
|
'line: ' . $e->getLine() |
71
|
|
|
]); |
72
|
|
|
return Command::FAILURE; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getCommandNameFromServer(): string |
77
|
|
|
{ |
78
|
|
|
return $_SERVER['argv'][0]; |
79
|
|
|
} |
80
|
|
|
} |
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.