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 Nnx\DoctrineFixtureModule\FixtureInitializer\FixtureInitializerManagerInterface; |
9
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
10
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
11
|
|
|
use Zend\ServiceManager\FactoryInterface; |
12
|
|
|
use Zend\ServiceManager\MutableCreationOptionsInterface; |
13
|
|
|
use Zend\ServiceManager\MutableCreationOptionsTrait; |
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
15
|
|
|
use Nnx\DoctrineFixtureModule\Filter\FixtureFilterManagerInterface; |
16
|
|
|
use Nnx\DoctrineFixtureModule\Loader\FixtureLoaderManagerInterface; |
17
|
|
|
use ReflectionClass; |
18
|
|
|
use Doctrine\Fixture\Configuration; |
19
|
|
|
use Nnx\DoctrineFixtureModule\Options\ModuleOptions; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class ExecutorFactory |
23
|
|
|
* |
24
|
|
|
* @package Nnx\DoctrineFixtureModule\Executor |
25
|
|
|
*/ |
26
|
|
|
class ExecutorFactory implements FactoryInterface, MutableCreationOptionsInterface |
27
|
|
|
{ |
28
|
|
|
use MutableCreationOptionsTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @inheritDoc |
32
|
|
|
* |
33
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
34
|
|
|
* @throws \Nnx\DoctrineFixtureModule\Executor\Exception\RuntimeException |
35
|
|
|
*/ |
36
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
37
|
|
|
{ |
38
|
|
|
$appServiceLocator = $serviceLocator instanceof AbstractPluginManager ? $serviceLocator->getServiceLocator() :$serviceLocator; |
39
|
|
|
|
40
|
|
|
$creationOptions = $this->getCreationOptions(); |
41
|
|
|
|
42
|
|
|
$configurationServiceName = DefaultExecutorConfiguration::class; |
43
|
|
|
if (array_key_exists('configuration', $creationOptions)) { |
44
|
|
|
$configurationServiceName = $creationOptions['configuration']; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
View Code Duplication |
if ($appServiceLocator->has($configurationServiceName)) { |
|
|
|
|
48
|
|
|
/** @var Configuration $configuration */ |
49
|
|
|
$configuration = $appServiceLocator->get($configurationServiceName); |
50
|
|
|
} elseif (class_exists($configurationServiceName)) { |
51
|
|
|
$r = new ReflectionClass($configurationServiceName); |
52
|
|
|
/** @var Configuration $configuration */ |
53
|
|
|
$configuration = $r->newInstance(); |
54
|
|
|
} else { |
55
|
|
|
$errMsg = 'Invalid fixture executor configuration'; |
56
|
|
|
throw new Exception\RuntimeException($errMsg); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$builderServiceName = FixtureExecutorBuilderInterface::class; |
60
|
|
|
if (array_key_exists('builder', $creationOptions)) { |
61
|
|
|
$builderServiceName = $creationOptions['builder']; |
62
|
|
|
} |
63
|
|
View Code Duplication |
if ($appServiceLocator->has($builderServiceName)) { |
|
|
|
|
64
|
|
|
/** @var FixtureExecutorBuilderInterface $builder */ |
65
|
|
|
$builder = $appServiceLocator->get($builderServiceName); |
66
|
|
|
} elseif (class_exists($builderServiceName)) { |
67
|
|
|
$r = new ReflectionClass($builderServiceName); |
68
|
|
|
/** @var FixtureExecutorBuilderInterface $builder */ |
69
|
|
|
$builder = $r->newInstance(); |
70
|
|
|
} else { |
71
|
|
|
$errMsg = 'Invalid fixture executor builder'; |
72
|
|
|
throw new Exception\RuntimeException($errMsg); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
/** @var FixtureInitializerManagerInterface $fixtureInitializerManager */ |
77
|
|
|
$fixtureInitializerManager = $appServiceLocator->get(FixtureInitializerManagerInterface::class); |
78
|
|
|
|
79
|
|
|
$executor = new Executor($configuration, $builder, $fixtureInitializerManager); |
80
|
|
|
|
81
|
|
|
|
82
|
|
|
if (array_key_exists('fixturesLoader', $creationOptions)) { |
83
|
|
|
/** @var FixtureLoaderManagerInterface $fixtureLoaderManager */ |
84
|
|
|
$fixtureLoaderManager = $appServiceLocator->get(FixtureLoaderManagerInterface::class); |
85
|
|
|
|
86
|
|
|
$loaderCreationOptions = [ |
87
|
|
|
'contextExecutor' => $executor |
88
|
|
|
]; |
89
|
|
|
$fixtureLoader = $fixtureLoaderManager->get($creationOptions['fixturesLoader'], $loaderCreationOptions); |
|
|
|
|
90
|
|
|
|
91
|
|
|
$executor->setLoader($fixtureLoader); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
if (array_key_exists('filter', $creationOptions)) { |
96
|
|
|
/** @var FixtureFilterManagerInterface $fixtureFilterManager */ |
97
|
|
|
$fixtureFilterManager = $appServiceLocator->get(FixtureFilterManagerInterface::class); |
98
|
|
|
|
99
|
|
|
$filterCreationOptions = [ |
100
|
|
|
'contextExecutor' => $executor |
101
|
|
|
]; |
102
|
|
|
$fixtureFilter = $fixtureFilterManager->get($creationOptions['filter'], $filterCreationOptions); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$executor->setFilter($fixtureFilter); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if (array_key_exists('name', $creationOptions)) { |
108
|
|
|
$executor->setName($creationOptions['name']); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsPluginManager */ |
112
|
|
|
$moduleOptionsPluginManager = $appServiceLocator->get(ModuleOptionsPluginManagerInterface::class); |
113
|
|
|
|
114
|
|
|
/** @var ModuleOptions $moduleOptions */ |
115
|
|
|
$moduleOptions = $moduleOptionsPluginManager->get(ModuleOptions::class); |
116
|
|
|
|
117
|
|
|
$executor->setContextInitializer($moduleOptions->getContextInitializer()); |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
return $executor; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.