Passed
Push — master ( 444a41...f3d99f )
by Alexander
03:12
created

ConsoleApplicationRunner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Runner;
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\Output\ConsoleBufferedOutput;
19
20
final class ConsoleApplicationRunner
21
{
22
    private bool $debug;
23
    private ?string $environment;
24
25
    public function __construct(bool $debug, ?string $environment)
26
    {
27
        $this->debug = $debug;
28
        $this->environment = $environment;
29
    }
30
31
    /**
32
     * @throws CircularReferenceException|ErrorException|Exception|InvalidConfigException|NotFoundException
33
     * @throws NotInstantiableException
34
     */
35
    public function run(): void
36
    {
37
        $config = new Config(
38
            dirname(__DIR__, 2),
39
            '/config/packages', // Configs path.
40
            $this->environment,
41
            [
42
                'params',
43
                'events',
44
                'events-web',
45
                'events-console',
46
            ],
47
        );
48
49
        $container = new Container($config->get('console'), $config->get('providers-console'), [], $this->debug);
50
        $container = $container->get(ContainerInterface::class);
51
52
        // Run bootstrap
53
        $this->runBootstrap($container, $config->get('bootstrap-console'));
54
55
        /** @var Application */
56
        $application = $container->get(Application::class);
57
        $exitCode = 1;
58
59
        try {
60
            $application->start();
61
            $exitCode = $application->run(null, new ConsoleBufferedOutput());
62
        } catch (Error $error) {
63
            $application->renderThrowable($error, new ConsoleBufferedOutput());
64
        } finally {
65
            $application->shutdown($exitCode);
66
            exit($exitCode);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
67
        }
68
    }
69
70
    private function runBootstrap(ContainerInterface $container, array $bootstrapList): void
71
    {
72
        (new BootstrapRunner($container, $bootstrapList))->run();
73
    }
74
}
75