Passed
Push — master ( 6eb156...b34d93 )
by Thierry
04:25
created

Bootstrap::asset()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 4
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Bootstrap.php - Jaxon application bootstrapper
5
 *
6
 * @package jaxon-core
0 ignored issues
show
Coding Style introduced by
Package name "jaxon-core" is not valid; consider "Jaxoncore" instead
Loading history...
7
 * @author Thierry Feuzeu <[email protected]>
8
 * @copyright 2019 Thierry Feuzeu <[email protected]>
9
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
10
 * @link https://github.com/jaxon-php/jaxon-core
11
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
12
13
namespace Jaxon\App;
14
15
use Jaxon\Config\ConfigManager;
16
use Jaxon\Plugin\Manager\PackageManager;
17
use Jaxon\Request\Handler\CallbackManager;
18
use Jaxon\Ui\View\ViewRenderer;
19
use Jaxon\Utils\Config\Config;
20
use Jaxon\Exception\SetupException;
21
22
use function call_user_func;
23
24
class Bootstrap
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Bootstrap
Loading history...
25
{
26
    /**
27
     * @var ConfigManager
28
     */
29
    private $xConfigManager;
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line(s) before first member var; 0 found
Loading history...
30
31
    /**
32
     * @var PackageManager
33
     */
34
    private $xPackageManager;
35
36
    /**
37
     * @var CallbackManager
38
     */
39
    private $xCallbackManager;
40
41
    /**
42
     * @var ViewRenderer
43
     */
44
    private $xViewRenderer;
45
46
    /**
47
     * The library options
48
     *
49
     * @var array
50
     */
51
    private $aLibOptions = [];
52
53
    /**
54
     * The application options
55
     *
56
     * @var array
57
     */
58
    private $aAppOptions = [];
59
60
    /**
61
     * The class constructor
62
     *
63
     * @param ConfigManager $xConfigManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
64
     * @param PackageManager $xPackageManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
65
     * @param CallbackManager $xCallbackManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
66
     * @param ViewRenderer $xViewRenderer
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
67
     */
68
    public function __construct(ConfigManager $xConfigManager, PackageManager $xPackageManager,
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
69
        CallbackManager $xCallbackManager, ViewRenderer $xViewRenderer)
70
    {
71
        $this->xConfigManager = $xConfigManager;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
        $this->xPackageManager = $xPackageManager;
73
        $this->xCallbackManager = $xCallbackManager;
74
        $this->xViewRenderer = $xViewRenderer;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
76
77
    /**
78
     * Set the library options
79
     *
80
     * @param array $aLibOptions    The library options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
81
     *
82
     * @return Bootstrap
83
     */
84
    public function lib(array $aLibOptions): Bootstrap
85
    {
86
        $this->aLibOptions = $aLibOptions;
87
        return $this;
88
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
89
90
    /**
91
     * Set the applications options
92
     *
93
     * @param array $aAppOptions    The application options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
94
     *
95
     * @return Bootstrap
96
     */
97
    public function app(array $aAppOptions): Bootstrap
98
    {
99
        $this->aAppOptions = $aAppOptions;
100
        return $this;
101
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
102
103
    /**
104
     * Set the javascript asset
105
     *
106
     * @param bool $bExport    Whether to export the js code in a file
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
107
     * @param bool $bMinify    Whether to minify the exported js file
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
Coding Style introduced by
Expected 3 spaces after parameter type; 1 found
Loading history...
108
     * @param string $sUri    The URI to access the js file
109
     * @param string $sDir    The directory where to create the js file
110
     *
111
     * @return Bootstrap
112
     */
113
    public function asset(bool $bExport, bool $bMinify, string $sUri = '', string $sDir = ''): Bootstrap
114
    {
115
        // Jaxon library settings
116
        $this->xConfigManager->setOption('js.app.export', $bExport);
117
        $this->xConfigManager->setOption('js.app.minify', $bMinify);
118
        if($sUri !== '')
119
        {
120
            $this->xConfigManager->setOption('js.app.uri', $sUri);
121
        }
122
        if($sDir !== '')
123
        {
124
            $this->xConfigManager->setOption('js.app.dir', $sDir);
125
        }
126
        return $this;
127
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
128
129
    /**
130
     * Set the Jaxon application options.
131
     *
132
     * @param Config $xAppConfig    The config options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
133
     *
134
     * @return void
135
     * @throws SetupException
136
     */
137
    private function setupApp(Config $xAppConfig)
138
    {
139
        if($xAppConfig->hasOption('options.views.default'))
140
        {
141
            $this->xViewRenderer->setDefaultNamespace($xAppConfig->getOption('options.views.default'));
0 ignored issues
show
Bug introduced by
It seems like $xAppConfig->getOption('options.views.default') can also be of type null; however, parameter $sDefaultNamespace of Jaxon\Ui\View\ViewRenderer::setDefaultNamespace() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

141
            $this->xViewRenderer->setDefaultNamespace(/** @scrutinizer ignore-type */ $xAppConfig->getOption('options.views.default'));
Loading history...
142
        }
143
        // Register user functions and classes
144
        $this->xPackageManager->registerFromConfig($xAppConfig);
145
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
146
147
    /**
148
     * Wraps the module/package/bundle setup method.
149
     *
150
     * @return void
151
     * @throws SetupException
152
     */
153
    public function setup()
154
    {
155
        // Prevent the Jaxon library from sending the response or exiting
156
        $this->xConfigManager->setOption('core.response.send', false);
157
        $this->xConfigManager->setOption('core.process.exit', false);
158
        $this->xConfigManager->setOption('core.process.clean', false);
159
160
        // Setup the lib config options.
161
        $this->xConfigManager->setOptions($this->aLibOptions);
162
        // Get the app config options.
163
        $xAppConfig = $this->xConfigManager->newConfig($this->aAppOptions);
164
        $xAppConfig->setOption('options.views.default', 'default');
165
        // Setup the app.
166
        $this->setupApp($xAppConfig);
167
        $this->onBoot();
168
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
169
170
    /**
171
     * These callbacks are called right after the library is initialized.
172
     *
173
     * @return void
174
     */
175
    public function onBoot()
176
    {
177
        // Only call the callbacks that aren't called yet.
178
        $aBootCallbacks = $this->xCallbackManager->popBootCallbacks();
179
        foreach($aBootCallbacks as $aBootCallback)
180
        {
181
            call_user_func($aBootCallback);
182
        }
183
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
184
}
185