|
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
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class EntryNameResolver |
|
16
|
|
|
* |
|
17
|
|
|
* @package Nnx\EntryNameResolver\EntryNameResolver |
|
18
|
|
|
*/ |
|
19
|
|
|
class EntryNameResolverChainFactory implements FactoryInterface, MutableCreationOptionsInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use MutableCreationOptionsTrait; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @inheritdoc |
|
25
|
|
|
* |
|
26
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
27
|
|
|
* |
|
28
|
|
|
* @return EntryNameResolverChain |
|
29
|
|
|
* |
|
30
|
|
|
* @throws Exception\RuntimeException |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotFoundException |
|
33
|
|
|
* @throws \Zend\ServiceManager\Exception\ServiceNotCreatedException |
|
34
|
|
|
* @throws \Zend\ServiceManager\Exception\RuntimeException |
|
35
|
|
|
*/ |
|
36
|
|
|
public function createService(ServiceLocatorInterface $serviceLocator) |
|
37
|
|
|
{ |
|
38
|
|
|
/** @var AbstractPluginManager $serviceLocator */ |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
$creationOptions = $this->getCreationOptions(); |
|
42
|
|
|
$options = is_array($creationOptions) ? $creationOptions : []; |
|
43
|
|
|
|
|
44
|
|
|
$resolvers = array_key_exists('resolvers', $options) && is_array($options['resolvers']) ? $options['resolvers'] : []; |
|
45
|
|
|
|
|
46
|
|
|
$chain = new EntryNameResolverChain(); |
|
47
|
|
|
foreach ($resolvers as $entryNameResolverConfig) { |
|
48
|
|
|
if (!is_array($entryNameResolverConfig)) { |
|
49
|
|
|
$errMsg = 'Entry name resolver config is not array'; |
|
50
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (!array_key_exists('name', $entryNameResolverConfig)) { |
|
54
|
|
|
$errMsg = 'Resolver entry name not found'; |
|
55
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
$name = $entryNameResolverConfig['name']; |
|
59
|
|
|
|
|
60
|
|
|
$resolverOptions = array_key_exists('options', $entryNameResolverConfig) ? $entryNameResolverConfig['options'] : []; |
|
61
|
|
|
|
|
62
|
|
|
if (!is_array($resolverOptions)) { |
|
63
|
|
|
$errMsg = 'Resolver options is not array'; |
|
64
|
|
|
throw new Exception\RuntimeException($errMsg); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** @var EntryNameResolverInterface $resolver */ |
|
68
|
|
|
$resolver = $serviceLocator->get($name, $resolverOptions); |
|
69
|
|
|
|
|
70
|
|
|
$priority = array_key_exists('priority', $entryNameResolverConfig) ? (integer)$entryNameResolverConfig['priority'] : EntryNameResolverChain::DEFAULT_PRIORITY; |
|
71
|
|
|
|
|
72
|
|
|
$chain->attach($resolver, $priority); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $chain; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|