-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCli.php
104 lines (94 loc) · 3.22 KB
/
Cli.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
/**
* @author Artur Kyryliuk <mail@artur.work>
*/
namespace App;
use Garden\Cli\Application\CliApplication;
class Cli extends CliApplication
{
const OPT_REQUIRED_ALL = 'requiredAll';
/**
* @param array<int, string> $argv
*/
public function run(array $argv = []): int
{
return $this->main(count($argv) === 1 ? array_merge($argv, ['start']) : $argv);
}
/**
* Override until https://github.com/vanilla/garden-cli/pull/48 will be merged
* @param string $className
* @param string $methodName
* @param array<string, mixed> $options
* @return $this
* @throws \ReflectionException
*/
public function addMethod(
string $className,
string $methodName,
array $options = []
): self {
parent::addMethod($className, $methodName, $options += [self::OPT_REQUIRED_ALL => false]);
$class = new \ReflectionClass($className);
$method = new \ReflectionMethod($className, $methodName);
$setterFilter = [
$this,
$method->isStatic() ? 'staticSetterFilter' : 'setterFilter',
];
if ($options[self::OPT_SETTERS]) {
$this->addSettersCustom($class, $setterFilter, $options);
}
return $this;
}
/**
* Customized addSetters until https://github.com/vanilla/garden-cli/pull/48 will be merged
* @param \ReflectionClass<object> $class
* @param callable|null $filter
* @param array<string, mixed> $options
* @return void
*/
public function addSettersCustom(
\ReflectionClass $class,
callable $filter = null,
array $options = [self::OPT_REQUIRED_ALL => false]
): void {
foreach (
$this->reflectSetters($class, $filter)
as $optName => $method
) {
$param = $method->getParameters()[0];
if (null === ($t = $param->getType())) {
$type = "string";
} else {
$type =
$t instanceof \ReflectionNamedType
? $t->getName()
: (string)$t;
}
if (!empty($method->getDocComment())) {
$doc = $this->docBlocks()->create($method);
$description = $doc->getSummary();
} else {
$description = "";
}
$this->opt($optName, $description, $options[self::OPT_REQUIRED_ALL], $type, [
self::META_DISPATCH_TYPE => self::TYPE_CALL,
self::META_DISPATCH_VALUE => $method->getName(),
]);
}
}
protected function configureCli(): void
{
parent::configureCli();
$this->getContainer()->rule(\Garden\Cli\StreamLogger::class)->setConstructorArgs([STDERR])
->addCall('setTimeFormat', ['Y-m-d H:i:s']);
$this->getContainer()->setShared(true);
$this->getContainer()->rule(\Psr\Log\LoggerInterface::class)->setAliasOf(\Garden\Cli\StreamLogger::class);
$this->addCommandClass(\App\Command\Start::class);
$this->addCommandClass(
\App\Command\PaypalToken::class,
'run',
[self::OPT_REQUIRED_ALL => true]
);
}
}