|
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
|
2 |
|
public function add(BaseCommand $command) |
|
22
|
|
|
{ |
|
23
|
2 |
|
return $this->addStrict($command); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
public function addStrict(BaseCommand $command) : ? BaseCommand |
|
27
|
|
|
{ |
|
28
|
2 |
|
$out = parent::add($command); |
|
29
|
|
|
|
|
30
|
2 |
|
if ($out instanceof Command) { |
|
31
|
2 |
|
$maybeFramework = $this->GetDaftFramework(); |
|
32
|
|
|
|
|
33
|
2 |
|
if ( ! ($maybeFramework instanceof Framework)) { |
|
34
|
2 |
|
throw new BadMethodCallException( |
|
35
|
2 |
|
'Cannot add a daft framework command without a framework being attached!' |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
$out->AttachDaftFramework($maybeFramework); |
|
40
|
2 |
|
} elseif ($command instanceof Command) { |
|
41
|
2 |
|
$command->DetachDaftFramework(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
2 |
|
return $out; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
2 |
|
public function GetCommandCollector() : StaticMethodCollector |
|
48
|
|
|
{ |
|
49
|
2 |
|
return new StaticMethodCollector( |
|
50
|
|
|
[ |
|
51
|
2 |
|
DaftConsoleSource::class => [ |
|
52
|
|
|
'DaftFrameworkConsoleSources' => [ |
|
53
|
|
|
DaftConsoleSource::class, |
|
54
|
|
|
BaseCommand::class, |
|
55
|
|
|
Command::class, |
|
56
|
|
|
], |
|
57
|
|
|
], |
|
58
|
|
|
], |
|
59
|
|
|
[ |
|
60
|
2 |
|
BaseCommand::class, |
|
61
|
|
|
Command::class, |
|
62
|
|
|
] |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
public function CollectCommands(string ...$sources) : void |
|
67
|
|
|
{ |
|
68
|
4 |
|
$framework = $this->GetDaftFramework(); |
|
69
|
|
|
|
|
70
|
4 |
|
if ( ! ($framework instanceof Framework)) { |
|
71
|
2 |
|
throw new BadMethodCallException( |
|
72
|
2 |
|
'Cannot collect commands without an attached framework instance!' |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
2 |
|
foreach ($this->GetCommandCollector()->Collect(...$sources) as $implementation) { |
|
77
|
2 |
|
if (is_a($implementation, BaseCommand::class, true)) { |
|
78
|
|
|
/** |
|
79
|
|
|
* @var BaseCommand $command |
|
80
|
|
|
*/ |
|
81
|
2 |
|
$command = new $implementation($implementation::getDefaultName()); |
|
82
|
|
|
|
|
83
|
2 |
|
$this->add($command); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
2 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return static |
|
90
|
|
|
*/ |
|
91
|
2 |
|
public static function CollectApplicationWithCommands( |
|
92
|
|
|
string $name, |
|
93
|
|
|
string $version, |
|
94
|
|
|
Framework $framework |
|
95
|
|
|
) : self { |
|
96
|
2 |
|
$application = new static($name, $version); |
|
97
|
2 |
|
$application->AttachDaftFramework($framework); |
|
98
|
|
|
|
|
99
|
2 |
|
$config = ($framework->ObtainConfig()[DaftConsoleSource::class] ?? []); |
|
100
|
|
|
|
|
101
|
2 |
|
$application->CollectCommands(...array_values(is_array($config) ? $config : [])); |
|
102
|
|
|
|
|
103
|
2 |
|
return $application; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|