-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.php
85 lines (74 loc) · 2.61 KB
/
runner.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env php
<?php
use App\Console\Command;
require_once __DIR__ . '/autoloader.php';
$consoleDirectory = __DIR__ . '/src/Console/Command';
function getClassname($directory, $file): string
{
$relativePath = str_replace($directory, '', $file->getPathname());
$relativePath = str_replace('/', '\\', $relativePath);
$relativePath = trim($relativePath, '\\');
$className = 'App\\Console\\Command\\' . $relativePath;
return str_replace('.php', '', $className);
}
function discoverCommands(string $directory, $argc, $argv): array
{
$commands = [];
foreach (
new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($directory, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::LEAVES_ONLY
) as $file) {
if ($file->isFile() && $file->getExtension() === 'php') {
$className = getClassName($directory, $file);
if (is_subclass_of($className, Command::class)) {
$instance = resolve($className);
$instance->setCliArguments($argc, $argv);
$commands[$instance->getSignature()] = $instance;
}
}
}
return $commands;
}
function resolve(string $className) {
$reflection = new ReflectionClass($className);
$constructor = $reflection->getConstructor();
if (!$constructor) {
return new $className;
}
$parameters = $constructor->getParameters();
$dependencies = [];
foreach ($parameters as $parameter) {
$type = $parameter->getType();
if ($type && !$type->isBuiltin()) {
$dependencyClass = $type->getName();
$dependencies[] = resolve($dependencyClass);
} elseif ($parameter->isDefaultValueAvailable()) {
$dependencies[] = $parameter->getDefaultValue();
} else {
throw new Exception("Cannot resolve dependency: " . $parameter->getName());
}
}
return $reflection->newInstanceArgs($dependencies);
}
function listCommands(array $commands): void
{
echo "Available commands:\n";
/** @var Command $command */
foreach ($commands as $signature => $command) {
echo $signature . " - " . $command->getDescription() . "\n";
}
}
$commands = discoverCommands($consoleDirectory, $argc, $argv);
$commandInput = $argv[1] ?? 'help';
if (in_array($commandInput, ['help', '--help', '-h', '?'], true)) {
listCommands($commands);
exit(Command::SUCCESS);
}
if (isset($commands[$commandInput])) {
$status = $commands[$commandInput]->handle($argc, $argv);
exit($status);
} else {
echo "Command not found\n";
exit(Command::INVALID);
}