|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace luya\traits; |
|
4
|
|
|
|
|
5
|
|
|
use Yii; |
|
6
|
|
|
use luya\base\AdminModuleInterface; |
|
7
|
|
|
use luya\base\Module; |
|
8
|
|
|
use luya\base\CoreModuleInterface; |
|
9
|
|
|
use luya\base\PackageInstaller; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* LUYA Appliation trait |
|
13
|
|
|
* |
|
14
|
|
|
* @property string $webroot Returns the webroot directory event for console commands. |
|
15
|
|
|
* @property \luya\components\Mail $mail Get luya mail component |
|
16
|
|
|
* |
|
17
|
|
|
* @author Basil Suter <[email protected]> |
|
18
|
|
|
* @since 1.0.0 |
|
19
|
|
|
*/ |
|
20
|
|
|
trait ApplicationTrait |
|
21
|
|
|
{ |
|
22
|
|
|
private $_webroot; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string Title for the application used in different sections like Login screen |
|
26
|
|
|
*/ |
|
27
|
|
|
public $siteTitle = 'LUYA Application'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var string|boolean Set a token, which will be used to collect data from a central host, if you want to enable this feature. |
|
31
|
|
|
* Use http://passwordsgenerator.net/ to create complex strings. When you have enabled this feature you can collect information's from |
|
32
|
|
|
* all your hosts with `example.com/admin/remote?token=Sha1EncodedRemoteToken`. |
|
33
|
|
|
*/ |
|
34
|
|
|
public $remoteToken = false; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var string The directory where your webroot represents, this is basically used to find the webroot directory |
|
38
|
|
|
* in the console mode, cause some importer classes need those variables. |
|
39
|
|
|
*/ |
|
40
|
|
|
public $webrootDirectory = 'public_html'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string This value will be used as hostInfo when running console applications in urlManager. An example for using the hostInfo |
|
44
|
|
|
* |
|
45
|
|
|
* ```php |
|
46
|
|
|
* 'consoleHostInfo' => 'https://luya.io' |
|
47
|
|
|
* ``` |
|
48
|
|
|
*/ |
|
49
|
|
|
public $consoleHostInfo; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string This value is used when declared for console request as urlManger baseUrl in order to enable urlHandling. If {{luya\web\traits\ApplicationTrait::$consoleHostInfo}} |
|
53
|
|
|
* is defined, consoleBaseUrl will use `/` as default value. The base url is the path where the application is running after hostInfo like |
|
54
|
|
|
* |
|
55
|
|
|
* ```php |
|
56
|
|
|
* 'consoleBaseUrl' => '/luya-kickstarter' |
|
57
|
|
|
* ``` |
|
58
|
|
|
* |
|
59
|
|
|
* But in the most cases when the website is online the baseUrl is `/` which is enabled by default when {{luya\web\traits\ApplicationTrait::$consoleHostInfo}} is defined. |
|
60
|
|
|
*/ |
|
61
|
|
|
public $consoleBaseUrl; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var array Add tags to the TagParser class. Example |
|
65
|
|
|
* |
|
66
|
|
|
* ```php |
|
67
|
|
|
* 'tags' => [ |
|
68
|
|
|
* 'foobar' => ['class' => '\app\tags\FoobarTag'], |
|
69
|
|
|
* ], |
|
70
|
|
|
* ``` |
|
71
|
|
|
*/ |
|
72
|
|
|
public $tags = []; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @var array Can override the localisation value used for php internal `setlocale()` method for specific language. For example |
|
76
|
|
|
* the language is de but the it should use the locale charset `de_CH.utf` (locale -a will return all locales installed on the server) |
|
77
|
|
|
* you can define them inside an array where key is the language and value the locale value to be used. |
|
78
|
|
|
* |
|
79
|
|
|
* ```php |
|
80
|
|
|
* public $locales = [ |
|
81
|
|
|
* 'de' => 'de_CH', |
|
82
|
|
|
* 'en' => 'en_GB', |
|
83
|
|
|
* ]; |
|
84
|
|
|
* ``` |
|
85
|
|
|
*/ |
|
86
|
|
|
public $locales = []; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Add trace info to luya application trait |
|
90
|
|
|
*/ |
|
91
|
|
|
public function init() |
|
92
|
|
|
{ |
|
93
|
|
|
parent::init(); |
|
94
|
|
|
|
|
95
|
|
|
// add trace info |
|
96
|
|
|
Yii::trace('initialize LUYA Application', __METHOD__); |
|
97
|
|
|
|
|
98
|
|
|
$this->setLocale($this->language); |
|
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Transform the $language into a locale sign to set php env settings. |
|
103
|
|
|
* |
|
104
|
|
|
* Example transform input `de` to `de_CH` when available $locales property. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $lang Find the locale POSIX for the provided $lang short code. |
|
107
|
|
|
* @return string The localisation code for the provided lang short code. |
|
108
|
|
|
*/ |
|
109
|
|
|
public function ensureLocale($lang) |
|
110
|
|
|
{ |
|
111
|
|
|
if (array_key_exists($lang, $this->locales)) { |
|
112
|
|
|
return $this->locales[$lang]; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (strlen($lang) == 2) { |
|
116
|
|
|
switch ($lang) { |
|
117
|
|
|
case 'de': |
|
118
|
|
|
return 'de_DE'; |
|
119
|
|
|
case 'fr': |
|
120
|
|
|
return 'fr_FR'; |
|
121
|
|
|
case 'it': |
|
122
|
|
|
return 'it_IT'; |
|
123
|
|
|
case 'ru': |
|
124
|
|
|
return 'ru_RU'; |
|
125
|
|
|
case 'en': |
|
126
|
|
|
return 'en_US'; |
|
127
|
|
|
case 'cn': |
|
128
|
|
|
return 'cn_CN'; |
|
129
|
|
|
default: |
|
130
|
|
|
return strtolower($lang) . '_' . strtoupper($lang); |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return $lang; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Setter method ensures the locilations POSIX from {{ensureLocale}} for the provided lang |
|
139
|
|
|
* and changes the Yii::$app->langauge and sets the `setlocale()` code from ensureLocale(). |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $lang The language short code to set the locale for. |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setLocale($lang) |
|
144
|
|
|
{ |
|
145
|
|
|
$locale = str_replace('.utf8', '', $this->ensureLocale($lang)); |
|
146
|
|
|
$this->language = $locale; |
|
147
|
|
|
setlocale(LC_ALL, $locale.'.utf8', $locale); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get the package Installer |
|
152
|
|
|
* @return \luya\base\PackageInstaller |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getPackageInstaller() |
|
155
|
|
|
{ |
|
156
|
|
|
$file = Yii::getAlias('@vendor/luyadev/installer.php'); |
|
157
|
|
|
|
|
158
|
|
|
$data = is_file($file) ? include $file : []; |
|
159
|
|
|
|
|
160
|
|
|
return new PackageInstaller($data); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @inheritdoc |
|
165
|
|
|
*/ |
|
166
|
|
|
protected function bootstrap() |
|
167
|
|
|
{ |
|
168
|
|
|
foreach ($this->getPackageInstaller()->getConfigs() as $config) { |
|
169
|
|
|
$this->bootstrap = array_merge($this->bootstrap, $config->bootstrap); |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
parent::bootstrap(); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Read only property which is used in cli bootstrap process to set the @webroot alias |
|
177
|
|
|
* |
|
178
|
|
|
* ```php |
|
179
|
|
|
* Yii::setAlias('@webroot', $app->webroot); |
|
180
|
|
|
* ``` |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getWebroot() |
|
183
|
|
|
{ |
|
184
|
|
|
if ($this->_webroot === null) { |
|
185
|
|
|
$this->_webroot = realpath(realpath($this->basePath) . DIRECTORY_SEPARATOR . $this->webrootDirectory); |
|
|
|
|
|
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return $this->_webroot; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Add additional core components to the yii2 base core components. |
|
193
|
|
|
*/ |
|
194
|
|
|
public function luyaCoreComponents() |
|
195
|
|
|
{ |
|
196
|
|
|
return array_merge(parent::coreComponents(), [ |
|
|
|
|
|
|
197
|
|
|
'mail' => ['class' => 'luya\components\Mail'], |
|
198
|
|
|
'formatter' => ['class' => 'luya\components\Formatter'], |
|
199
|
|
|
]); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Get an array with all modules which are an instance of the `luya\base\Module`. |
|
204
|
|
|
* |
|
205
|
|
|
* @return \luya\base\Module |
|
206
|
|
|
*/ |
|
207
|
|
View Code Duplication |
public function getApplicationModules() |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
$modules = []; |
|
210
|
|
|
|
|
211
|
|
|
foreach ($this->getModules() as $id => $obj) { |
|
|
|
|
|
|
212
|
|
|
if ($obj instanceof Module) { |
|
213
|
|
|
$modules[$id] = $obj; |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
return $modules; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Return a list with all registered frontend modules except 'luya' and 'cms'. This is needed in the module block. |
|
222
|
|
|
* |
|
223
|
|
|
* @return \luya\base\Module |
|
224
|
|
|
*/ |
|
225
|
|
View Code Duplication |
public function getFrontendModules() |
|
|
|
|
|
|
226
|
|
|
{ |
|
227
|
|
|
$modules = []; |
|
228
|
|
|
|
|
229
|
|
|
foreach ($this->getModules() as $id => $obj) { |
|
|
|
|
|
|
230
|
|
|
if ($obj instanceof Module && !$obj instanceof AdminModuleInterface && !$obj instanceof CoreModuleInterface) { |
|
231
|
|
|
$modules[$id] = $obj; |
|
232
|
|
|
} |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
return $modules; |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Return all Admin Module Interface implementing modules. |
|
240
|
|
|
* |
|
241
|
|
|
* @return \luya\base\AdminModuleInterface |
|
242
|
|
|
*/ |
|
243
|
|
View Code Duplication |
public function getAdminModules() |
|
|
|
|
|
|
244
|
|
|
{ |
|
245
|
|
|
$modules = []; |
|
246
|
|
|
|
|
247
|
|
|
foreach ($this->getModules() as $id => $obj) { |
|
|
|
|
|
|
248
|
|
|
if ($obj instanceof Module && $obj instanceof AdminModuleInterface) { |
|
249
|
|
|
$modules[$id] = $obj; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
return $modules; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: