-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathContainer.php
213 lines (178 loc) · 7.11 KB
/
Container.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
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
<?php
declare(strict_types = 1);
/*
* Go! AOP framework
*
* @copyright Copyright 2012, Lisachenko Alexander <[email protected]>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Go\Core;
use Closure;
use Go\Aop\Aspect;
use Go\Aop\AspectException;
use Go\Aop\Pointcut\DNF\Parser\TokenizerParser;
use Go\Aop\Pointcut\DNF\Parser\TokenizerParserInterface;
use Go\Aop\Pointcut\DNF\SemanticAnalyzer;
use Go\Aop\Pointcut\DNF\SemanticAnalyzerInterface;
use Go\Aop\Pointcut\PointcutGrammar;
use Go\Aop\Pointcut\PointcutLexer;
use Go\Aop\Pointcut\PointcutParser;
use Go\Instrument\ClassLoading\CachePathManager;
use OutOfBoundsException;
use ReflectionObject;
/**
* DI-container
*/
class Container implements AspectContainer
{
/**
* @var (array&array<string,mixed>) Hashmap of items/services in the container
*/
private array $values = [];
/**
* @var (array&array<class-string, list<string>>) Holds information about mapping of interface tags into identifiers
*/
private array $tags = [];
/**
* Cached timestamp for resources, might be uninitialized if {@see self::hasAnyResourceChangedSince()} is not called yet
*/
private int $cachedMaxTimestamp;
/**
* @var (array&array<string, string>) Hashmap of resources for application
*/
private array $resources = [];
/**
* Constructor for container
*
* @param array<string> $resources [Optional] List of additional resources to track for container invalidation
*/
public function __construct(array $resources = [])
{
$this->resources = array_combine($resources, $resources);
$this->addLazy(PointcutLexer::class, fn() => new PointcutLexer());
$this->addLazy(PointcutParser::class, fn(AspectContainer $container) => new PointcutParser(
new PointcutGrammar($container)
));
$this->addLazy(AdviceMatcher::class, fn(AspectContainer $container) => new AdviceMatcher(
(bool) $container->getValue('kernel.interceptFunctions')
));
$this->addLazy(AspectLoader::class, function (AspectContainer $container) {
$lexer = $container->getService(PointcutLexer::class);
$parser = $container->getService(PointcutParser::class);
return new AspectLoader(
$container,
new AttributeAspectLoaderExtension($lexer, $parser),
new IntroductionAspectExtension($lexer, $parser)
);
});
$this->addLazy(CachedAspectLoader::class, function (AspectContainer $container) {
$options = $container->getValue('kernel.options');
if (is_array($options) && !empty($options['cacheDir'])) {
$loader = new CachedAspectLoader($container, AspectLoader::class, $options);
} else {
$loader = $container->getService(AspectLoader::class);
}
return $loader;
});
$this->addLazy(LazyAdvisorAccessor::class, fn(AspectContainer $container) => new LazyAdvisorAccessor(
$container,
$container->getService(CachedAspectLoader::class)
));
$this->addLazy(CachePathManager::class, fn(AspectContainer $container) => new CachePathManager(
$container->getService(AspectKernel::class)
));
$this->addLazy(TokenizerParserInterface::class, static fn(AspectContainer $c) => new TokenizerParser());
$this->addLazy(SemanticAnalyzerInterface::class, static fn(AspectContainer $c) => new SemanticAnalyzer());
}
final public function registerAspect(Aspect $aspect): void
{
$this->add($aspect::class, $aspect);
}
final public function add(string $id, mixed $value): void
{
$this->values[$id] = $value;
// For objects we would like to use interface names as tags, eg Pointcut, Advisor, Aspect, etc
if (is_object($value) && !$value instanceof Closure) {
$reflectionObject = new ReflectionObject($value);
foreach ($reflectionObject->getInterfaceNames() as $interfaceTagName) {
$this->tags[$interfaceTagName][] = $id;
}
// Also register corresponding file names to track freshness of container
$fileName = $reflectionObject->getFileName();
if (is_string($fileName)) {
$this->addResource($fileName);
}
}
}
final public function getService(string $className): object
{
if (!isset($this->values[$className])) {
throw new OutOfBoundsException("Value {$className} is not defined in the container");
}
// Support for lazy-evaluation and initialization
if ($this->values[$className] instanceof Closure) {
return $this->values[$className]($this);
}
if (!$this->values[$className] instanceof $className) {
throw new AspectException("Service {$className} is not properly registered");
}
return $this->values[$className];
}
final public function getValue(string $key): mixed
{
if (!isset($this->values[$key])) {
throw new OutOfBoundsException("Value {$key} is not defined in the container");
}
return $this->values[$key];
}
final public function has(string $id): bool
{
return isset($this->values[$id]);
}
final public function getServicesByInterface(string $interfaceTagClassName): array
{
$values = [];
foreach (($this->tags[$interfaceTagClassName] ?? []) as $containerKey) {
$values[$containerKey] = $this->getValue($containerKey);
}
return $values;
}
final public function hasAnyResourceChangedSince(int $timestamp): bool
{
if (!isset($this->cachedMaxTimestamp)) {
$this->cachedMaxTimestamp = max(array_filter(array_map(filemtime(...), $this->resources)) + [0]);
}
return $this->cachedMaxTimestamp <= $timestamp;
}
/**
* Adds a link to the file resource into the container
*
* This set of resources is used later to check the freshness of cache
*
* @param string $resource Path to the resource
*/
final protected function addResource(string $resource): void
{
if (!isset($this->resources[$resource])) {
$this->resources[$resource] = $resource;
// Invalidation of calculated timestamp
unset($this->cachedMaxTimestamp);
}
}
/**
* Add value in the container, uses lazy-loading scheme to optimize init time
*
* @param Closure(AspectContainer $container): object $lazyDefinitionClosure
*/
final protected function addLazy(string $id, Closure $lazyDefinitionClosure): void
{
$this->add($id, function (self $container) use ($id, $lazyDefinitionClosure): object {
$evaluatedLazyValue = $lazyDefinitionClosure($container);
// Here we just replace Closure with resolved value to optimize access
$container->values[$id] = $evaluatedLazyValue;
return $evaluatedLazyValue;
});
}
}