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 Countable; |
9
|
|
|
use Zend\Stdlib\PriorityQueue; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class EntryNameResolver |
13
|
|
|
* |
14
|
|
|
* @package Nnx\EntryNameResolver\EntryNameResolver |
15
|
|
|
*/ |
16
|
|
|
class EntryNameResolverChain implements Countable, EntryNameResolverInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Приоритет по умолчанию для резолвера, в момент его добавления в цепочку |
20
|
|
|
*/ |
21
|
|
|
const DEFAULT_PRIORITY = 1; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Цепочка резолверов |
25
|
|
|
* |
26
|
|
|
* @var PriorityQueue |
27
|
|
|
*/ |
28
|
|
|
protected $resolvers; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* EntryNameResolver constructor. |
32
|
|
|
*/ |
33
|
|
|
public function __construct() |
34
|
|
|
{ |
35
|
|
|
$this->resolvers = new PriorityQueue(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Возвращает колличество добавленных резолверов |
40
|
|
|
* |
41
|
|
|
* @return int |
42
|
|
|
*/ |
43
|
|
|
public function count() |
44
|
|
|
{ |
45
|
|
|
return count($this->resolvers); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Добавляет резолвер |
51
|
|
|
* |
52
|
|
|
* @param EntryNameResolverInterface $resolver |
53
|
|
|
* @param int $priority |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function attach(EntryNameResolverInterface $resolver, $priority = self::DEFAULT_PRIORITY) |
58
|
|
|
{ |
59
|
|
|
$this->resolvers->insert( |
60
|
|
|
[ |
61
|
|
|
'instance' => $resolver, |
62
|
|
|
|
63
|
|
|
], |
64
|
|
|
$priority |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Добавляет резолвер в начало цепочки |
73
|
|
|
* |
74
|
|
|
* @param EntryNameResolverInterface $resolver |
75
|
|
|
* |
76
|
|
|
* @return $this |
77
|
|
|
* |
78
|
|
|
*/ |
79
|
|
|
public function prependResolver(EntryNameResolverInterface $resolver) |
80
|
|
|
{ |
81
|
|
|
$priority = self::DEFAULT_PRIORITY; |
82
|
|
|
|
83
|
|
|
if (!$this->resolvers->isEmpty()) { |
84
|
|
|
$queue = $this->resolvers->getIterator(); |
85
|
|
|
$queue->setExtractFlags(PriorityQueue::EXTR_PRIORITY); |
86
|
|
|
$extractedNode = $queue->extract(); |
87
|
|
|
$priority = $extractedNode[0] + 1; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->resolvers->insert( |
91
|
|
|
[ |
92
|
|
|
'instance' => $resolver, |
93
|
|
|
|
94
|
|
|
], |
95
|
|
|
$priority |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
* |
104
|
|
|
* @param $entryName |
105
|
|
|
* @param null $context |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
|
|
public function resolveEntryNameByContext($entryName, $context = null) |
110
|
|
|
{ |
111
|
|
|
$resolvers = clone $this->resolvers; |
112
|
|
|
|
113
|
|
|
$result = null; |
114
|
|
|
foreach ($resolvers as $resolverItem) { |
115
|
|
|
/** @var EntryNameResolverInterface $resolver */ |
116
|
|
|
$resolver = $resolverItem['instance']; |
117
|
|
|
$result = $resolver->resolveEntryNameByContext($entryName, $context); |
118
|
|
|
if (null !== $result) { |
119
|
|
|
break; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $result; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|