|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\base; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use ReflectionClass; |
|
7
|
|
|
use luya\Exception; |
|
8
|
|
|
use luya\web\Application as WebApplication; |
|
9
|
|
|
use luya\console\Application as ConsoleApplication; |
|
10
|
|
|
use luya\helpers\ArrayHelper; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* LUYA Boot wrapper. |
|
14
|
|
|
* |
|
15
|
|
|
* Run the Luya/Yii Application based on the current enviroment which is determined trough get_sapi_name(). To run an application |
|
16
|
|
|
* a config file with custom Luya/Yii configuration must be provided via `$configFile` property. By default luya will try to find |
|
17
|
|
|
* the default config `../configs/env.php`. |
|
18
|
|
|
* |
|
19
|
|
|
* @author Basil Suter <[email protected]> |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class Boot |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string The current LUYA version (see: https://github.com/luyadev/luya/blob/master/core/CHANGELOG.md) |
|
26
|
|
|
*/ |
|
27
|
|
|
const VERSION = '1.6.3'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string The path to the config file, which returns an array containing you configuration. |
|
31
|
|
|
*/ |
|
32
|
|
|
public $configFile = '../configs/env.php'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var \luya\web\Application|\luya\console\Application The application object. |
|
36
|
|
|
*/ |
|
37
|
|
|
public $app; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var bool When enabled the boot process will not return/echo something, but the variabled will contain the Application object. |
|
41
|
|
|
*/ |
|
42
|
|
|
public $mockOnly = false; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var string Path to the Yii.php file. |
|
46
|
|
|
*/ |
|
47
|
|
|
private $_baseYiiFile; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Setter method for the base Yii file. |
|
51
|
|
|
* |
|
52
|
|
|
* Example path to the yii base file: |
|
53
|
|
|
* |
|
54
|
|
|
* ```php |
|
55
|
|
|
* $boot->setYiiPath(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php'); |
|
56
|
|
|
* ``` |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $baseYiiFile The path to the Yii.php file. |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setBaseYiiFile($baseYiiFile) |
|
61
|
|
|
{ |
|
62
|
|
|
$this->_baseYiiFile = $baseYiiFile; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Getter method for Yii base file. |
|
67
|
|
|
* |
|
68
|
|
|
* @return string |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getBaseYiiFile() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->_baseYiiFile; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Whether current request is runing in cli env or not. |
|
77
|
|
|
* |
|
78
|
|
|
* This is determined by php_sapi_name(). |
|
79
|
|
|
* |
|
80
|
|
|
* @return boolean |
|
81
|
|
|
* @deprecated Depreacted since 1.0.12 use getisCli() instead. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function isCli() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->getIsCli(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
private $_isCli; |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Getter method whether current request is cli or not. |
|
92
|
|
|
* |
|
93
|
|
|
* If not set via setIsCli() the value is determined trough php_sapi_name(); |
|
94
|
|
|
* |
|
95
|
|
|
* @return boolean Whether current request is console env or not. |
|
96
|
|
|
* @since 1.0.12 |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getIsCli() |
|
99
|
|
|
{ |
|
100
|
|
|
if ($this->_isCli === null) { |
|
101
|
|
|
$this->_isCli = $this->getSapiName() === 'cli'; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return $this->_isCli; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Setter method for isCli. |
|
109
|
|
|
* |
|
110
|
|
|
* @param boolean $isCli |
|
111
|
|
|
* @since 1.0.12 |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setIsCli($isCli) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->_isCli = $isCli; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Returns the current sapi name in lower case. |
|
120
|
|
|
* |
|
121
|
|
|
* @return string e.g. cli or web |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getSapiName() |
|
124
|
|
|
{ |
|
125
|
|
|
return strtolower(php_sapi_name()); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
private $_configArray; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* This method allows you to directly inject a configuration array instead of using the config file |
|
132
|
|
|
* method. |
|
133
|
|
|
* |
|
134
|
|
|
* This method is commonly used when running php unit tests which do not require an additional file. |
|
135
|
|
|
* |
|
136
|
|
|
* ```php |
|
137
|
|
|
* $app = new Boot(); |
|
138
|
|
|
* $app->setConfigArray([ |
|
139
|
|
|
* // ... |
|
140
|
|
|
* ]); |
|
141
|
|
|
* ``` |
|
142
|
|
|
* |
|
143
|
|
|
* @param array $config The configuration array for the application. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setConfigArray(array $config) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->_configArray = $config; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* The prependConfigArray will be merged into the config, this way you can prepand config values for a custom Boot class. |
|
152
|
|
|
* |
|
153
|
|
|
* > When using prependConfig inside a custom boot class, the custom boot class will not used in the vendor bin file `./vendor/bin/luya`, |
|
154
|
|
|
* > so make sure to generate your own bin file. |
|
155
|
|
|
* |
|
156
|
|
|
* @return array |
|
157
|
|
|
*/ |
|
158
|
|
|
public function prependConfigArray() |
|
159
|
|
|
{ |
|
160
|
|
|
return []; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Get the config array from the configFile path with the predefined values. |
|
165
|
|
|
* |
|
166
|
|
|
* @throws \luya\Exception Throws exception if the config file does not exists. |
|
167
|
|
|
* @return array The array which will be injected into the Application Constructor. |
|
168
|
|
|
*/ |
|
169
|
|
|
public function getConfigArray() |
|
170
|
|
|
{ |
|
171
|
|
|
if ($this->_configArray === null) { |
|
172
|
|
|
if (!file_exists($this->configFile)) { |
|
173
|
|
|
if (!$this->getIsCli()) { |
|
174
|
|
|
throw new Exception("Unable to load the config file '".$this->configFile."'."); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
$config = ['id' => 'consoleapp', 'basePath' => dirname(__DIR__)]; |
|
178
|
|
|
} else { |
|
179
|
|
|
$config = require $this->configFile; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
if (!is_array($config)) { |
|
183
|
|
|
throw new Exception("config file '".$this->configFile."' found but no array returning."); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// preset the values from the defaultConfigArray |
|
187
|
|
|
if (!empty($this->prependConfigArray())) { |
|
188
|
|
|
$config = ArrayHelper::merge($config, $this->prependConfigArray()); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
$this->_configArray = $config; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
return $this->_configArray; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Run the application based on the Sapi Name. |
|
199
|
|
|
* |
|
200
|
|
|
* @return \luya\web\Application|\luya\console\Application Application objected based on the sapi name. |
|
201
|
|
|
*/ |
|
202
|
|
|
public function run() |
|
203
|
|
|
{ |
|
204
|
|
|
if ($this->getIsCli()) { |
|
205
|
|
|
return $this->applicationConsole(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $this->applicationWeb(); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
/** |
|
212
|
|
|
* Run Cli-Application based on the provided config file. |
|
213
|
|
|
* |
|
214
|
|
|
* @return string|integer |
|
215
|
|
|
*/ |
|
216
|
|
|
public function applicationConsole() |
|
217
|
|
|
{ |
|
218
|
|
|
$this->setIsCli(true); |
|
219
|
|
|
$config = $this->getConfigArray(); |
|
220
|
|
|
$config['defaultRoute'] = 'help'; |
|
221
|
|
|
if (isset($config['components'])) { |
|
222
|
|
|
if (isset($config['components']['composition'])) { |
|
223
|
|
|
unset($config['components']['composition']); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$this->includeYii(); |
|
228
|
|
|
|
|
229
|
|
|
$baseUrl = null; |
|
230
|
|
|
if (isset($config['consoleBaseUrl'])) { |
|
231
|
|
|
$baseUrl = $config['consoleBaseUrl']; |
|
232
|
|
|
} elseif (isset($config['consoleHostInfo'])) { |
|
233
|
|
|
$baseUrl = '/'; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
$mergedConfig = ArrayHelper::merge($config, [ |
|
237
|
|
|
'bootstrap' => ['luya\console\Bootstrap'], |
|
238
|
|
|
'components' => [ |
|
239
|
|
|
'urlManager' => [ |
|
240
|
|
|
'class' => 'yii\web\UrlManager', |
|
241
|
|
|
'enablePrettyUrl' => true, |
|
242
|
|
|
'showScriptName' => false, |
|
243
|
|
|
'baseUrl' => $baseUrl, |
|
244
|
|
|
'hostInfo' => isset($config['consoleHostInfo']) ? $config['consoleHostInfo'] : null, |
|
245
|
|
|
], |
|
246
|
|
|
], |
|
247
|
|
|
]); |
|
248
|
|
|
$this->app = new ConsoleApplication($mergedConfig); |
|
249
|
|
|
|
|
250
|
|
|
if (!$this->mockOnly) { |
|
251
|
|
|
exit($this->app->run()); |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Run Web-Application based on the provided config file. |
|
257
|
|
|
* |
|
258
|
|
|
* @return string Returns the Yii Application run() method if mock is disabled. Otherwise returns void |
|
259
|
|
|
*/ |
|
260
|
|
|
public function applicationWeb() |
|
261
|
|
|
{ |
|
262
|
|
|
$config = $this->getConfigArray(); |
|
263
|
|
|
$this->includeYii(); |
|
264
|
|
|
$mergedConfig = ArrayHelper::merge($config, ['bootstrap' => ['luya\web\Bootstrap']]); |
|
265
|
|
|
$this->app = new WebApplication($mergedConfig); |
|
266
|
|
|
|
|
267
|
|
|
if (!$this->mockOnly) { |
|
268
|
|
|
return $this->app->run(); |
|
269
|
|
|
} |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Returns the path to luya core files |
|
274
|
|
|
* |
|
275
|
|
|
* @return string The base path to the luya core folder. |
|
276
|
|
|
*/ |
|
277
|
|
|
public function getCoreBasePath() |
|
278
|
|
|
{ |
|
279
|
|
|
$reflector = new ReflectionClass(get_class($this)); |
|
280
|
|
|
return dirname($reflector->getFileName()); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Helper method to check whether the provided Yii Base file exists, if yes include and |
|
285
|
|
|
* return the file. |
|
286
|
|
|
* |
|
287
|
|
|
* @return bool Return value based on require_once command. |
|
288
|
|
|
* @throws Exception Throws Exception if the YiiBase file does not exists. |
|
289
|
|
|
*/ |
|
290
|
|
|
private function includeYii() |
|
291
|
|
|
{ |
|
292
|
|
|
if (file_exists($this->_baseYiiFile)) { |
|
293
|
|
|
defined('LUYA_YII_VENDOR') ?: define('LUYA_YII_VENDOR', dirname($this->_baseYiiFile)); |
|
294
|
|
|
|
|
295
|
|
|
$baseYiiFolder = LUYA_YII_VENDOR . DIRECTORY_SEPARATOR; |
|
296
|
|
|
$luyaYiiFile = $this->getCoreBasePath() . DIRECTORY_SEPARATOR . 'Yii.php'; |
|
297
|
|
|
|
|
298
|
|
|
if (file_exists($luyaYiiFile)) { |
|
299
|
|
|
require_once($baseYiiFolder . 'BaseYii.php'); |
|
300
|
|
|
require_once($luyaYiiFile); |
|
301
|
|
|
} else { |
|
302
|
|
|
require_once($baseYiiFolder . 'Yii.php'); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
Yii::setAlias('@luya', $this->getCoreBasePath()); |
|
306
|
|
|
|
|
307
|
|
|
return true; |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
throw new Exception("YiiBase file does not exits '".$this->_baseYiiFile."'."); |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|