1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Console; |
6
|
|
|
|
7
|
|
|
use Exception; |
8
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
9
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface; |
10
|
|
|
use Symfony\Component\Console\Input\InputOption; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use Symfony\Component\Console\Style\StyleInterface; |
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
|
|
|
private EventDispatcherInterface $dispatcher; |
22
|
|
|
|
23
|
2 |
|
public function __construct(string $name = 'Yii Console', string $version = self::VERSION) |
24
|
|
|
{ |
25
|
2 |
|
parent::__construct($name, $version); |
26
|
2 |
|
} |
27
|
|
|
|
28
|
2 |
|
public function setDispatcher(SymfonyEventDispatcherInterface $dispatcher): void |
29
|
|
|
{ |
30
|
2 |
|
$this->dispatcher = $dispatcher; |
31
|
2 |
|
parent::setDispatcher($dispatcher); |
32
|
2 |
|
} |
33
|
|
|
|
34
|
|
|
public function getDispatcher(): EventDispatcherInterface |
35
|
|
|
{ |
36
|
|
|
return $this->dispatcher; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function start(): void |
40
|
|
|
{ |
41
|
|
|
$this->dispatcher->dispatch(new ApplicationStartup()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function shutdown(int $exitCode): void |
45
|
|
|
{ |
46
|
|
|
$this->dispatcher->dispatch(new ApplicationShutdown($exitCode)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function doRenderException(Exception $e, OutputInterface $output): void |
50
|
|
|
{ |
51
|
|
|
parent::doRenderException($e, $output); |
|
|
|
|
52
|
|
|
// Friendly Exception support |
53
|
|
|
if ($e instanceof FriendlyExceptionInterface) { |
54
|
|
|
if ($output instanceof StyleInterface) { |
55
|
|
|
$output->title($e->getName()); |
56
|
|
|
$output->note($e->getSolution()); |
57
|
|
|
$output->newLine(); |
58
|
|
|
} else { |
59
|
|
|
$output->writeln([ |
60
|
|
|
'<fg=red>' . $e->getName() . '</>', |
61
|
|
|
'<fg=yellow>' . $e->getSolution() . '</>', |
62
|
|
|
'', |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
public function addOptions(InputOption $options): void |
69
|
|
|
{ |
70
|
2 |
|
$this->getDefinition()->addOption($options); |
71
|
2 |
|
} |
72
|
|
|
} |
73
|
|
|
|