1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/doctrine-fixture-module |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\DoctrineFixtureModule\Executor; |
7
|
|
|
|
8
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
9
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
10
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
11
|
|
|
use Nnx\DoctrineFixtureModule\Options\ModuleOptions; |
12
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class AbstractExecutorFactory |
17
|
|
|
* |
18
|
|
|
* @package Nnx\DoctrineFixtureModule\Executor |
19
|
|
|
*/ |
20
|
|
|
class AbstractExecutorFactory implements AbstractFactoryInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Флаг определяет была ли инциализированна фабрика |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $isInit = false; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Настройки модуля |
31
|
|
|
* |
32
|
|
|
* @var ModuleOptions |
33
|
|
|
*/ |
34
|
|
|
protected $moduleOptions; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Инициализация фабрики |
38
|
|
|
* |
39
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
protected function init(ServiceLocatorInterface $serviceLocator) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
if ($this->isInit) { |
47
|
|
|
return null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$appServiceLocator = $serviceLocator; |
51
|
|
|
if ($serviceLocator instanceof AbstractPluginManager) { |
52
|
|
|
$appServiceLocator = $serviceLocator->getServiceLocator(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */ |
56
|
|
|
$moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
57
|
|
|
/** @var ModuleOptions $moduleOptions */ |
58
|
|
|
$this->moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class); |
59
|
|
|
|
60
|
|
|
$this->isInit = true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @inheritDoc |
66
|
|
|
* |
67
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
68
|
|
|
*/ |
69
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
70
|
|
|
{ |
71
|
|
|
$this->init($serviceLocator); |
72
|
|
|
|
73
|
|
|
return array_key_exists($requestedName, $this->moduleOptions->getExecutors()); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @inheritDoc |
78
|
|
|
* |
79
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
80
|
|
|
*/ |
81
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
82
|
|
|
{ |
83
|
|
|
$this->init($serviceLocator); |
84
|
|
|
|
85
|
|
|
$executors = $this->moduleOptions->getExecutors(); |
86
|
|
|
|
87
|
|
|
$creationOptions = $executors[$requestedName]; |
88
|
|
|
|
89
|
|
|
$creationOptions['name'] = $requestedName; |
90
|
|
|
|
91
|
|
|
/** @var FixtureExecutorManagerInterface $serviceLocator*/ |
92
|
|
|
return $serviceLocator->get(Executor::class, $creationOptions); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.