1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\Compiler; |
6
|
|
|
|
7
|
|
|
use Ray\Aop\Compiler; |
8
|
|
|
use Ray\Aop\Pointcut; |
9
|
|
|
use Ray\Compiler\Exception\Unbound; |
10
|
|
|
use Ray\Di\AbstractModule; |
11
|
|
|
use Ray\Di\Bind; |
12
|
|
|
use Ray\Di\Dependency; |
13
|
|
|
use Ray\Di\Exception\NotFound; |
14
|
|
|
|
15
|
|
|
use function assert; |
16
|
|
|
use function error_reporting; |
17
|
|
|
use function explode; |
18
|
|
|
use function file_exists; |
19
|
|
|
use function file_get_contents; |
20
|
|
|
use function is_array; |
21
|
|
|
use function is_bool; |
22
|
|
|
use function unserialize; |
23
|
|
|
|
24
|
|
|
use const E_NOTICE; |
25
|
|
|
|
26
|
|
|
final class OnDemandCompiler |
27
|
|
|
{ |
28
|
|
|
/** @var string */ |
29
|
|
|
private $scriptDir; |
30
|
|
|
|
31
|
|
|
/** @var ScriptInjector */ |
32
|
|
|
private $injector; |
33
|
|
|
|
34
|
|
|
/** @var AbstractModule */ |
35
|
|
|
private $module; |
36
|
|
|
|
37
|
|
|
public function __construct(ScriptInjector $injector, string $sctiptDir, AbstractModule $module) |
38
|
|
|
{ |
39
|
|
|
$this->scriptDir = $sctiptDir; |
40
|
|
|
$this->injector = $injector; |
41
|
|
|
$this->module = $module; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Compile dependency on demand |
46
|
|
|
*/ |
47
|
|
|
public function __invoke(string $dependencyIndex): void |
48
|
|
|
{ |
49
|
|
|
[$class] = explode('-', $dependencyIndex); |
|
|
|
|
50
|
|
|
$containerObject = $this->module->getContainer(); |
51
|
|
|
try { |
52
|
|
|
new Bind($containerObject, $class); |
53
|
|
|
} catch (NotFound $e) { |
54
|
|
|
throw new Unbound($dependencyIndex, 0, $e); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$containerArray = $containerObject->getContainer(); |
58
|
|
|
if (! isset($containerArray[$dependencyIndex])) { |
59
|
|
|
throw new Unbound($dependencyIndex, 0); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$dependency = $containerArray[$dependencyIndex]; |
63
|
|
|
$pointCuts = $this->loadPointcuts(); |
64
|
|
|
if ($dependency instanceof Dependency && is_array($pointCuts)) { |
65
|
|
|
$dependency->weaveAspects(new Compiler($this->scriptDir), $pointCuts); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$code = (new DependencyCode($containerObject, $this->injector))->getCode($dependency); |
69
|
|
|
(new DependencySaver($this->scriptDir))($dependencyIndex, $code); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array<Pointcut>|false |
74
|
|
|
*/ |
75
|
|
|
private function loadPointcuts() |
76
|
|
|
{ |
77
|
|
|
$pointcutsPath = $this->scriptDir . ScriptInjector::AOP; |
78
|
|
|
if (! file_exists($pointcutsPath)) { |
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$serialized = file_get_contents($pointcutsPath); |
83
|
|
|
assert(! is_bool($serialized)); |
84
|
|
|
$er = error_reporting(error_reporting() ^ E_NOTICE); |
85
|
|
|
$pointcuts = unserialize($serialized, ['allowed_classes' => true]); |
86
|
|
|
error_reporting($er); |
87
|
|
|
|
88
|
|
|
return $pointcuts; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.