1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace Yiisoft\Yii\Runner\Console; |
||
6 | |||
7 | use ErrorException; |
||
8 | use Exception; |
||
9 | use Psr\Container\ContainerExceptionInterface; |
||
10 | use Psr\Container\NotFoundExceptionInterface; |
||
11 | use Symfony\Component\Console\Input\ArgvInput; |
||
12 | use Throwable; |
||
13 | use Yiisoft\Definitions\Exception\CircularReferenceException; |
||
14 | use Yiisoft\Definitions\Exception\InvalidConfigException; |
||
15 | use Yiisoft\Definitions\Exception\NotInstantiableException; |
||
16 | use Yiisoft\Di\NotFoundException; |
||
17 | use Yiisoft\Yii\Console\Application; |
||
18 | use Yiisoft\Yii\Console\ExitCode; |
||
19 | use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput; |
||
20 | use Yiisoft\Yii\Runner\ApplicationRunner; |
||
21 | |||
22 | /** |
||
23 | * `ConsoleApplicationRunner` runs the Yii console application. |
||
24 | */ |
||
25 | final class ConsoleApplicationRunner extends ApplicationRunner |
||
26 | { |
||
27 | /** |
||
28 | * @param string $rootPath The absolute path to the project root. |
||
29 | * @param bool $debug Whether the debug mode is enabled. |
||
30 | * @param bool $checkEvents Whether to check events' configuration. |
||
31 | * @param string|null $environment The environment name. |
||
32 | * @param string $bootstrapGroup The bootstrap configuration group name. |
||
33 | * @param string $eventsGroup The events' configuration group name. |
||
34 | * @param string $diGroup The container definitions' configuration group name. |
||
35 | * @param string $diProvidersGroup The container providers' configuration group name. |
||
36 | * @param string $diDelegatesGroup The container delegates' configuration group name. |
||
37 | * @param string $diTagsGroup The container tags' configuration group name. |
||
38 | * @param string $paramsGroup The configuration parameters group name. |
||
39 | * @param array $nestedParamsGroups Configuration group names that are included into configuration parameters group. |
||
40 | * This is needed for recursive merging of parameters. |
||
41 | * @param array $nestedEventsGroups Configuration group names that are included into events' configuration group. |
||
42 | * This is needed for reverse and recursive merge of events' configurations. |
||
43 | * @param object[] $configModifiers Modifiers for {@see Config}. |
||
44 | * |
||
45 | * @psalm-param list<string> $nestedParamsGroups |
||
46 | * @psalm-param list<string> $nestedEventsGroups |
||
47 | * @psalm-param list<object> $configModifiers |
||
48 | */ |
||
49 | public function __construct( |
||
50 | string $rootPath, |
||
51 | bool $debug = false, |
||
52 | bool $checkEvents = false, |
||
53 | ?string $environment = null, |
||
54 | string $bootstrapGroup = 'bootstrap-console', |
||
55 | string $eventsGroup = 'events-console', |
||
56 | string $diGroup = 'di-console', |
||
57 | string $diProvidersGroup = 'di-providers-console', |
||
58 | string $diDelegatesGroup = 'di-delegates-console', |
||
59 | string $diTagsGroup = 'di-tags-console', |
||
60 | string $paramsGroup = 'params-console', |
||
61 | array $nestedParamsGroups = ['params'], |
||
62 | array $nestedEventsGroups = ['events'], |
||
63 | array $configModifiers = [], |
||
64 | ) { |
||
65 | parent::__construct( |
||
66 | $rootPath, |
||
67 | $debug, |
||
68 | $checkEvents, |
||
69 | $environment, |
||
70 | $bootstrapGroup, |
||
71 | $eventsGroup, |
||
72 | $diGroup, |
||
73 | $diProvidersGroup, |
||
74 | $diDelegatesGroup, |
||
75 | $diTagsGroup, |
||
76 | $paramsGroup, |
||
77 | $nestedParamsGroups, |
||
78 | $nestedEventsGroups, |
||
79 | $configModifiers, |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritDoc} |
||
85 | * |
||
86 | * @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException |
||
87 | * @throws ContainerExceptionInterface|NotFoundException|NotFoundExceptionInterface|NotInstantiableException |
||
88 | */ |
||
89 | public function run(): void |
||
90 | { |
||
91 | $this->runBootstrap(); |
||
92 | $this->checkEvents(); |
||
93 | |||
94 | /** @var Application $application */ |
||
95 | $application = $this->getContainer()->get(Application::class); |
||
96 | $exitCode = ExitCode::UNSPECIFIED_ERROR; |
||
97 | |||
98 | $input = new ArgvInput(); |
||
99 | $output = new ConsoleBufferedOutput(); |
||
100 | |||
101 | try { |
||
102 | $application->start($input); |
||
103 | $exitCode = $application->run($input, $output); |
||
104 | } catch (Throwable $throwable) { |
||
105 | $application->renderThrowable($throwable, $output); |
||
106 | } finally { |
||
107 | $application->shutdown($exitCode); |
||
108 | exit($exitCode); |
||
0 ignored issues
–
show
|
|||
109 | } |
||
110 | } |
||
111 | } |
||
112 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.