|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Borodulin\Container; |
|
6
|
|
|
|
|
7
|
|
|
use Borodulin\Container\Autowire\ClassNameExtractor; |
|
8
|
|
|
use Borodulin\Container\Autowire\FileFinder; |
|
9
|
|
|
use Borodulin\Container\Autowire\Item\AliasItem; |
|
10
|
|
|
use Borodulin\Container\Autowire\Item\CallableItem; |
|
11
|
|
|
use Borodulin\Container\Autowire\Item\ClassItem; |
|
12
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
13
|
|
|
use Psr\SimpleCache\InvalidArgumentException; |
|
14
|
|
|
|
|
15
|
|
|
class ContainerBuilder |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var FileFinder |
|
19
|
|
|
*/ |
|
20
|
|
|
private $fileFinder; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var CacheInterface|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $cache; |
|
25
|
|
|
/** |
|
26
|
|
|
* @var iterable |
|
27
|
|
|
*/ |
|
28
|
|
|
private $config = []; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var ClassNameExtractor |
|
31
|
|
|
*/ |
|
32
|
|
|
private $classNameExtractor; |
|
33
|
|
|
|
|
34
|
13 |
|
public function __construct() |
|
35
|
|
|
{ |
|
36
|
13 |
|
$this->classNameExtractor = new ClassNameExtractor(); |
|
37
|
13 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @throws ContainerException |
|
41
|
|
|
* @throws InvalidArgumentException |
|
42
|
|
|
*/ |
|
43
|
13 |
|
public function build(): Container |
|
44
|
|
|
{ |
|
45
|
13 |
|
if ($this->cache && $this->cache->has(static::class)) { |
|
46
|
1 |
|
$compilerItems = unserialize($this->cache->get(static::class)); |
|
47
|
|
|
} else { |
|
48
|
13 |
|
$compilerItems = []; |
|
49
|
|
|
|
|
50
|
13 |
|
$this->buildConfig($compilerItems); |
|
51
|
|
|
|
|
52
|
12 |
|
$this->buildFiles($compilerItems); |
|
53
|
|
|
|
|
54
|
12 |
|
if ($this->cache) { |
|
55
|
1 |
|
$this->cache->set(static::class, serialize($compilerItems)); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
return new Container($compilerItems); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
8 |
|
public function setConfig(iterable $config): self |
|
63
|
|
|
{ |
|
64
|
8 |
|
$this->config = $config; |
|
65
|
|
|
|
|
66
|
8 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
1 |
|
public function setCache(?CacheInterface $cache): self |
|
70
|
|
|
{ |
|
71
|
1 |
|
$this->cache = $cache; |
|
72
|
|
|
|
|
73
|
1 |
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
6 |
|
public function setFileFinder(FileFinder $fileFinder): self |
|
77
|
|
|
{ |
|
78
|
6 |
|
$this->fileFinder = $fileFinder; |
|
79
|
|
|
|
|
80
|
6 |
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
13 |
|
private function buildConfig(array &$compilerItems): void |
|
84
|
|
|
{ |
|
85
|
13 |
|
foreach ($this->config as $id => $item) { |
|
86
|
8 |
|
if (\is_string($item)) { |
|
87
|
7 |
|
$id = \is_int($id) ? $item : $id; |
|
88
|
7 |
|
$compilerItems[$id] = new AliasItem($id, $item); |
|
89
|
4 |
|
} elseif (\is_callable($item)) { |
|
90
|
3 |
|
$id = (string) $id; |
|
91
|
3 |
|
$compilerItems[$id] = new CallableItem($id, $item); |
|
92
|
|
|
} else { |
|
93
|
1 |
|
throw new ContainerException('Unsupported item type'); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
12 |
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
private function buildFiles(array &$compilerItems): void |
|
99
|
|
|
{ |
|
100
|
12 |
|
if (null !== $this->fileFinder) { |
|
101
|
6 |
|
foreach ($this->fileFinder as $fileName) { |
|
102
|
6 |
|
$className = $this->classNameExtractor->extract($fileName); |
|
103
|
6 |
|
if (null !== $className) { |
|
104
|
6 |
|
if (!isset($compilerItems[$className])) { |
|
105
|
6 |
|
$compilerItems[$className] = new ClassItem($className); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
12 |
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|