|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Jaxon\Di\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Jaxon\Annotations\AnnotationReader; |
|
6
|
|
|
use Jaxon\App\Config\ConfigEventManager; |
|
7
|
|
|
use Jaxon\App\Config\ConfigListenerInterface; |
|
8
|
|
|
use Jaxon\Plugin\AnnotationReaderInterface; |
|
9
|
|
|
use Jaxon\Utils\Config\Config; |
|
10
|
|
|
|
|
11
|
|
|
use function class_exists; |
|
12
|
|
|
|
|
13
|
|
|
trait AnnotationTrait |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Register the values into the container |
|
17
|
|
|
* |
|
18
|
|
|
* @return void |
|
19
|
|
|
*/ |
|
20
|
|
|
private function registerAnnotations() |
|
|
|
|
|
|
21
|
|
|
{ |
|
22
|
|
|
// By default, register a fake annotation reader. |
|
23
|
|
|
$this->set(AnnotationReaderInterface::class, function() { |
|
|
|
|
|
|
24
|
|
|
return new class implements AnnotationReaderInterface |
|
25
|
|
|
{ |
|
26
|
|
|
public function getAttributes(string $sClass, array $aMethods): array |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
return [false, [], []]; |
|
29
|
|
|
} |
|
|
|
|
|
|
30
|
|
|
}; |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
if(class_exists(AnnotationReader::class)) |
|
34
|
|
|
{ |
|
35
|
|
|
$sEventListenerKey = AnnotationReader::class . '\\ConfigListener'; |
|
36
|
|
|
// The annotation package is installed, register the real annotation reader, |
|
37
|
|
|
// but only if the feature is activated in the config. |
|
38
|
|
|
$this->set($sEventListenerKey, function() { |
|
39
|
|
|
return new class implements ConfigListenerInterface |
|
40
|
|
|
{ |
|
41
|
|
|
public function onChanges(Config $xConfig) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
if($xConfig->getOption('core.annotations.on')) |
|
44
|
|
|
{ |
|
45
|
|
|
AnnotationReader::register(jaxon()->di()); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
public function onChange(Config $xConfig, string $sName) |
|
|
|
|
|
|
50
|
|
|
{ |
|
51
|
|
|
if($sName === 'core.annotations.on' && $xConfig->getOption('core.annotations.on')) |
|
52
|
|
|
{ |
|
53
|
|
|
AnnotationReader::register(jaxon()->di()); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
|
|
|
|
|
56
|
|
|
}; |
|
57
|
|
|
}); |
|
58
|
|
|
|
|
59
|
|
|
// Register the event listener |
|
60
|
|
|
$xEventManager = $this->g(ConfigEventManager::class); |
|
|
|
|
|
|
61
|
|
|
$xEventManager->addListener($sEventListenerKey); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|