-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathContainerResolver.php
66 lines (49 loc) · 1.26 KB
/
ContainerResolver.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Nette;
use Nette\DI\Container;
use PHPStan\ShouldNotHappenException;
use function is_file;
use function is_readable;
use function sprintf;
class ContainerResolver
{
/** @var string|null */
private $containerLoader;
/** @var Container|false|null */
private $container;
public function __construct(?string $containerLoader)
{
$this->containerLoader = $containerLoader;
}
public function getContainer(): ?Container
{
if ($this->container === false) {
return null;
}
if ($this->container !== null) {
return $this->container;
}
if ($this->containerLoader === null) {
$this->container = false;
return null;
}
$this->container = $this->loadContainer($this->containerLoader);
return $this->container;
}
private function loadContainer(string $containerLoader): ?Container
{
if (!is_file($containerLoader)) {
throw new ShouldNotHappenException(sprintf(
'Nette container could not be loaded: file "%s" does not exist',
$containerLoader
));
}
if (!is_readable($containerLoader)) {
throw new ShouldNotHappenException(sprintf(
'Nette container could not be loaded: file "%s" is not readable',
$containerLoader
));
}
return require $containerLoader;
}
}