Passed
Push — master ( e82656...5198fb )
by Thierry
03:06
created

Bootstrap::setup()   B

Complexity

Conditions 9
Paths 32

Size

Total Lines 34
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 15
nc 32
nop 0
dl 0
loc 34
rs 8.0555
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\Utils\Config\Config;
19
use Jaxon\Exception\SetupException;
20
21
use function call_user_func;
22
use function count;
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
     * The library options
43
     *
44
     * @var array
45
     */
46
    private $aLibOptions = [];
47
48
    /**
49
     * The application options
50
     *
51
     * @var array
52
     */
53
    private $aAppOptions = [];
54
55
    /**
56
     * The Ajax endpoint URI
57
     *
58
     * @var string
59
     */
60
    private $sUri = '';
61
62
    /**
63
     * The js code URI
64
     *
65
     * @var string
66
     */
67
    private $sJsUri = '';
68
69
    /**
70
     * The js code dir
71
     *
72
     * @var string
73
     */
74
    private $sJsDir = '';
75
76
    /**
77
     * Export the js code
78
     *
79
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
80
     */
81
    private $bExportJs = false;
82
83
    /**
84
     * Minify the js code
85
     *
86
     * @var bool
0 ignored issues
show
Bug introduced by
Expected "boolean" but found "bool" for @var tag in member variable comment
Loading history...
87
     */
88
    private $bMinifyJs = false;
89
90
    /**
91
     * The class constructor
92
     *
93
     * @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...
94
     * @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...
95
     * @param CallbackManager $xCallbackManager
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
96
     */
97
    public function __construct(ConfigManager $xConfigManager, PackageManager $xPackageManager, CallbackManager $xCallbackManager)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
98
    {
99
        $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...
100
        $this->xPackageManager = $xPackageManager;
101
        $this->xCallbackManager = $xCallbackManager;
102
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
103
104
    /**
105
     * Set the library options
106
     *
107
     * @param array $aLibOptions    The library options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
108
     *
109
     * @return Bootstrap
110
     */
111
    public function lib(array $aLibOptions): Bootstrap
112
    {
113
        $this->aLibOptions = $aLibOptions;
114
        return $this;
115
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
116
117
    /**
118
     * Set the applications options
119
     *
120
     * @param array $aAppOptions    The application options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
121
     *
122
     * @return Bootstrap
123
     */
124
    public function app(array $aAppOptions): Bootstrap
125
    {
126
        $this->aAppOptions = $aAppOptions;
127
        return $this;
128
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
129
130
    /**
131
     * Set the ajax endpoint URI
132
     *
133
     * @param string $sUri    The ajax endpoint URI
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
134
     *
135
     * @return Bootstrap
136
     */
137
    public function uri(string $sUri): Bootstrap
138
    {
139
        $this->sUri = $sUri;
140
        return $this;
141
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
142
143
    /**
144
     * Set the javascript code
145
     *
146
     * @param bool $bExportJs    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...
147
     * @param string $sJsUri    The URI to access the js file
148
     * @param string $sJsDir    The directory where to create the js file
149
     * @param bool $bMinifyJs    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...
150
     *
151
     * @return Bootstrap
152
     */
153
    public function js(bool $bExportJs, string $sJsUri = '', string $sJsDir = '', bool $bMinifyJs = false): Bootstrap
154
    {
155
        $this->sJsUri = $sJsUri;
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...
156
        $this->sJsDir = $sJsDir;
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...
157
        $this->bExportJs = $bExportJs;
158
        $this->bMinifyJs = $bMinifyJs;
159
        return $this;
160
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
161
162
    /**
163
     * Set the Jaxon application options.
164
     *
165
     * @param Config $xAppConfig    The config options
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
166
     *
167
     * @return void
168
     * @throws SetupException
169
     */
170
    private function setupApp(Config $xAppConfig)
171
    {
172
        // Register user functions and classes
173
        $this->xPackageManager->registerFromConfig($xAppConfig);
174
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
175
176
    /**
177
     * Wraps the module/package/bundle setup method.
178
     *
179
     * @return void
180
     * @throws SetupException
181
     */
182
    public function setup()
183
    {
184
        // Setup the lib config options.
185
        $this->xConfigManager->setOptions($this->aLibOptions);
186
187
        // Get the app config options.
188
        $xAppConfig = $this->xConfigManager->newConfig($this->aAppOptions);
189
        $xAppConfig->setOption('options.views.default', 'default');
190
        // Setup the app.
191
        $this->setupApp($xAppConfig);
192
193
        // Jaxon library settings
194
        if(!$this->xConfigManager->hasOption('js.app.export'))
195
        {
196
            $this->xConfigManager->setOption('js.app.export', $this->bExportJs);
197
        }
198
        if(!$this->xConfigManager->hasOption('js.app.minify'))
199
        {
200
            $this->xConfigManager->setOption('js.app.minify', $this->bMinifyJs);
201
        }
202
        if(!$this->xConfigManager->hasOption('js.app.uri') && $this->sJsUri != '')
203
        {
204
            $this->xConfigManager->setOption('js.app.uri', $this->sJsUri);
205
        }
206
        if(!$this->xConfigManager->hasOption('js.app.dir') && $this->sJsDir != '')
207
        {
208
            $this->xConfigManager->setOption('js.app.dir', $this->sJsDir);
209
        }
210
        // Set the request URI
211
        if(!$this->xConfigManager->hasOption('core.request.uri') && $this->sUri != '')
212
        {
213
            $this->xConfigManager->setOption('core.request.uri', $this->sUri);
214
        }
215
        $this->onBoot();
216
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
217
218
    /**
219
     * These callbacks are called right after the library is initialized.
220
     *
221
     * @return void
222
     */
223
    public function onBoot()
224
    {
225
        // Only call the callbacks that aren't called yet.
226
        $aBootCallbacks = $this->xCallbackManager->popBootCallbacks();
227
        foreach($aBootCallbacks as $aBootCallback)
228
        {
229
            call_user_func($aBootCallback);
230
        }
231
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
232
}
233