Passed
Pull Request — master (#9)
by Evgeniy
02:01
created

ApplicationRunner::withoutCheckingEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Runner;
6
7
use ErrorException;
8
use Psr\Container\ContainerExceptionInterface;
9
use Psr\Container\ContainerInterface;
10
use Psr\Container\NotFoundExceptionInterface;
11
use RuntimeException;
12
use Yiisoft\Config\Config;
13
use Yiisoft\Config\ConfigInterface;
14
use Yiisoft\Config\ConfigPaths;
15
use Yiisoft\Definitions\Exception\InvalidConfigException;
16
use Yiisoft\Di\Container;
17
use Yiisoft\Di\ContainerConfig;
18
use Yiisoft\Yii\Event\ListenerConfigurationChecker;
19
20
/**
21
 * Provides basic functionality for creating adapters.
22
 */
23
abstract class ApplicationRunner implements RunnerInterface
24
{
25
    protected bool $debug;
26
    protected string $rootPath;
27
    protected ?string $environment;
28
    protected ?ConfigInterface $config = null;
29
    protected ?ContainerInterface $container = null;
30
    protected ?string $bootstrapGroup = null;
31
    protected ?string $eventsGroup = null;
32
33
    /**
34
     * @param string $rootPath The absolute path to the project root.
35
     * @param bool $debug Whether the debug mode is enabled.
36
     * @param string|null $environment The environment name.
37
     */
38 8
    public function __construct(string $rootPath, bool $debug, ?string $environment)
39
    {
40 8
        $this->rootPath = $rootPath;
41 8
        $this->debug = $debug;
42 8
        $this->environment = $environment;
43 8
    }
44
45
    abstract public function run(): void;
46
47
    /**
48
     * Returns a new instance with the specified bootstrap configuration group name.
49
     *
50
     * @param string $bootstrapGroup The bootstrap configuration group name.
51
     *
52
     * @return self
53
     */
54 3
    public function withBootstrap(string $bootstrapGroup): self
55
    {
56 3
        $new = clone $this;
57 3
        $new->bootstrapGroup = $bootstrapGroup;
58 3
        return $new;
59
    }
60
61
    /**
62
     * Returns a new instance and disables the use of bootstrap configuration group.
63
     *
64
     * @return self
65
     */
66 2
    public function withoutBootstrap(): self
67
    {
68 2
        $new = clone $this;
69 2
        $new->bootstrapGroup = null;
70 2
        return $new;
71
    }
72
73
    /**
74
     * Returns a new instance with the specified configuration group of events name for check.
75
     *
76
     * Note: The configuration of events is checked only in debug mode.
77
     *
78
     * @param string $eventsGroup The configuration group name of events for check.
79
     *
80
     * @return self
81
     */
82 3
    public function withCheckingEvents(string $eventsGroup): self
83
    {
84 3
        $new = clone $this;
85 3
        $new->eventsGroup = $eventsGroup;
86 3
        return $new;
87
    }
88
89
    /**
90
     * Returns a new instance and disables checking of the event configuration group.
91
     *
92
     * Note: The configuration of events is checked only in debug mode.
93
     *
94
     * @return self
95
     */
96 2
    public function withoutCheckingEvents(): self
97
    {
98 2
        $new = clone $this;
99 2
        $new->eventsGroup = null;
100 2
        return $new;
101
    }
102
103
    /**
104
     * Returns a new instance with the specified config instance {@see ConfigInterface}.
105
     *
106
     * @param ConfigInterface $config The config instance.
107
     *
108
     * @return self
109
     */
110 2
    public function withConfig(ConfigInterface $config): self
111
    {
112 2
        $new = clone $this;
113 2
        $new->config = $config;
114 2
        return $new;
115
    }
116
117
    /**
118
     * Returns a new instance with the specified container instance {@see ContainerInterface}.
119
     *
120
     * @param ContainerInterface $container The container instance.
121
     *
122
     * @return self
123
     */
124 2
    public function withContainer(ContainerInterface $container): self
125
    {
126 2
        $new = clone $this;
127 2
        $new->container = $container;
128 2
        return $new;
129
    }
130
131
    /**
132
     * @throws ErrorException
133
     */
134 5
    protected function createConfig(): Config
135
    {
136 5
        return ConfigFactory::create(new ConfigPaths($this->rootPath, 'config'), $this->environment);
137
    }
138
139
    /**
140
     * @throws ErrorException|InvalidConfigException
141
     */
142 4
    protected function createContainer(ConfigInterface $config, string $definitionEnvironment): Container
143
    {
144 4
        $containerConfig = ContainerConfig::create()->withValidate($this->debug);
145
146 4
        if ($config->has($definitionEnvironment)) {
147 4
            $containerConfig = $containerConfig->withDefinitions($config->get($definitionEnvironment));
148
        }
149
150 4
        if ($config->has("providers-$definitionEnvironment")) {
151 4
            $containerConfig = $containerConfig->withProviders($config->get("providers-$definitionEnvironment"));
152
        }
153
154 4
        if ($config->has("delegates-$definitionEnvironment")) {
155 4
            $containerConfig = $containerConfig->withDelegates($config->get("delegates-$definitionEnvironment"));
156
        }
157
158 4
        return new Container($containerConfig);
159
    }
160
161
    /**
162
     * @throws ErrorException|RuntimeException
163
     */
164 4
    protected function runBootstrap(ConfigInterface $config, ContainerInterface $container): void
165
    {
166 4
        if ($this->bootstrapGroup !== null) {
167 2
            (new BootstrapRunner($container, $config->get($this->bootstrapGroup)))->run();
168
        }
169 4
    }
170
171
    /**
172
     * @throws ContainerExceptionInterface|ErrorException|NotFoundExceptionInterface
173
     */
174 4
    protected function checkEvents(ConfigInterface $config, ContainerInterface $container): void
175
    {
176 4
        if ($this->debug && $this->eventsGroup !== null) {
177
            /** @psalm-suppress MixedMethodCall */
178 2
            $container->get(ListenerConfigurationChecker::class)->check($config->get($this->eventsGroup));
179
        }
180 3
    }
181
}
182