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\Config\Config; |
12
|
|
|
use Yiisoft\Di\Container; |
13
|
|
|
use Yiisoft\Definitions\Exception\CircularReferenceException; |
14
|
|
|
use Yiisoft\Definitions\Exception\InvalidConfigException; |
15
|
|
|
use Yiisoft\Definitions\Exception\NotFoundException; |
16
|
|
|
use Yiisoft\Definitions\Exception\NotInstantiableException; |
17
|
|
|
use Yiisoft\Yii\Console\Application; |
18
|
|
|
use Yiisoft\Yii\Console\ExitCode; |
19
|
|
|
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput; |
20
|
|
|
use Yiisoft\Yii\Runner\BootstrapRunner; |
21
|
|
|
use Yiisoft\Yii\Runner\ConfigFactory; |
22
|
|
|
use Yiisoft\Yii\Runner\RunnerInterface; |
23
|
|
|
|
24
|
|
|
final class ConsoleApplicationRunner implements RunnerInterface |
25
|
|
|
{ |
26
|
|
|
private bool $debug; |
27
|
|
|
private string $rootPath; |
28
|
|
|
private ?string $environment; |
29
|
|
|
private ?Config $config = null; |
30
|
|
|
private ?ContainerInterface $container = null; |
31
|
|
|
private ?string $bootstrapGroup = 'bootstrap-console'; |
32
|
|
|
|
33
|
|
|
public function __construct(string $rootPath, bool $debug, ?string $environment) |
34
|
|
|
{ |
35
|
|
|
$this->rootPath = $rootPath; |
36
|
|
|
$this->debug = $debug; |
37
|
|
|
$this->environment = $environment; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function withBootstrap(string $bootstrapGroup): self |
41
|
|
|
{ |
42
|
|
|
$new = clone $this; |
43
|
|
|
$new->bootstrapGroup = $bootstrapGroup; |
44
|
|
|
return $new; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function withoutBootstrap(): self |
48
|
|
|
{ |
49
|
|
|
$new = clone $this; |
50
|
|
|
$new->bootstrapGroup = null; |
51
|
|
|
return $new; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function withConfig(Config $config): self |
55
|
|
|
{ |
56
|
|
|
$new = clone $this; |
57
|
|
|
$new->config = $config; |
58
|
|
|
return $new; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function withContainer(ContainerInterface $container): self |
62
|
|
|
{ |
63
|
|
|
$new = clone $this; |
64
|
|
|
$new->container = $container; |
65
|
|
|
return $new; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException|NotFoundException |
70
|
|
|
* @throws NotInstantiableException |
71
|
|
|
*/ |
72
|
|
|
public function run(): void |
73
|
|
|
{ |
74
|
|
|
$config = $this->config ?? ConfigFactory::create($this->rootPath, $this->environment); |
75
|
|
|
|
76
|
|
|
$container = $this->container ?? new Container( |
77
|
|
|
$config->get('console'), |
78
|
|
|
$config->get('providers-console'), |
79
|
|
|
[], |
80
|
|
|
$this->debug, |
81
|
|
|
$config->get('delegates-console') |
82
|
|
|
); |
83
|
|
|
|
84
|
|
|
if ($container instanceof Container) { |
85
|
|
|
$container = $container->get(ContainerInterface::class); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Run bootstrap |
89
|
|
|
if ($this->bootstrapGroup !== null) { |
90
|
|
|
$this->runBootstrap($container, $config->get($this->bootstrapGroup)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** @var Application */ |
94
|
|
|
$application = $container->get(Application::class); |
95
|
|
|
$exitCode = ExitCode::UNSPECIFIED_ERROR; |
96
|
|
|
|
97
|
|
|
try { |
98
|
|
|
$application->start(); |
99
|
|
|
$exitCode = $application->run(null, new ConsoleBufferedOutput()); |
100
|
|
|
} catch (Error $error) { |
101
|
|
|
$application->renderThrowable($error, new ConsoleBufferedOutput()); |
102
|
|
|
} finally { |
103
|
|
|
$application->shutdown($exitCode); |
104
|
|
|
exit($exitCode); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function runBootstrap(ContainerInterface $container, array $bootstrapList): void |
109
|
|
|
{ |
110
|
|
|
(new BootstrapRunner($container, $bootstrapList))->run(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.