1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author SignpostMarv |
4
|
|
|
*/ |
5
|
|
|
declare(strict_types=1); |
6
|
|
|
|
7
|
|
|
namespace SignpostMarv\DaftFramework\Symfony\Console; |
8
|
|
|
|
9
|
|
|
use BadMethodCallException; |
10
|
|
|
use SignpostMarv\DaftFramework\AttachDaftFramework; |
11
|
|
|
use SignpostMarv\DaftFramework\Framework; |
12
|
|
|
use SignpostMarv\DaftFramework\Symfony\Console\Command\Command; |
13
|
|
|
use SignpostMarv\DaftInterfaceCollector\StaticMethodCollector; |
14
|
|
|
use Symfony\Component\Console\Application as Base; |
15
|
|
|
use Symfony\Component\Console\Command\Command as BaseCommand; |
16
|
|
|
|
17
|
|
|
class Application extends Base |
18
|
|
|
{ |
19
|
|
|
use AttachDaftFramework; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @return BaseCommand|null |
23
|
|
|
*/ |
24
|
1 |
|
public function add(BaseCommand $command) |
25
|
|
|
{ |
26
|
1 |
|
return $this->addStrict($command); |
27
|
|
|
} |
28
|
|
|
|
29
|
1 |
|
public function addStrict(BaseCommand $command) : ? BaseCommand |
30
|
|
|
{ |
31
|
1 |
|
$out = parent::add($command); |
32
|
|
|
|
33
|
1 |
|
if ($out instanceof Command) { |
34
|
1 |
|
$maybeFramework = $this->GetDaftFramework(); |
35
|
|
|
|
36
|
1 |
|
if ( ! ($maybeFramework instanceof Framework)) { |
37
|
1 |
|
throw new BadMethodCallException( |
38
|
1 |
|
'Cannot add a daft framework command without a framework being attached!' |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
$out->AttachDaftFramework($maybeFramework); |
43
|
1 |
|
} elseif ($command instanceof Command) { |
44
|
1 |
|
$command->DetachDaftFramework(); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
return $out; |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function GetCommandCollector() : StaticMethodCollector |
51
|
|
|
{ |
52
|
1 |
|
return new StaticMethodCollector( |
53
|
|
|
[ |
54
|
|
|
DaftConsoleSource::class => [ |
55
|
|
|
'DaftFrameworkConsoleSources' => [ |
56
|
1 |
|
DaftConsoleSource::class, |
57
|
|
|
BaseCommand::class, |
58
|
|
|
Command::class, |
59
|
|
|
], |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
[ |
63
|
1 |
|
BaseCommand::class, |
64
|
|
|
Command::class, |
65
|
|
|
] |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @psalm-param class-string ...$sources |
71
|
|
|
*/ |
72
|
2 |
|
public function CollectCommands(string ...$sources) : void |
73
|
|
|
{ |
74
|
2 |
|
$framework = $this->GetDaftFramework(); |
75
|
|
|
|
76
|
2 |
|
if ( ! ($framework instanceof Framework)) { |
77
|
1 |
|
throw new BadMethodCallException( |
78
|
1 |
|
'Cannot collect commands without an attached framework instance!' |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var iterable<scalar|array|object|null> |
84
|
|
|
*/ |
85
|
1 |
|
$implementations = $this->GetCommandCollector()->Collect(...$sources); |
86
|
|
|
|
87
|
1 |
|
foreach ($implementations as $implementation) { |
88
|
1 |
|
if (is_string($implementation) && is_a($implementation, BaseCommand::class, true)) { |
89
|
|
|
/** |
90
|
|
|
* @var BaseCommand |
91
|
|
|
*/ |
92
|
1 |
|
$command = new $implementation($implementation::getDefaultName()); |
93
|
|
|
|
94
|
1 |
|
$this->add($command); |
95
|
|
|
} |
96
|
|
|
} |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return static |
101
|
|
|
*/ |
102
|
1 |
|
public static function CollectApplicationWithCommands( |
103
|
|
|
string $name, |
104
|
|
|
string $version, |
105
|
|
|
Framework $framework |
106
|
|
|
) : self { |
107
|
1 |
|
$application = new static($name, $version); |
108
|
1 |
|
$application->AttachDaftFramework($framework); |
109
|
|
|
|
110
|
1 |
|
$config = (array) ($framework->ObtainConfig()[DaftConsoleSource::class] ?? []); |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var string[] |
114
|
|
|
* |
115
|
|
|
* @psalm-var class-string[] |
116
|
|
|
*/ |
117
|
1 |
|
$sources = array_values(array_filter($config, 'is_string')); |
118
|
|
|
|
119
|
1 |
|
$application->CollectCommands(...$sources); |
120
|
|
|
|
121
|
1 |
|
return $application; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|