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