1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/zf2-test-toolkit |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\ZF2TestToolkit\Utils; |
7
|
|
|
|
8
|
|
|
use Nnx\ZF2TestToolkit\Listener\CallbacksListenerAggregate; |
9
|
|
|
use Zend\Mvc\MvcEvent; |
10
|
|
|
use Zend\ServiceManager\ServiceManager; |
11
|
|
|
use Zend\Stdlib\ArrayUtils; |
12
|
|
|
use Ramsey\Uuid\Uuid; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class OverrideAppConfigTrait |
16
|
|
|
* |
17
|
|
|
* @package Nnx\ZF2TestToolkit\Utils |
18
|
|
|
*/ |
19
|
|
|
trait OverrideAppConfigTrait |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Устанавливает конфиг приолжения |
23
|
|
|
* |
24
|
|
|
* @param $applicationConfig |
25
|
|
|
* |
26
|
|
|
* @return void |
27
|
|
|
*/ |
28
|
|
|
abstract public function setApplicationConfig($applicationConfig); |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Возвращает конфиг приложения |
32
|
|
|
* |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
|
|
abstract public function getApplicationConfig(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Расширяет конфиг приложения |
39
|
|
|
* |
40
|
|
|
* @param array $newConfig |
41
|
|
|
* |
42
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
43
|
|
|
* @throws \Zend\ServiceManager\Exception\InvalidServiceNameException |
44
|
|
|
*/ |
45
|
|
|
public function overrideAppConfig(array $newConfig = []) |
46
|
|
|
{ |
47
|
|
|
$applicationConfig = $this->getApplicationConfig(); |
48
|
|
|
|
49
|
|
|
if (is_array($applicationConfig)) { |
50
|
|
|
$listenerName = 'bootstrapAppHandler_' . Uuid::uuid4()->toString(); |
51
|
|
|
|
52
|
|
|
$appConfig = ArrayUtils::merge($applicationConfig, [ |
53
|
|
|
'listeners' => [ |
54
|
|
|
$listenerName |
55
|
|
|
], |
56
|
|
|
'service_manager' => [ |
57
|
|
|
'services' => [ |
58
|
|
|
$listenerName => new CallbacksListenerAggregate([ |
59
|
|
|
MvcEvent::EVENT_BOOTSTRAP => [ |
60
|
|
|
[ |
61
|
|
|
'handler' => function (MvcEvent $e) use ($newConfig) { |
62
|
|
|
/** @var ServiceManager $sm */ |
63
|
|
|
$sm = $e->getApplication()->getServiceManager(); |
64
|
|
|
$baseAppConfig = $sm->get('Config'); |
65
|
|
|
|
66
|
|
|
$appConfig = ArrayUtils::merge($baseAppConfig, $newConfig); |
|
|
|
|
67
|
|
|
|
68
|
|
|
|
69
|
|
|
$originalAllowOverride = $sm->getAllowOverride(); |
70
|
|
|
$sm->setAllowOverride(true); |
71
|
|
|
$sm->setService('config', $appConfig); |
72
|
|
|
$sm->setAllowOverride($originalAllowOverride); |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
] |
76
|
|
|
] |
77
|
|
|
]) |
78
|
|
|
] |
79
|
|
|
] |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
$this->setApplicationConfig($appConfig); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.