|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
|
11
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use Yiisoft\FriendlyException\FriendlyExceptionInterface; |
|
14
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationShutdown; |
|
15
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationStartup; |
|
16
|
|
|
|
|
17
|
|
|
use function count; |
|
18
|
|
|
use function explode; |
|
19
|
|
|
|
|
20
|
|
|
final class Application extends \Symfony\Component\Console\Application |
|
21
|
|
|
{ |
|
22
|
|
|
public const NAME = 'Yii Console'; |
|
23
|
|
|
public const VERSION = '1.0'; |
|
24
|
|
|
|
|
25
|
|
|
private ?EventDispatcherInterface $dispatcher = null; |
|
26
|
|
|
|
|
27
|
49 |
|
public function __construct(string $name = self::NAME, string $version = self::VERSION) |
|
28
|
|
|
{ |
|
29
|
49 |
|
parent::__construct($name, $version); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
49 |
|
public function setDispatcher(EventDispatcherInterface $dispatcher): void |
|
33
|
|
|
{ |
|
34
|
49 |
|
$this->dispatcher = $dispatcher; |
|
35
|
49 |
|
parent::setDispatcher($dispatcher); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
public function start(?ArgvInput $input = null): void |
|
39
|
|
|
{ |
|
40
|
3 |
|
if ($this->dispatcher !== null && $input !== null) { |
|
41
|
|
|
$this->dispatcher->dispatch(new ApplicationStartup($input->getFirstArgument())); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
public function shutdown(int $exitCode): void |
|
46
|
|
|
{ |
|
47
|
3 |
|
if ($this->dispatcher !== null) { |
|
48
|
3 |
|
$this->dispatcher->dispatch(new ApplicationShutdown($exitCode)); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
public function renderThrowable(Throwable $e, OutputInterface $output): void |
|
53
|
|
|
{ |
|
54
|
3 |
|
$output->writeln('', OutputInterface::VERBOSITY_QUIET); |
|
55
|
|
|
|
|
56
|
3 |
|
$this->doRenderThrowable($e, $output); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
3 |
|
protected function doRenderThrowable(Throwable $e, OutputInterface $output): void |
|
60
|
|
|
{ |
|
61
|
3 |
|
parent::doRenderThrowable($e, $output); |
|
62
|
|
|
|
|
63
|
|
|
// Friendly Exception support |
|
64
|
3 |
|
if ($e instanceof FriendlyExceptionInterface) { |
|
65
|
3 |
|
if ($output instanceof StyleInterface) { |
|
66
|
1 |
|
$output->title($e->getName()); |
|
67
|
1 |
|
if (($solution = $e->getSolution()) !== null) { |
|
68
|
1 |
|
$output->note($solution); |
|
69
|
|
|
} |
|
70
|
1 |
|
$output->newLine(); |
|
71
|
|
|
} else { |
|
72
|
2 |
|
$output->writeln('<fg=red>' . $e->getName() . '</>'); |
|
73
|
2 |
|
if (($solution = $e->getSolution()) !== null) { |
|
74
|
2 |
|
$output->writeln('<fg=yellow>' . $solution . '</>'); |
|
75
|
|
|
} |
|
76
|
2 |
|
$output->writeln(''); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
$output->writeln($e->getTraceAsString()); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
49 |
|
public function addOptions(InputOption $options): void |
|
84
|
|
|
{ |
|
85
|
49 |
|
$this |
|
86
|
49 |
|
->getDefinition() |
|
87
|
49 |
|
->addOption($options); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
7 |
|
public function extractNamespace(string $name, int $limit = null): string |
|
91
|
|
|
{ |
|
92
|
7 |
|
return parent::extractNamespace(str_replace('/', ':', $name), $limit); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
1 |
|
public function getNamespaces(): array |
|
96
|
|
|
{ |
|
97
|
1 |
|
$namespaces = []; |
|
98
|
1 |
|
foreach ($this->all() as $command) { |
|
99
|
1 |
|
if ($command->isHidden()) { |
|
100
|
1 |
|
continue; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
$namespaces[] = $this->extractAllNamespaces($command->getName()); |
|
104
|
|
|
|
|
105
|
|
|
/** @var string $alias */ |
|
106
|
1 |
|
foreach ($command->getAliases() as $alias) { |
|
107
|
1 |
|
$namespaces[] = $this->extractAllNamespaces($alias); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
1 |
|
return array_values(array_unique(array_filter(array_merge([], ...$namespaces)))); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return string[] |
|
116
|
|
|
*/ |
|
117
|
1 |
|
private function extractAllNamespaces(?string $name): array |
|
118
|
|
|
{ |
|
119
|
1 |
|
if ($name === null) { |
|
120
|
|
|
return []; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
1 |
|
$name = str_replace('/', ':', $name); |
|
124
|
|
|
|
|
125
|
1 |
|
$parts = explode(':', $name, -1); |
|
126
|
1 |
|
$namespaces = []; |
|
127
|
|
|
|
|
128
|
1 |
|
foreach ($parts as $part) { |
|
129
|
1 |
|
if (count($namespaces)) { |
|
130
|
|
|
$namespaces[] = end($namespaces) . ':' . $part; |
|
131
|
|
|
} else { |
|
132
|
1 |
|
$namespaces[] = $part; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
return $namespaces; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|