|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Runner\Console; |
|
6
|
|
|
|
|
7
|
|
|
use Error; |
|
8
|
|
|
use ErrorException; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use Psr\Container\ContainerInterface; |
|
11
|
|
|
use Yiisoft\Di\Container; |
|
12
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
|
13
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
|
14
|
|
|
use Yiisoft\Definitions\Exception\NotFoundException; |
|
15
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
|
16
|
|
|
use Yiisoft\Yii\Console\Application; |
|
17
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
|
18
|
|
|
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput; |
|
19
|
|
|
use Yiisoft\Yii\Runner\BootstrapRunner; |
|
20
|
|
|
use Yiisoft\Yii\Runner\ConfigFactory; |
|
21
|
|
|
use Yiisoft\Yii\Runner\RunnerInterface; |
|
22
|
|
|
|
|
23
|
|
|
final class ConsoleApplicationRunner implements RunnerInterface |
|
24
|
|
|
{ |
|
25
|
|
|
private bool $debug; |
|
26
|
|
|
private ?string $environment; |
|
27
|
|
|
private string $rootPath; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(string $rootPath, bool $debug, ?string $environment) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->rootPath = $rootPath; |
|
32
|
|
|
$this->debug = $debug; |
|
33
|
|
|
$this->environment = $environment; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException|NotFoundException |
|
38
|
|
|
* @throws NotInstantiableException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function run(): void |
|
41
|
|
|
{ |
|
42
|
|
|
$config = ConfigFactory::create($this->rootPath, $this->environment); |
|
43
|
|
|
|
|
44
|
|
|
$container = new Container( |
|
45
|
|
|
$config->get('console'), |
|
46
|
|
|
$config->get('providers-console'), |
|
47
|
|
|
[], |
|
48
|
|
|
$this->debug, |
|
49
|
|
|
$config->get('delegates-console') |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
$container = $container->get(ContainerInterface::class); |
|
53
|
|
|
|
|
54
|
|
|
// Run bootstrap |
|
55
|
|
|
$this->runBootstrap($container, $config->get('bootstrap-console')); |
|
56
|
|
|
|
|
57
|
|
|
/** @var Application */ |
|
58
|
|
|
$application = $container->get(Application::class); |
|
59
|
|
|
$exitCode = ExitCode::UNSPECIFIED_ERROR; |
|
60
|
|
|
|
|
61
|
|
|
try { |
|
62
|
|
|
$application->start(); |
|
63
|
|
|
$exitCode = $application->run(null, new ConsoleBufferedOutput()); |
|
64
|
|
|
} catch (Error $error) { |
|
65
|
|
|
$application->renderThrowable($error, new ConsoleBufferedOutput()); |
|
66
|
|
|
} finally { |
|
67
|
|
|
$application->shutdown($exitCode); |
|
68
|
|
|
exit($exitCode); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
private function runBootstrap(ContainerInterface $container, array $bootstrapList): void |
|
73
|
|
|
{ |
|
74
|
|
|
(new BootstrapRunner($container, $bootstrapList))->run(); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.