-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathNetteDIModule.php
More file actions
219 lines (176 loc) · 5.1 KB
/
Copy pathNetteDIModule.php
File metadata and controls
219 lines (176 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php declare(strict_types = 1);
namespace Contributte\Codeception\Module;
use Codeception\Module;
use Codeception\TestInterface;
use Nette\Bootstrap\Configurator;
use Nette\Caching\Storages\Journal;
use Nette\Caching\Storages\SQLiteJournal;
use Nette\DI\Container;
use Nette\DI\Extensions\ExtensionsExtension;
use Nette\DI\MissingServiceException;
use Nette\Http\Session;
use Nette\Utils\FileSystem;
use ReflectionProperty;
class NetteDIModule extends Module
{
/** @var callable[] function(Container $configurator): void; */
public $onCreateConfigurator = [];
/** @var callable[] function(Container $container): void; */
public $onCreateContainer = [];
/** @var array<string, mixed> */
protected array $config = [
'configFiles' => [],
'appDir' => null,
'logDir' => null,
'wwwDir' => null,
'debugMode' => null,
'removeDefaultExtensions' => false,
'newContainerForEachTest' => false,
];
/** @var string[] */
protected array $requiredFields = [
'tempDir',
];
/** @var string */
private $path;
/** @var string[] */
private $configFiles = [];
/** @var Container|null */
private $container;
/**
* @param array{path?: string} $settings
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
*/
public function _beforeSuite(array $settings = []): void
{
assert(isset($settings['path']));
$this->path = rtrim($settings['path'], '/');
$this->clearTempDir();
}
public function _before(TestInterface $test): void
{
if ($this->config['newContainerForEachTest'] === true) {
$this->clearTempDir();
$this->container = null;
$this->configFiles = [];
}
}
public function _afterSuite(): void
{
$this->stopContainer();
}
public function _after(TestInterface $test): void
{
if ($this->config['newContainerForEachTest'] === true) {
$this->stopContainer();
}
}
/**
* @param string[] $configFiles
*/
public function useConfigFiles(array $configFiles): void
{
if ($this->config['newContainerForEachTest'] !== true) {
$this->fail('The useConfigFiles can only be used if the newContainerForEachTest option is set to true.');
}
if ($this->container !== null) {
$this->fail('Can\'t set configFiles after the container is created.');
}
$this->configFiles = $configFiles;
}
public function getContainer(): Container
{
if ($this->container === null) {
$this->createContainer();
}
return $this->container;
}
/**
* @return object
* @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
*/
public function grabService(string $service)
{
try {
/** @phpstan-var class-string $service */
return $this->getContainer()->getByType($service);
} catch (MissingServiceException $e) {
$this->fail($e->getMessage());
}
}
private function createContainer(): void
{
$configurator = new Configurator();
if ($this->config['removeDefaultExtensions'] === true) {
$configurator->defaultExtensions = [
'extensions' => ExtensionsExtension::class,
];
}
if ($this->config['logDir'] !== null) {
$logDir = $this->path . '/' . $this->config['logDir'];
FileSystem::createDir($logDir);
$configurator->enableTracy($logDir);
}
$configurator->addStaticParameters([
'appDir' => $this->path . ($this->config['appDir'] !== null ? '/' . $this->config['appDir'] : ''),
'wwwDir' => $this->path . ($this->config['wwwDir'] !== null ? '/' . $this->config['wwwDir'] : ''),
]);
$this->clearTempDir();
$tempDir = $this->getTempDir();
$configurator->setTempDirectory($tempDir);
if ($this->config['debugMode'] !== null) {
$configurator->setDebugMode((bool) $this->config['debugMode']);
}
/** @var iterable<string> $configFiles */
$configFiles = $this->configFiles !== [] ? $this->configFiles : $this->config['configFiles'];
foreach ($configFiles as $file) {
$configurator->addConfig(FileSystem::isAbsolute($file) ? $file : $this->path . '/' . $file);
}
foreach ($this->onCreateConfigurator as $callback) {
$callback($configurator);
}
$this->container = $configurator->createContainer();
foreach ($this->onCreateContainer as $callback) {
$callback($this->container);
}
}
private function getTempDir(): string
{
return $this->path . '/' . $this->config['tempDir'];
}
private function clearTempDir(): void
{
$this->deleteTempDir();
$tempDir = $this->getTempDir();
FileSystem::createDir($tempDir);
}
private function deleteTempDir(): void
{
$tempDir = $this->getTempDir();
if (is_dir($tempDir)) {
FileSystem::delete(realpath($tempDir));
}
}
private function stopContainer(): void
{
if ($this->container === null) {
return;
}
try {
$this->container->getByType(Session::class)->close();
} catch (MissingServiceException $e) {
// Session is optional
}
try {
$journal = $this->container->getByType(Journal::class);
if ($journal instanceof SQLiteJournal) {
$property = new ReflectionProperty(SQLiteJournal::class, 'pdo');
$property->setAccessible(true);
$property->setValue($journal, null);
}
} catch (MissingServiceException $e) {
// IJournal is optional
}
$this->deleteTempDir();
}
}