Passed
Push — main ( 723415...fef313 )
by Thierry
11:06
created

AppTrait::getJsLibVersion()   A

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Jaxon\Di\Traits;
4
5
use Jaxon\App\App;
6
use Jaxon\App\AppInterface;
7
use Jaxon\App\Bootstrap;
8
use Jaxon\App\Config\ConfigEventManager;
9
use Jaxon\App\Config\ConfigManager;
10
use Jaxon\App\Dialog\Library\DialogLibraryManager;
11
use Jaxon\App\I18n\Translator;
12
use Jaxon\App\View\ViewRenderer;
13
use Jaxon\Di\Container;
14
use Jaxon\Jaxon;
15
use Jaxon\Plugin\Manager\PackageManager;
16
use Jaxon\Request\Handler\CallbackManager;
17
use Jaxon\Utils\Config\ConfigReader;
18
19
trait AppTrait
0 ignored issues
show
Coding Style introduced by
Missing doc comment for trait AppTrait
Loading history...
20
{
21
    /**
22
     * @var string
23
     */
24
    private $sJsLibVersion = 'jaxon_javascript_library_version';
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
25
26
    /**
27
     * The default config options
28
     *
29
     * @var array
30
     */
31
    protected $aConfig =  [
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "="; 2 found
Loading history...
32
        'core' => [
33
            'version'               => Jaxon::VERSION,
34
            'language'              => 'en',
35
            'encoding'              => 'utf-8',
36
            'decode_utf8'           => false,
37
            'prefix' => [
38
                'function'          => 'jaxon_',
39
                'class'             => 'Jaxon',
40
            ],
41
            'request' => [
42
                // 'uri'            => '',
43
                'mode'              => 'asynchronous',
44
                'method'            => 'POST', // W3C: Method is case-sensitive
45
            ],
46
            'response' => [
47
                'send'              => true,
48
            ],
49
            'debug' => [
50
                'on'                => false,
51
                'verbose'           => false,
52
            ],
53
            'process' => [
54
                'exit'              => true,
55
                'timeout'           => 6000,
56
            ],
57
            'error' => [
58
                'handle'            => false,
59
                'log_file'          => '',
60
            ],
61
            'jquery' => [
62
                'no_conflict'       => false,
63
            ],
64
        ],
65
        'js' => [
66
            'lib' => [
67
                'output_id'         => 0,
68
                'queue_size'        => 0,
69
                'load_timeout'      => 2000,
70
                'show_status'       => false,
71
                'show_cursor'       => true,
72
            ],
73
            'app' => [
74
                'dir'               => '',
75
                'options'           => '',
76
            ],
77
        ],
78
    ];
79
80
    /**
81
     * Register the values into the container
82
     *
83
     * @return void
84
     */
85
    private function registerApp()
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
86
    {
87
        // Translator
88
        $this->set(Translator::class, function($c) {
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

88
        $this->/** @scrutinizer ignore-call */ 
89
               set(Translator::class, function($c) {
Loading history...
89
            $xTranslator = new Translator();
90
            $sResourceDir = rtrim(trim($c->g('jaxon.core.dir.translation')), '/\\');
91
            // Load the Jaxon package translations
92
            $xTranslator->loadTranslations($sResourceDir . '/en/errors.php', 'en');
93
            $xTranslator->loadTranslations($sResourceDir . '/fr/errors.php', 'fr');
94
            $xTranslator->loadTranslations($sResourceDir . '/es/errors.php', 'es');
95
            // Load the config translations
96
            $xTranslator->loadTranslations($sResourceDir . '/en/config.php', 'en');
97
            $xTranslator->loadTranslations($sResourceDir . '/fr/config.php', 'fr');
98
            $xTranslator->loadTranslations($sResourceDir . '/es/config.php', 'es');
99
            return $xTranslator;
100
        });
101
102
        // Config Manager
103
        $this->set(ConfigEventManager::class, function($c) {
104
            return new ConfigEventManager($c->g(Container::class));
105
        });
106
        $this->set(ConfigManager::class, function($c) {
107
            $xEventManager = $c->g(ConfigEventManager::class);
108
            $xConfigManager = new ConfigManager($c->g(ConfigReader::class), $xEventManager, $c->g(Translator::class));
109
            $xConfigManager->setOptions($this->aConfig);
110
            // It's important to call this after the $xConfigManager->setOptions(),
111
            // because we don't want to trigger the events since the listeners cannot yet be instantiated.
112
            $xEventManager->addListener(Translator::class);
113
            $xEventManager->addListener(DialogLibraryManager::class);
114
            return $xConfigManager;
115
        });
116
117
        // Jaxon App
118
        $this->set(AppInterface::class, function($c) {
119
            return new App($c->g(Container::class));
120
        });
121
        // Jaxon App bootstrap
122
        $this->set(Bootstrap::class, function($c) {
123
            return new Bootstrap($c->g(ConfigManager::class), $c->g(PackageManager::class),
124
                $c->g(CallbackManager::class), $c->g(ViewRenderer::class));
125
        });
126
        // The javascript library version
127
        $this->set($this->sJsLibVersion, function($c) {
128
            $xRequest = $c->getRequest();
129
            $aParams = $xRequest->getMethod() === 'POST' ?
0 ignored issues
show
Coding Style introduced by
Expected 1 space after "?"; newline found
Loading history...
130
                $xRequest->getParsedBody() : $xRequest->getQueryParams();
131
            return $aParams['jxnv'] ?? '3.3.0';
132
        });
133
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
134
135
    /**
136
     * Get the App instance
137
     *
138
     * @return AppInterface
139
     */
140
    public function getApp(): AppInterface
141
    {
142
        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

142
        return $this->/** @scrutinizer ignore-call */ g(AppInterface::class);
Loading history...
143
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
144
145
    /**
146
     * Get the App bootstrap
147
     *
148
     * @return Bootstrap
149
     */
150
    public function getBootstrap(): Bootstrap
151
    {
152
        return $this->g(Bootstrap::class);
153
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
154
155
    /**
156
     * Get the javascript library version
157
     *
158
     * @return string
159
     */
160
    public function getJsLibVersion(): string
161
    {
162
        return $this->g($this->sJsLibVersion);
163
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
164
}
165