1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Bootstrap.php |
5
|
|
|
* |
6
|
|
|
* Jaxon application bootstrapper |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @author Thierry Feuzeu <[email protected]> |
10
|
|
|
* @copyright 2019 Thierry Feuzeu <[email protected]> |
11
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
12
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
13
|
|
|
*/ |
|
|
|
|
14
|
|
|
|
15
|
|
|
namespace Jaxon\App; |
16
|
|
|
|
17
|
|
|
use Jaxon\App\Config\ConfigManager; |
18
|
|
|
use Jaxon\App\View\ViewRenderer; |
19
|
|
|
use Jaxon\Exception\SetupException; |
20
|
|
|
use Jaxon\Plugin\Manager\PackageManager; |
21
|
|
|
use Jaxon\Request\Handler\CallbackManager; |
22
|
|
|
use Jaxon\Utils\Config\Config; |
23
|
|
|
|
24
|
|
|
use function call_user_func; |
25
|
|
|
|
26
|
|
|
class Bootstrap |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ConfigManager |
30
|
|
|
*/ |
31
|
|
|
private $xConfigManager; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var PackageManager |
35
|
|
|
*/ |
36
|
|
|
private $xPackageManager; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var CallbackManager |
40
|
|
|
*/ |
41
|
|
|
private $xCallbackManager; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var ViewRenderer |
45
|
|
|
*/ |
46
|
|
|
private $xViewRenderer; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The library options |
50
|
|
|
* |
51
|
|
|
* @var array |
52
|
|
|
*/ |
53
|
|
|
private $aLibOptions = []; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The application options |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
private $aAppOptions = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* The class constructor |
64
|
|
|
* |
65
|
|
|
* @param ConfigManager $xConfigManager |
|
|
|
|
66
|
|
|
* @param PackageManager $xPackageManager |
|
|
|
|
67
|
|
|
* @param CallbackManager $xCallbackManager |
|
|
|
|
68
|
|
|
* @param ViewRenderer $xViewRenderer |
|
|
|
|
69
|
|
|
*/ |
70
|
|
|
public function __construct(ConfigManager $xConfigManager, PackageManager $xPackageManager, |
|
|
|
|
71
|
|
|
CallbackManager $xCallbackManager, ViewRenderer $xViewRenderer) |
72
|
|
|
{ |
73
|
|
|
$this->xConfigManager = $xConfigManager; |
|
|
|
|
74
|
|
|
$this->xPackageManager = $xPackageManager; |
75
|
|
|
$this->xCallbackManager = $xCallbackManager; |
76
|
|
|
$this->xViewRenderer = $xViewRenderer; |
|
|
|
|
77
|
|
|
} |
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set the library options |
81
|
|
|
* |
82
|
|
|
* @param array $aLibOptions The library options |
|
|
|
|
83
|
|
|
* |
84
|
|
|
* @return Bootstrap |
85
|
|
|
*/ |
86
|
|
|
public function lib(array $aLibOptions): Bootstrap |
87
|
|
|
{ |
88
|
|
|
$this->aLibOptions = $aLibOptions; |
89
|
|
|
return $this; |
90
|
|
|
} |
|
|
|
|
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Set the applications options |
94
|
|
|
* |
95
|
|
|
* @param array $aAppOptions The application options |
|
|
|
|
96
|
|
|
* |
97
|
|
|
* @return Bootstrap |
98
|
|
|
*/ |
99
|
|
|
public function app(array $aAppOptions): Bootstrap |
100
|
|
|
{ |
101
|
|
|
$this->aAppOptions = $aAppOptions; |
102
|
|
|
return $this; |
103
|
|
|
} |
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set the javascript asset |
107
|
|
|
* |
108
|
|
|
* @param bool $bExport Whether to export the js code in a file |
|
|
|
|
109
|
|
|
* @param bool $bMinify Whether to minify the exported js file |
|
|
|
|
110
|
|
|
* @param string $sUri The URI to access the js file |
111
|
|
|
* @param string $sDir The directory where to create the js file |
112
|
|
|
* |
113
|
|
|
* @return Bootstrap |
114
|
|
|
*/ |
115
|
|
|
public function asset(bool $bExport, bool $bMinify, string $sUri = '', string $sDir = ''): Bootstrap |
116
|
|
|
{ |
117
|
|
|
// Jaxon library settings |
118
|
|
|
$this->xConfigManager->setOption('js.app.export', $bExport); |
119
|
|
|
$this->xConfigManager->setOption('js.app.minify', $bMinify); |
120
|
|
|
if($sUri !== '') |
121
|
|
|
{ |
122
|
|
|
$this->xConfigManager->setOption('js.app.uri', $sUri); |
123
|
|
|
} |
124
|
|
|
if($sDir !== '') |
125
|
|
|
{ |
126
|
|
|
$this->xConfigManager->setOption('js.app.dir', $sDir); |
127
|
|
|
} |
128
|
|
|
return $this; |
129
|
|
|
} |
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the Jaxon application options. |
133
|
|
|
* |
134
|
|
|
* @param Config $xAppConfig The config options |
|
|
|
|
135
|
|
|
* |
136
|
|
|
* @return void |
137
|
|
|
* @throws SetupException |
138
|
|
|
*/ |
139
|
|
|
private function setupApp(Config $xAppConfig) |
140
|
|
|
{ |
141
|
|
|
// Register user functions and classes |
142
|
|
|
$this->xPackageManager->registerFromConfig($xAppConfig); |
143
|
|
|
} |
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Wraps the module/package/bundle setup method. |
147
|
|
|
* |
148
|
|
|
* @return void |
149
|
|
|
* @throws SetupException |
150
|
|
|
*/ |
151
|
|
|
public function setup() |
152
|
|
|
{ |
153
|
|
|
// Prevent the Jaxon library from sending the response or exiting |
154
|
|
|
$this->xConfigManager->setOption('core.response.send', false); |
155
|
|
|
$this->xConfigManager->setOption('core.process.exit', false); |
156
|
|
|
|
157
|
|
|
// Setup the lib config options. |
158
|
|
|
$this->xConfigManager->setOptions($this->aLibOptions); |
159
|
|
|
// Get the app config options. |
160
|
|
|
$xAppConfig = $this->xConfigManager->newConfig($this->aAppOptions); |
161
|
|
|
|
162
|
|
|
// Setup the app. |
163
|
|
|
$this->setupApp($xAppConfig); |
164
|
|
|
$this->onBoot(); |
165
|
|
|
} |
|
|
|
|
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* These callbacks are called right after the library is initialized. |
169
|
|
|
* |
170
|
|
|
* @return void |
171
|
|
|
*/ |
172
|
|
|
public function onBoot() |
173
|
|
|
{ |
174
|
|
|
// Only call the callbacks that aren't called yet. |
175
|
|
|
$aBootCallbacks = $this->xCallbackManager->popBootCallbacks(); |
176
|
|
|
foreach($aBootCallbacks as $aBootCallback) |
177
|
|
|
{ |
178
|
|
|
call_user_func($aBootCallback); |
179
|
|
|
} |
180
|
|
|
} |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|