|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
|
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 as SymfonyEventDispatcherInterface; |
|
12
|
|
|
use Throwable; |
|
13
|
|
|
use Yiisoft\FriendlyException\FriendlyExceptionInterface; |
|
14
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationShutdown; |
|
15
|
|
|
use Yiisoft\Yii\Console\Event\ApplicationStartup; |
|
16
|
|
|
|
|
17
|
|
|
class Application extends \Symfony\Component\Console\Application |
|
18
|
|
|
{ |
|
19
|
|
|
public const VERSION = '3.0.0-dev'; |
|
20
|
|
|
|
|
21
|
|
|
/** @psalm-suppress PropertyNotSetInConstructor */ |
|
22
|
|
|
private EventDispatcherInterface $dispatcher; |
|
23
|
|
|
|
|
24
|
31 |
|
public function __construct(string $name = 'Yii Console', string $version = self::VERSION) |
|
25
|
|
|
{ |
|
26
|
31 |
|
parent::__construct($name, $version); |
|
27
|
31 |
|
} |
|
28
|
|
|
|
|
29
|
31 |
|
public function setDispatcher(SymfonyEventDispatcherInterface $dispatcher): void |
|
30
|
|
|
{ |
|
31
|
31 |
|
$this->dispatcher = $dispatcher; |
|
32
|
31 |
|
parent::setDispatcher($dispatcher); |
|
33
|
31 |
|
} |
|
34
|
|
|
|
|
35
|
2 |
|
public function getDispatcher(): EventDispatcherInterface |
|
36
|
|
|
{ |
|
37
|
2 |
|
return $this->dispatcher; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
3 |
|
public function start(): void |
|
41
|
|
|
{ |
|
42
|
3 |
|
$this->dispatcher->dispatch(new ApplicationStartup()); |
|
43
|
3 |
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
public function shutdown(int $exitCode): void |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->dispatcher->dispatch(new ApplicationShutdown($exitCode)); |
|
48
|
3 |
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
public function renderThrowable(Throwable $e, OutputInterface $output): void |
|
51
|
|
|
{ |
|
52
|
3 |
|
$output->writeln('', OutputInterface::VERBOSITY_QUIET); |
|
53
|
|
|
|
|
54
|
3 |
|
$this->doRenderThrowable($e, $output); |
|
55
|
3 |
|
} |
|
56
|
|
|
|
|
57
|
3 |
|
protected function doRenderThrowable(Throwable $e, OutputInterface $output): void |
|
58
|
|
|
{ |
|
59
|
3 |
|
parent::doRenderThrowable($e, $output); |
|
60
|
|
|
// Friendly Exception support |
|
61
|
3 |
|
if ($e instanceof FriendlyExceptionInterface) { |
|
62
|
3 |
|
if ($output instanceof StyleInterface) { |
|
63
|
1 |
|
$output->title($e->getName()); |
|
64
|
1 |
|
if (($solution = $e->getSolution()) !== null) { |
|
65
|
1 |
|
$output->note($solution); |
|
66
|
|
|
} |
|
67
|
1 |
|
$output->newLine(); |
|
68
|
|
|
} else { |
|
69
|
2 |
|
$output->writeln('<fg=red>' . $e->getName() . '</>'); |
|
70
|
2 |
|
if (($solution = $e->getSolution()) !== null) { |
|
71
|
2 |
|
$output->writeln('<fg=yellow>' . $solution . '</>'); |
|
72
|
|
|
} |
|
73
|
2 |
|
$output->writeln(''); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
3 |
|
} |
|
77
|
|
|
|
|
78
|
31 |
|
public function addOptions(InputOption $options): void |
|
79
|
|
|
{ |
|
80
|
31 |
|
$this->getDefinition()->addOption($options); |
|
81
|
31 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function extractNamespace(string $name, int $limit = null) |
|
84
|
|
|
{ |
|
85
|
|
|
$parts = explode('/', $name, -1); |
|
86
|
|
|
|
|
87
|
|
|
return implode('/', null === $limit ? $parts : \array_slice($parts, 0, $limit)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|