Passed
Push — main ( 527d6d...d1d6a4 )
by Thierry
01:54
created

anonymous//src/start.php$0   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 8
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 10
c 2
b 0
f 0
wmc 4
1
<?php
2
3
namespace Jaxon\Annotations;
4
5
use Jaxon\App\Config\ConfigEventManager;
6
use Jaxon\App\Config\ConfigListenerInterface;
7
use Jaxon\Di\Container;
8
use Jaxon\Plugin\AnnotationReaderInterface;
9
use Jaxon\Utils\Config\Config;
10
use mindplay\annotations\AnnotationCache;
11
use mindplay\annotations\AnnotationManager;
12
13
use function jaxon;
14
15
/**
16
 * Register the annotation reader into the Jaxon DI container
17
 *
18
 * @param Container $di
19
 * @param bool $bForce Force registration
20
 *
21
 * @return void
22
 */
23
function register(Container $di, bool $bForce = false)
24
{
25
    if(!$bForce && $di->h(AnnotationReader::class))
26
    {
27
        return;
28
    }
29
30
    $sCacheDirKey = 'jaxon_annotations_cache_dir';
31
    if(!$di->h($sCacheDirKey))
32
    {
33
        $di->val($sCacheDirKey, sys_get_temp_dir());
34
    }
35
    $di->set(AnnotationReader::class, function($c) use($sCacheDirKey) {
36
        $xAnnotationManager = new AnnotationManager();
37
        $xAnnotationManager->cache = new AnnotationCache($c->g($sCacheDirKey));
38
        return new AnnotationReader($xAnnotationManager);
39
    });
40
    $di->alias(AnnotationReaderInterface::class, AnnotationReader::class);
41
}
42
43
/**
44
 * Register the values into the container
45
 *
46
 * @return void
47
 */
48
function registerAnnotations()
49
{
50
    $di = jaxon()->di();
51
    $sEventListenerKey = AnnotationReader::class . '\\ConfigListener';
52
    if($di->h($sEventListenerKey))
53
    {
54
        return;
55
    }
56
57
    // The annotation package is installed, register the real annotation reader,
58
    // but only if the feature is activated in the config.
59
    $di->set($sEventListenerKey, function() {
60
        return new class implements ConfigListenerInterface
61
        {
62
            public function onChange(Config $xConfig, string $sName)
63
            {
64
                $sConfigKey = 'core.annotations.enabled';
65
                if(($sName === $sConfigKey || $sName === '') && $xConfig->getOption($sConfigKey))
66
                {
67
                    register(jaxon()->di());
68
                }
69
            }
70
        };
71
    });
72
73
    // Register the event listener
74
    $xEventManager = $di->g(ConfigEventManager::class);
75
    $xEventManager->addListener($sEventListenerKey);
76
}
77
78
// Initialize the upload handler
79
registerAnnotations();
80