Completed
Push — master ( b30129...e703d5 )
by Андрей
07:21 queued 05:06
created

OverrideAppConfigTrait::overrideAppConfigHandler()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 1
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 Zend\ModuleManager\ModuleEvent;
9
use Zend\ServiceManager\ServiceManager;
10
use Zend\Stdlib\ArrayUtils;
11
12
13
/**
14
 * Class OverrideAppConfigTrait
15
 *
16
 * @package Nnx\ZF2TestToolkit\Utils
17
 */
18
trait OverrideAppConfigTrait
19
{
20
    /**
21
     * ServiceListener
22
     *
23
     * @var ServiceListenerForTest
24
     */
25
    protected $serviceListenerForTest;
26
27
    /**
28
     * Конфиги для переопределения, конфига приложения
29
     *
30
     * @var array
31
     */
32
    protected $overrideConfigs = [];
33
34
    /**
35
     * Устанавливает конфиг приолжения
36
     *
37
     * @param $applicationConfig
38
     *
39
     * @return void
40
     */
41
    abstract public function setApplicationConfig($applicationConfig);
42
43
    /**
44
     * Возвращает конфиг приложения
45
     *
46
     * @return array
47
     */
48
    abstract public function getApplicationConfig();
49
50
    /**
51
     * Расширяет конфиг приложения
52
     *
53
     * @param array $newConfig
54
     *
55
     * @throws \Zend\ServiceManager\Exception\ServiceNotFoundException
56
     * @throws \Zend\ServiceManager\Exception\InvalidServiceNameException
57
     */
58
    public function overrideAppConfig(array $newConfig = [])
59
    {
60
        $applicationConfig = $this->getApplicationConfig();
61
62
        if (is_array($applicationConfig) && !isset($applicationConfig['service_manager']['factories']['ServiceListenerInterface'])) {
63
            $appConfig = ArrayUtils::merge($applicationConfig, [
64
                'service_manager' => [
65
                    'factories' => [
66
                        'ServiceListenerInterface' => [$this, 'serviceListenerInterfaceFactory'],
67
                    ]
68
                ]
69
            ]);
70
71
            $this->setApplicationConfig($appConfig);
72
        }
73
74
        $this->addOverrideConfig($newConfig);
75
    }
76
77
78
    /**
79
     * Метод фабрика, отвечающий за создание ServiceListener
80
     *
81
     * @param ServiceManager $sm
82
     *
83
     * @return ServiceListenerForTest
84
     */
85
    public function serviceListenerInterfaceFactory(ServiceManager $sm)
86
    {
87
        if ($this->serviceListenerForTest) {
88
            return $this->serviceListenerForTest;
89
        }
90
91
        $this->serviceListenerForTest = new ServiceListenerForTest($sm);
92
        $this->serviceListenerForTest->setOverrideAppConfigHandler([$this, 'overrideAppConfigHandler']);
93
94
        return $this->serviceListenerForTest;
95
    }
96
97
    /**
98
     * Добавляет конфиг, который будет переопределять, конфиг приложения
99
     *
100
     * @param array $overrideConfigs
101
     *
102
     * @return $this
103
     */
104
    protected function addOverrideConfig(array $overrideConfigs = [])
105
    {
106
        $this->overrideConfigs[] = $overrideConfigs;
107
108
        return $this;
109
    }
110
111
    /**
112
     * Наборк конфигов, которые заменяют конфиг приложения
113
     *
114
     * @return array
115
     */
116
    public function getOverrideConfigs()
117
    {
118
        return $this->overrideConfigs;
119
    }
120
121
122
    /**
123
     * Обработчик события загрузки всех модулей
124
     *
125
     * @param ModuleEvent $e
126
     */
127
    public function overrideAppConfigHandler(ModuleEvent $e)
128
    {
129
        $configListener = $e->getConfigListener();
130
        $baseAppConfig         = $configListener->getMergedConfig(false);
131
132
        $overrideConfigs = $this->getOverrideConfigs();
133
        foreach ($overrideConfigs as $overrideConfig) {
134
            $baseAppConfig = ArrayUtils::merge($baseAppConfig, $overrideConfig);
135
        }
136
137
        $configListener->setMergedConfig($baseAppConfig);
138
    }
139
}
140