-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathModule.php
304 lines (275 loc) · 10.3 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace myzero1\yii2giiplus;
use Yii;
use yii\base\BootstrapInterface;
use yii\web\ForbiddenHttpException;
/**
* This is the main module class for the Gii module.
*
* To use Gii, include it as a module in the application configuration like the following:
*
* ~~~
* return [
* 'bootstrap' => ['gii'],
* 'modules' => [
* 'gii' => ['class' => 'yii\gii\Module'],
* ],
* ]
* ~~~
*
* Because Gii generates new code files on the server, you should only use it on your own
* development machine. To prevent other people from using this module, by default, Gii
* can only be accessed by localhost. You may configure its [[allowedIPs]] property if
* you want to make it accessible on other machines.
*
* With the above configuration, you will be able to access GiiModule in your browser using
* the URL `http://localhost/path/to/index.php?r=gii`
*
* If your application enables [[\yii\web\UrlManager::enablePrettyUrl|pretty URLs]],
* you can then access Gii via URL: `http://localhost/path/to/index.php/gii`
*
* @author Qiang Xue <[email protected]>
* @since 2.0
*/
class Module extends \yii\base\Module implements BootstrapInterface
{
/**
* @inheritdoc
*/
public $controllerNamespace = 'yii\gii\controllers';
/**
* @var array the list of IPs that are allowed to access this module.
* Each array element represents a single IP filter which can be either an IP address
* or an address with wildcard (e.g. 192.168.0.*) to represent a network segment.
* The default value is `['127.0.0.1', '::1']`, which means the module can only be accessed
* by localhost.
*/
public $allowedIPs = ['127.0.0.1', '::1'];
/**
* @var array|Generator[] a list of generator configurations or instances. The array keys
* are the generator IDs (e.g. "crud"), and the array elements are the corresponding generator
* configurations or the instances.
*
* After the module is initialized, this property will become an array of generator instances
* which are created based on the configurations previously taken by this property.
*
* Newly assigned generators will be merged with the [[coreGenerators()|core ones]], and the former
* takes precedence in case when they have the same generator ID.
*/
public $generators = [];
/**
* @var integer the permission to be set for newly generated code files.
* This value will be used by PHP chmod function.
* Defaults to 0666, meaning the file is read-writable by all users.
*/
public $newFileMode = 0666;
/**
* @var integer the permission to be set for newly generated directories.
* This value will be used by PHP chmod function.
* Defaults to 0777, meaning the directory can be read, written and executed by all users.
*/
public $newDirMode = 0777;
/**
* @var integer the permission to be set for newly generated directories.
* This value will be used by PHP chmod function.
* Defaults to 0777, meaning the directory can be read, written and executed by all users.
*/
public $myzero1Tools = [];
/**
* @inheritdoc
*/
public function bootstrap($app)
{
if ($app instanceof \yii\web\Application) {
$app->getUrlManager()->addRules([
['class' => 'yii\web\UrlRule', 'pattern' => 'myzero1' . $this->id, 'route' => $this->id . '/default/index'],
['class' => 'yii\web\UrlRule', 'pattern' => 'myzero1' . $this->id . '/<id:\w+>', 'route' => $this->id . '/default/view'],
['class' => 'yii\web\UrlRule', 'pattern' => 'myzero1' . $this->id . '/<controller:[\w\-]+>/<action:[\w\-]+>', 'route' => $this->id . '/<controller>/<action>'],
], false);
} elseif ($app instanceof \yii\console\Application) {
$app->controllerMap[$this->id] = [
'class' => 'yii\gii\console\GenerateController',
'generators' => array_merge($this->coreGenerators(), $this->generators),
'module' => $this,
];
}
// add the examples to app
$app->setModules(
[
'redactor' => [
'class' => 'yii\redactor\RedactorModule',
'uploadDir' => '@webroot',
'uploadUrl' => '@web',
],
]
);
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->myzero1Init();
$this->addDefaultGii($this);
$this->addExample($this); // add the examples to myzero1 module
}
/**
* @inheritdoc
*/
public function beforeAction($action)
{
if (!parent::beforeAction($action)) {
return false;
}
if (Yii::$app instanceof \yii\web\Application && !$this->checkAccess()) {
throw new ForbiddenHttpException('You are not allowed to access this page.');
}
$this->resetGlobalSettings();
return true;
}
/**
* Resets potentially incompatible global settings done in app config.
*/
protected function resetGlobalSettings()
{
if (Yii::$app instanceof \yii\web\Application) {
Yii::$app->assetManager->bundles = [];
}
}
/**
* @return boolean whether the module can be accessed by the current user
*/
protected function checkAccess()
{
$ip = Yii::$app->getRequest()->getUserIP();
foreach ($this->allowedIPs as $filter) {
if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
return true;
}
}
Yii::warning('Access to Gii is denied due to IP address restriction. The requested IP is ' . $ip, __METHOD__);
return false;
}
protected function addDefaultGii($app)
{
$generators = [
'myzero1_home' => [
'class' => \myzero1\yii2giiplus\generators\myzero1\Generator::class,
],
'myzero1_theming' => [
'class' => \myzero1\yii2giiplus\generators\theming\Generator::class,
],
'myzero1_upload' => [
'class' => \myzero1\yii2giiplus\generators\upload\Generator::class,
],
'myzero1_captcha' => [
'class' => \myzero1\yii2giiplus\generators\captcha\Generator::class,
],
'myzero1_wysiwyg' => [
'class' => \myzero1\yii2giiplus\generators\wysiwyg\Generator::class,
],
// 'myzero1_mvc' => [
// 'class' => \myzero1\yii2giiplus\generators\mvc\Generator::class,
// ],
'crud' => [
'class' => \yii\gii\generators\crud\Generator::class,
'templates' => [
'adminlte' => '@myzero1/yii2giiplus/generators/theming/default/adminlte/_gii_templates/crud',
],
'template' => 'adminlte',
'messageCategory' => 'backend'
],
];
$this->generators = array_merge($generators, $this->generators);
$app->setModules(
[
'gii' => [
'class' => '\yii\gii\Module',
'allowedIPs' => $this->allowedIPs,
'generators' => $this->generators,
]
]
);
}
/**
* @return string the controller namespace of the module.
*/
protected function addExample($app)
{
$app->setModules(
[
'myzero1_upload' => [
'class' => 'myzero1\yii2upload\Tools',
'upload' => [
'basePath' => '@webroot/myzero1_upload',
'baseUrl' => '@web/myzero1_upload',
],
]
]
);
}
/**
* @return string the controller namespace of the module.
*/
protected function myzero1Init()
{
$moduleId = $this->getModuleId();
$this->myzero1Tools = [
'myzero1_theming' => [
'name' => 'Myzero1 theming Generator',
'description' => 'This is a description.',
'url' => $this->getUrlInModule(['/'.$moduleId.'/gii/default/view', 'id' => 'myzero1_theming']),
],
'myzero1_upload' => [
'name' => 'Myzero1 upload Widget',
'description' => 'This is a description.',
'url' => $this->getUrlInModule(['/'.$moduleId.'/gii/default/view', 'id' => 'myzero1_upload']),
],
'myzero1_captcha' => [
'name' => 'Myzero1 captcha Widget',
'description' => 'This is a description.',
'url' => $this->getUrlInModule(['/'.$moduleId.'/gii/default/view', 'id' => 'myzero1_captcha']),
],
'myzero1_wysiwyg' => [
'name' => 'Redactor WYSIWYG Generator',
'description' => 'Extension Redactor WYSIWYG for Yii2 framework.',
'url' => $this->getUrlInModule(['/'.$moduleId.'/gii/default/view', 'id' => 'myzero1_wysiwyg']),
],
];
}
/**
* Normalizes [[actions]] into an array of action IDs.
* @return array an array of action IDs entered by the user
*/
public function getUrlInModule( $params )
{
$sUrl = Yii::$app->getUrlManager()->createUrl($params);
$sUrlEnd = str_replace('?r=index.php', '', $sUrl);
return $sUrlEnd;
}
/**
* Normalizes [[actions]] into an array of action IDs.
* @return array an array of action IDs entered by the user
*/
public function getModuleId()
{
foreach (Yii::$app->getModules() as $key => $mModule) {
if (is_array($mModule)) {
if (trim($mModule['class'], '\\') == 'myzero1\yii2giiplus\Module') {
$moduleId = $key;
}
} else {
if (trim($mModule::className(), '\\') == 'myzero1\yii2giiplus\Module') {
$moduleId = $mModule->id;
}
}
}
return $moduleId;
}
}