1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/nnx-framework/entry-name-resolver |
4
|
|
|
* @author Malofeykin Andrey <[email protected]> |
5
|
|
|
*/ |
6
|
|
|
namespace Nnx\EntryNameResolver; |
7
|
|
|
|
8
|
|
|
use Zend\ServiceManager\AbstractPluginManager; |
9
|
|
|
use Zend\ServiceManager\FactoryInterface; |
10
|
|
|
use Zend\ServiceManager\MutableCreationOptionsInterface; |
11
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
12
|
|
|
use Zend\ServiceManager\MutableCreationOptionsTrait; |
13
|
|
|
use ReflectionClass; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class EntryNameResolver |
17
|
|
|
* |
18
|
|
|
* @package Nnx\EntryNameResolver\EntryNameResolver |
19
|
|
|
*/ |
20
|
|
|
class EntryNameResolverChainFactory implements FactoryInterface, MutableCreationOptionsInterface |
21
|
|
|
{ |
22
|
|
|
use MutableCreationOptionsTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Имя создаваемого класса |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected static $defaultTargetClassName = EntryNameResolverChain::class; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @inheritdoc |
33
|
|
|
* |
34
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
35
|
|
|
* |
36
|
|
|
* @return EntryNameResolverChain |
37
|
|
|
* |
38
|
|
|
* @throws Exception\RuntimeException |
39
|
|
|
* |
40
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
41
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException |
42
|
|
|
* @throws \Zend\ServiceManager\Exception\RuntimeException |
43
|
|
|
*/ |
44
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
45
|
|
|
{ |
46
|
|
|
/** @var AbstractPluginManager $serviceLocator */ |
47
|
|
|
|
48
|
|
|
$creationOptions = $this->getCreationOptions(); |
49
|
|
|
$options = is_array($creationOptions) ? $creationOptions : []; |
50
|
|
|
|
51
|
|
|
$resolvers = array_key_exists('resolvers', $options) && is_array($options['resolvers']) ? $options['resolvers'] : []; |
52
|
|
|
|
53
|
|
|
$className = array_key_exists('className', $options) ? (string)$options['className'] : static::$defaultTargetClassName; |
54
|
|
|
|
55
|
|
|
$r = new ReflectionClass($className); |
56
|
|
|
$chain = $r->newInstance(); |
57
|
|
|
|
58
|
|
View Code Duplication |
if (!$chain instanceof static::$defaultTargetClassName) { |
|
|
|
|
59
|
|
|
$errMsg = sprintf('EntryNameResolverChain not implements: %s', static::$defaultTargetClassName); |
60
|
|
|
throw new Exception\RuntimeException($errMsg); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($resolvers as $entryNameResolverConfig) { |
64
|
|
|
if (!is_array($entryNameResolverConfig)) { |
65
|
|
|
$errMsg = 'Entry name resolver config is not array'; |
66
|
|
|
throw new Exception\RuntimeException($errMsg); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if (!array_key_exists('name', $entryNameResolverConfig)) { |
70
|
|
|
$errMsg = 'Resolver entry name not found'; |
71
|
|
|
throw new Exception\RuntimeException($errMsg); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$name = $entryNameResolverConfig['name']; |
75
|
|
|
|
76
|
|
|
$resolverOptions = array_key_exists('options', $entryNameResolverConfig) ? $entryNameResolverConfig['options'] : []; |
77
|
|
|
|
78
|
|
|
if (!is_array($resolverOptions)) { |
79
|
|
|
$errMsg = 'Resolver options is not array'; |
80
|
|
|
throw new Exception\RuntimeException($errMsg); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @var EntryNameResolverInterface $resolver */ |
84
|
|
|
$resolver = $serviceLocator->get($name, $resolverOptions); |
85
|
|
|
|
86
|
|
|
$priority = array_key_exists('priority', $entryNameResolverConfig) ? (integer)$entryNameResolverConfig['priority'] : EntryNameResolverChain::DEFAULT_PRIORITY; |
87
|
|
|
|
88
|
|
|
$chain->attach($resolver, $priority); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $chain; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
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.