1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/doctrine-fixture-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\DoctrineFixtureModule\Loader; |
7
|
|
|
|
8
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
9
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
10
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
11
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
12
|
|
|
use Nnx\DoctrineFixtureModule\Options\ModuleOptions; |
13
|
|
|
use Nnx\DoctrineFixtureModule\Module; |
14
|
|
|
use Doctrine\Fixture\Loader\ChainLoader; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class FixtureLoaderAbstractFactory |
19
|
|
|
* |
20
|
|
|
* @package Nnx\DoctrineFixtureModule\Loader |
21
|
|
|
*/ |
22
|
|
|
class FixtureLoaderAbstractFactory |
23
|
|
|
implements AbstractFactoryInterface |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Флаг определеят была ли инициализирована фабрика |
28
|
|
|
* |
29
|
|
|
* @var bool |
30
|
|
|
*/ |
31
|
|
|
protected $isInit = false; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Конфиг с настройками загрузчиков фикстур |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $fixtureLoaderConfig = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Инициализация фабрики |
42
|
|
|
* |
43
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
44
|
|
|
* |
45
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
46
|
|
|
*/ |
47
|
|
View Code Duplication |
protected function initFactory(ServiceLocatorInterface $serviceLocator) |
|
|
|
|
48
|
|
|
{ |
49
|
|
|
if (true === $this->isInit) { |
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$appServiceLocator = $serviceLocator; |
54
|
|
|
if ($serviceLocator instanceof AbstractPluginManager) { |
55
|
|
|
$appServiceLocator = $serviceLocator->getServiceLocator(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */ |
59
|
|
|
$moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
60
|
|
|
/** @var ModuleOptions $moduleOptions */ |
61
|
|
|
$moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class); |
62
|
|
|
|
63
|
|
|
$this->fixtureLoaderConfig = $moduleOptions->getFixturesLoaders(); |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
$this->isInit = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritDoc |
72
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
73
|
|
|
*/ |
74
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
75
|
|
|
{ |
76
|
|
|
$this->initFactory($serviceLocator); |
77
|
|
|
|
78
|
|
|
return array_key_exists($requestedName, $this->fixtureLoaderConfig); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
84
|
|
|
* @throws \Nnx\DoctrineFixtureModule\Loader\Exception\RuntimeException |
85
|
|
|
*/ |
86
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
87
|
|
|
{ |
88
|
|
|
$this->initFactory($serviceLocator); |
89
|
|
|
|
90
|
|
|
/** @var FixtureLoaderManagerInterface $serviceLocator*/ |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
$loadersConfigs = $this->fixtureLoaderConfig[$requestedName]; |
94
|
|
|
|
95
|
|
|
$chains = []; |
96
|
|
|
foreach ($loadersConfigs as $index => $item) { |
97
|
|
View Code Duplication |
if (!is_array($item)) { |
|
|
|
|
98
|
|
|
$errMsg = sprintf( |
99
|
|
|
'Item [%s][fixtures][%s] of type %s is invalid. Must array', |
100
|
|
|
Module::CONFIG_KEY, |
101
|
|
|
$index, |
102
|
|
|
(is_object($item) ? get_class($item) : gettype($item)) |
103
|
|
|
); |
104
|
|
|
throw new Exception\RuntimeException($errMsg); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
View Code Duplication |
if (!array_key_exists('name', $item)) { |
|
|
|
|
108
|
|
|
$errMsg = sprintf( |
109
|
|
|
'Required parameter [%s][fixtures][%s][\'name\'] not found', |
110
|
|
|
Module::CONFIG_KEY, |
111
|
|
|
$index |
112
|
|
|
); |
113
|
|
|
throw new Exception\RuntimeException($errMsg); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
View Code Duplication |
if (!is_string($item['name'])) { |
|
|
|
|
117
|
|
|
$errMsg = sprintf( |
118
|
|
|
'Parameter [%s][fixtures][%s][\'name\'] of type %s is invalid. Must string', |
119
|
|
|
Module::CONFIG_KEY, |
120
|
|
|
$index, |
121
|
|
|
(is_object($item['name']) ? get_class($item['name']) : gettype($item['name'])) |
122
|
|
|
); |
123
|
|
|
throw new Exception\RuntimeException($errMsg); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
$name = $item['name']; |
127
|
|
|
$options = array_key_exists('options', $item) ? $item['options'] : []; |
128
|
|
View Code Duplication |
if (!is_array($options)) { |
|
|
|
|
129
|
|
|
$errMsg = sprintf( |
130
|
|
|
'Parameter [%s][fixtures][%s][\'options\'] of type %s is invalid. Must array', |
131
|
|
|
Module::CONFIG_KEY, |
132
|
|
|
$index, |
133
|
|
|
(is_object($options) ? get_class($options) : gettype($options)) |
134
|
|
|
); |
135
|
|
|
throw new Exception\RuntimeException($errMsg); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$chains[] = $serviceLocator->get($name, $options); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $serviceLocator->get(ChainLoader::class, [ |
|
|
|
|
142
|
|
|
'loaderList' => $chains |
143
|
|
|
]); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|