1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @year 2016 |
4
|
|
|
* @link https://github.com/nnx-framework/cloner |
5
|
|
|
* @author Lobanov Aleksandr <[email protected]> |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Nnx\Cloner; |
9
|
|
|
|
10
|
|
|
use Assert\Assertion; |
11
|
|
|
use Nnx\Cloner\Options\ClonerOptions; |
12
|
|
|
use Nnx\Cloner\Options\ModuleOptions; |
13
|
|
|
use Nnx\ModuleOptions\ModuleOptionsPluginManagerInterface; |
14
|
|
|
use Zend\ServiceManager\AbstractFactoryInterface; |
15
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
16
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
17
|
|
|
|
18
|
|
|
|
19
|
|
|
class ClonerAbstractFactory implements AbstractFactoryInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array|null |
24
|
|
|
*/ |
25
|
|
|
private $clonersConfig; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
29
|
|
|
* @param $name |
30
|
|
|
* @param $requestedName |
31
|
|
|
* |
32
|
|
|
* @return bool |
33
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
34
|
|
|
* @throws \Assert\AssertionFailedException |
35
|
|
|
*/ |
36
|
|
|
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
37
|
|
|
{ |
38
|
|
|
$serviceManager = $serviceLocator instanceof AbstractPluginManager |
39
|
|
|
? $serviceLocator->getServiceLocator() : $serviceLocator; |
40
|
|
|
|
41
|
|
|
if ($this->clonersConfig === null) { |
42
|
|
|
$this->clonersConfig = $this->getClonersConfig($serviceManager); |
43
|
|
|
} |
44
|
|
|
if (array_key_exists($requestedName, $this->clonersConfig)) { |
45
|
|
|
if (array_key_exists('class', $this->clonersConfig[$requestedName])) { |
46
|
|
|
return is_a($this->clonersConfig[$requestedName]['class'], Cloner::class); |
47
|
|
|
} else { |
48
|
|
|
return true; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
56
|
|
|
* @param $name |
57
|
|
|
* @param $requestedName |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
* @throws \Assert\AssertionFailedException |
61
|
|
|
*/ |
62
|
|
|
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName) |
63
|
|
|
{ |
64
|
|
|
Assertion::isArray($this->clonersConfig[$requestedName]); |
65
|
|
|
$options = new Options\ClonerOptions($this->clonersConfig[$requestedName]); |
66
|
|
|
|
67
|
|
|
Assertion::isInstanceOf($serviceLocator, ClonerManagerInterface::class); |
68
|
|
|
Assertion::isInstanceOf($options, Options\ClonerOptions::class); |
69
|
|
|
|
70
|
|
|
new $requestedName( |
71
|
|
|
$serviceLocator, |
72
|
|
|
$options |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ServiceLocatorInterface $serviceManager |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
81
|
|
|
* @throws \Assert\AssertionFailedException |
82
|
|
|
*/ |
83
|
|
|
private function getClonersConfig(ServiceLocatorInterface $serviceManager) |
84
|
|
|
{ |
85
|
|
|
/** @var ModuleOptionsPluginManagerInterface $moduleOptionsManager */ |
86
|
|
|
$moduleOptionsManager = $serviceManager->get(ModuleOptionsPluginManagerInterface::class); |
87
|
|
|
Assertion::isInstanceOf($moduleOptionsManager, ModuleOptionsPluginManagerInterface::class); |
88
|
|
|
|
89
|
|
|
/** @var ModuleOptions $moduleOptions */ |
90
|
|
|
$moduleOptions = $moduleOptionsManager->get(ModuleOptions::class); |
91
|
|
|
Assertion::isInstanceOf($moduleOptions, ModuleOptions::class); |
92
|
|
|
|
93
|
|
|
return $moduleOptions->getCloners(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
} |