AppTrait::getBootstrap()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\App\Ajax\App;
6
use Jaxon\App\Ajax\AppInterface;
7
use Jaxon\App\Ajax\Bootstrap;
8
use Jaxon\App\Config\ConfigEventManager;
9
use Jaxon\App\Config\ConfigManager;
10
use Jaxon\App\I18n\Translator;
11
use Jaxon\App\View\ViewRenderer;
12
use Jaxon\Di\Container;
13
use Jaxon\Request\Handler\CallbackManager;
14
use Jaxon\Plugin\Manager\PackageManager;
15
use Jaxon\Plugin\Response\Dialog\DialogManager;
16
use Jaxon\Utils\Config\ConfigReader;
17
18
trait AppTrait
19
{
20
    /**
21
     * @var string
22
     */
23
    private $sJsLibVersion = 'jaxon_javascript_library_version';
24
25
    /**
26
     * Register the values into the container
27
     *
28
     * @return void
29
     */
30
    private function registerApp()
31
    {
32
        // Config Manager
33
        $this->set(ConfigEventManager::class, function($di) {
0 ignored issues
show
Bug introduced by
It seems like set() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

33
        $this->/** @scrutinizer ignore-call */ 
34
               set(ConfigEventManager::class, function($di) {
Loading history...
34
            return new ConfigEventManager($di->g(Container::class));
35
        });
36
        $this->set(ConfigManager::class, function($di) {
37
            $xEventManager = $di->g(ConfigEventManager::class);
38
            $xConfigManager = new ConfigManager($di->g(ConfigReader::class), $xEventManager, $di->g(Translator::class));
39
            $xConfigManager->setOptions(require(__DIR__ . '/../../../config/lib.php'));
40
            // It's important to call this after the $xConfigManager->setOptions(),
41
            // because we don't want to trigger the events since the listeners cannot yet be instantiated.
42
            $xEventManager->addListener(Translator::class);
43
            $xEventManager->addListener(DialogManager::class);
44
            return $xConfigManager;
45
        });
46
47
        // Jaxon App
48
        $this->set(AppInterface::class, function() {
49
            return new App();
50
        });
51
        // Jaxon App bootstrap
52
        $this->set(Bootstrap::class, function($di) {
53
            return new Bootstrap($di->g(ConfigManager::class), $di->g(PackageManager::class),
54
                $di->g(CallbackManager::class), $di->g(ViewRenderer::class));
55
        });
56
        // The javascript library version
57
        $this->set($this->sJsLibVersion, function($di) {
58
            $xRequest = $di->getRequest();
59
            $aParams = $xRequest->getMethod() === 'POST' ?
60
                $xRequest->getParsedBody() : $xRequest->getQueryParams();
61
            return $aParams['jxnv'] ?? '3.3.0';
62
        });
63
    }
64
65
    /**
66
     * Get the App instance
67
     *
68
     * @return AppInterface
69
     */
70
    public function getApp(): AppInterface
71
    {
72
        return $this->g(AppInterface::class);
0 ignored issues
show
Bug introduced by
It seems like g() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        return $this->/** @scrutinizer ignore-call */ g(AppInterface::class);
Loading history...
73
    }
74
75
    /**
76
     * Get the App bootstrap
77
     *
78
     * @return Bootstrap
79
     */
80
    public function getBootstrap(): Bootstrap
81
    {
82
        return $this->g(Bootstrap::class);
83
    }
84
85
    /**
86
     * Get the javascript library version
87
     *
88
     * @return string
89
     */
90
    public function getJsLibVersion(): string
91
    {
92
        return $this->g($this->sJsLibVersion);
93
    }
94
}
95