|
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 Throwable; |
|
12
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
|
13
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
14
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
|
15
|
|
|
use Yiisoft\Di\NotFoundException; |
|
16
|
|
|
use Yiisoft\Yii\Console\Application; |
|
17
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
|
18
|
|
|
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput; |
|
19
|
|
|
use Yiisoft\Yii\Runner\ApplicationRunner; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* `ConsoleApplicationRunner` runs the Yii console application. |
|
23
|
|
|
*/ |
|
24
|
|
|
final class ConsoleApplicationRunner extends ApplicationRunner |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $rootPath The absolute path to the project root. |
|
28
|
|
|
* @param bool $debug Whether the debug mode is enabled. |
|
29
|
|
|
* @param string|null $environment The environment name. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(string $rootPath, bool $debug, ?string $environment) |
|
32
|
|
|
{ |
|
33
|
|
|
parent::__construct($rootPath, $debug, $environment); |
|
34
|
|
|
$this->bootstrapGroup = 'bootstrap-console'; |
|
35
|
|
|
$this->eventsGroup = 'events-console'; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritDoc} |
|
40
|
|
|
* |
|
41
|
|
|
* @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException |
|
42
|
|
|
* @throws ContainerExceptionInterface|NotFoundException|NotFoundExceptionInterface|NotInstantiableException |
|
43
|
|
|
*/ |
|
44
|
|
|
public function run(): void |
|
45
|
|
|
{ |
|
46
|
|
|
$config = $this->getConfig(); |
|
47
|
|
|
$container = $this->getContainer($config, 'console'); |
|
48
|
|
|
|
|
49
|
|
|
$this->runBootstrap($config, $container); |
|
50
|
|
|
$this->checkEvents($config, $container); |
|
51
|
|
|
|
|
52
|
|
|
/** @var Application */ |
|
53
|
|
|
$application = $container->get(Application::class); |
|
54
|
|
|
$exitCode = ExitCode::UNSPECIFIED_ERROR; |
|
55
|
|
|
|
|
56
|
|
|
try { |
|
57
|
|
|
$application->start(); |
|
58
|
|
|
$exitCode = $application->run(null, new ConsoleBufferedOutput()); |
|
59
|
|
|
} catch (Throwable $throwable) { |
|
60
|
|
|
$application->renderThrowable($throwable, new ConsoleBufferedOutput()); |
|
61
|
|
|
} finally { |
|
62
|
|
|
$application->shutdown($exitCode); |
|
63
|
|
|
exit($exitCode); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.