Repository navigation
Expand file tree
/
Copy pathAopKernel.php
More file actions
137 lines (118 loc) · 4.4 KB
/
Copy pathAopKernel.php
File metadata and controls
137 lines (118 loc) · 4.4 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
<?php
/**
* @noinspection PhpInternalEntityUsedInspection
* @noinspection PhpPropertyOnlyWrittenInspection
*/
namespace Okapi\Aop;
use Closure;
use DI\Attribute\Inject;
use Okapi\Aop\Attributes\Aspect;
use Okapi\Aop\Component\ComponentType;
use Okapi\Aop\Core\AutoloadInterceptor\ClassLoader;
use Okapi\Aop\Core\Cache\CachePaths;
use Okapi\Aop\Core\Cache\CacheStateFactory;
use Okapi\Aop\Core\Cache\CacheStateManager;
use Okapi\Aop\Core\Container\AspectManager;
use Okapi\Aop\Core\Container\TransformerManager;
use Okapi\Aop\Core\Options;
use Okapi\Aop\Core\Processor\AspectProcessor;
use Okapi\CodeTransformer\CodeTransformerKernel;
use Okapi\CodeTransformer\Core\AutoloadInterceptor\ClassLoader as CodeTransformerClassLoader;
use Okapi\CodeTransformer\Core\Cache\CachePaths as CodeTransformerCachePaths;
use Okapi\CodeTransformer\Core\Cache\CacheStateFactory as CodeTransformerCacheStateFactory;
use Okapi\CodeTransformer\Core\Cache\CacheStateManager as CodeTransformerCacheStateManager;
use Okapi\CodeTransformer\Core\Container\TransformerManager as CodeTransformerTransformerManager;
use Okapi\CodeTransformer\Core\DI;
use Okapi\CodeTransformer\Core\Options as CodeTransformerOptions;
use Okapi\CodeTransformer\Core\Processor\TransformerProcessor;
use function DI\decorate;
/**
* # AOP Kernel
*
* The AOP Kernel is the heart of the AOP library.
* It manages an environment for Aspect Oriented Programming.
*
* 1. Extend this class and define a list of aspects in the {@link $aspects}
* property.
* 2. Call the {@link init()} method early in the application lifecycle.
*
* If you want to modify the kernel options dynamically, override the
* {@link configureOptions()} method.
*
* @extends CodeTransformerKernel<Closure(class-string, ComponentType): object>
*/
abstract class AopKernel extends CodeTransformerKernel
{
// region DI
#[Inject]
private AspectManager $aspectManager;
// endregion
// region Settings
/**
* The cache directory.
* <br><b>Default:</b> ROOT_DIR/cache/aop<br>
*
* @var string|null
*/
protected ?string $cacheDir = null;
/**
* The exclude paths. Paths/directories in this array will be excluded
*
* @var string[]
*/
protected array $excludePaths = [];
// endregion
/**
* List of aspects to be applied.
*
* Class should be annotated with #[{@link Aspect}] attribute.
*
* @var class-string[]
*/
protected array $aspects = [];
/**
* @inheritDoc
*/
protected static function registerDependencyInjection(): void
{
parent::registerDependencyInjection();
// Overload classes for extending the functionality
DI::set(CodeTransformerOptions::class, decorate(static fn() => DI::get(Options::class)));
DI::set(CodeTransformerCachePaths::class, decorate(static fn() => DI::get(CachePaths::class)));
DI::set(CodeTransformerClassLoader::class, decorate(static fn(CodeTransformerClassLoader $previous) => DI::make(ClassLoader::class, [
'originalClassLoader' => $previous->originalClassLoader,
])));
DI::set(TransformerProcessor::class, decorate(static fn() => DI::get(AspectProcessor::class)));
DI::set(CodeTransformerCacheStateFactory::class, decorate(static fn() => DI::get(CacheStateFactory::class)));
DI::set(CodeTransformerCacheStateManager::class, decorate(static fn() => DI::get(CacheStateManager::class)));
DI::set(CodeTransformerTransformerManager::class, decorate(static fn() => DI::get(TransformerManager::class)));
}
/**
* @inheritdoc
*/
protected function preInit(): void
{
// Add the aspects
$this->aspectManager->addAspects($this->aspects);
parent::preInit();
}
/**
* Custom dependency injection handler.
*
* Pass a closure that takes an aspect/transformer class name as the
* argument and returns an aspect/transformer instance.
*
* @return null|Closure(class-string, ComponentType): object
*/
protected function dependencyInjectionHandler(): ?Closure
{
// Override this method to configure the dependency injection handler
return null;
}
protected function registerServices(): void
{
$this->aspectManager->registerCustomDependencyInjectionHandler($this->dependencyInjectionHandler());
$this->aspectManager->register();
parent::registerServices();
}
}