1
|
|
|
<?php |
2
|
|
|
namespace app\components; |
3
|
|
|
|
4
|
|
|
use Yii; |
5
|
|
|
use yii\web\Application; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Yii2 application mock class |
9
|
|
|
* @package app\components |
10
|
|
|
*/ |
11
|
|
|
class ApplicationMock extends Application |
12
|
|
|
{ |
13
|
|
|
/** @var array currently used application configuration */ |
14
|
|
|
public static $config; |
15
|
|
|
|
16
|
|
|
/** @inheritdoc */ |
17
|
|
|
public function __construct($config = []) |
18
|
|
|
{ |
19
|
|
|
if (self::$config === null) { |
20
|
|
|
// if first call, store original configuration |
21
|
|
|
self::$config = $config; |
22
|
|
|
} |
23
|
|
|
parent::__construct(self::$config); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Change module configuration |
28
|
|
|
* @param string $moduleId module id |
29
|
|
|
* @param array $params new params |
30
|
|
|
* @param bool $merge whether to merge new and old parameters |
31
|
|
|
*/ |
32
|
|
|
public static function changeModule($moduleId, $params = [], $merge = true) |
33
|
|
|
{ |
34
|
|
|
if (empty(self::$config['modules'])) { |
35
|
|
|
// create module field |
36
|
|
|
self::$config['modules'] = []; |
37
|
|
|
} |
38
|
|
|
if (!empty(self::$config['modules'][$moduleId]) && $merge) { |
39
|
|
|
// if we need merge configuration data |
40
|
|
|
$currentParams = self::$config['modules'][$moduleId]; |
41
|
|
|
if (is_string($currentParams)) { |
42
|
|
|
$currentParams = [ |
43
|
|
|
'class' => $currentParams, |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
foreach ($params as $name => $value) { |
47
|
|
|
$currentParams[$name] = $value; |
48
|
|
|
} |
49
|
|
|
$params = $currentParams; |
50
|
|
|
} |
51
|
|
|
self::$config['modules'][$moduleId] = $params; |
52
|
|
|
// apply for current $app instance |
53
|
|
|
$module = Yii::$app->getModule($moduleId); |
54
|
|
|
if (!$merge || $module === null || (!empty($params['class']) && $module::className() != $params['class'])) { |
55
|
|
|
// new or changed |
56
|
|
|
Yii::$app->setModule($moduleId, $params); |
57
|
|
|
} else { |
58
|
|
|
// change the attributes |
59
|
|
|
foreach ($params as $name => $value) { |
60
|
|
|
if ($name !== 'class' && $module->canSetProperty($name)) { |
61
|
|
|
$module->$name = $value; |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|