|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Core\View\Helper; |
|
12
|
|
|
|
|
13
|
|
|
use Core\EventManager\EventManager; |
|
14
|
|
|
use Zend\EventManager\EventManagerInterface; |
|
15
|
|
|
use Zend\Stdlib\ArrayUtils; |
|
16
|
|
|
use Zend\View\Exception; |
|
17
|
|
|
use Zend\View\Helper\AbstractHelper; |
|
18
|
|
|
use Zend\View\Helper\Partial; |
|
19
|
|
|
use Zend\View\Model\ModelInterface; |
|
20
|
|
|
use Zend\View\Model\ViewModel; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* ${CARET} |
|
24
|
|
|
* |
|
25
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
26
|
|
|
* @todo write test |
|
27
|
|
|
*/ |
|
28
|
|
|
class Snippet extends AbstractHelper |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* |
|
34
|
|
|
* @var array |
|
35
|
|
|
*/ |
|
36
|
|
|
private $config = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* |
|
40
|
|
|
* |
|
41
|
|
|
* @var EventManager |
|
42
|
|
|
*/ |
|
43
|
|
|
private $events; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* |
|
47
|
|
|
* |
|
48
|
|
|
* @var \Zend\View\Helper\Partial |
|
49
|
|
|
*/ |
|
50
|
|
|
private $partials; |
|
51
|
|
|
|
|
52
|
|
|
public function __construct(Partial $partialHelper, EventManager $eventManager, array $config) |
|
53
|
|
|
{ |
|
54
|
|
|
$this->config = $config; |
|
55
|
|
|
$this->events = $eventManager; |
|
56
|
|
|
$this->partials = $partialHelper; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function __invoke($name, $values = []) |
|
60
|
|
|
{ |
|
61
|
|
|
$snippets = ArrayUtils::merge($this->fromConfig($name), $this->fromEvent($name, $values)); |
|
62
|
|
|
|
|
63
|
|
|
uasort($snippets, function($a, $b) { |
|
64
|
|
|
$prioA = is_array($a) && isset($a['priority']) ? $a['priority'] : 0; |
|
65
|
|
|
$prioB = is_array($b) && isset($b['priority']) ? $b['priority'] : 0; |
|
66
|
|
|
|
|
67
|
|
|
return $prioA == $prioB ? 0 : ($prioA < $prioB ? 1 : -1); |
|
68
|
|
|
}); |
|
69
|
|
|
|
|
70
|
|
|
$content = ''; |
|
71
|
|
|
foreach ($snippets as $snippet) { |
|
72
|
|
|
$content .= $this->process($snippet, $values); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $content; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
private function fromConfig($name) |
|
79
|
|
|
{ |
|
80
|
|
|
$snippets = isset($this->config[$name]) ? $this->config[$name] : []; |
|
81
|
|
|
|
|
82
|
|
|
if (is_string($snippets)) { |
|
83
|
|
|
$snippets = [$snippets]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $snippets; |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
private function fromEvent($name, $values) |
|
91
|
|
|
{ |
|
92
|
|
|
$event = $this->events->getEvent($name, $this, $values); |
|
93
|
|
|
$results = $this->events->triggerEvent($event); |
|
94
|
|
|
|
|
95
|
|
|
$snippets = []; |
|
96
|
|
|
|
|
97
|
|
|
foreach ($results as $result) { |
|
98
|
|
|
if (null !== $result) { |
|
99
|
|
|
$snippets[] = $result; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $snippets; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* |
|
108
|
|
|
* |
|
109
|
|
|
* @param $item |
|
110
|
|
|
* @param $values |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
* @throws \UnexpectedValueException |
|
114
|
|
|
*/ |
|
115
|
|
|
private function process($item, $values) |
|
116
|
|
|
{ |
|
117
|
|
|
if ($item instanceOf ViewModel) { |
|
118
|
|
|
return $this->partials->__invoke($item); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if (is_string($item)) { |
|
122
|
|
|
if (false === strpos($item, ' ')) { |
|
123
|
|
|
return $this->partials->__invoke($item, $values); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
$item = ['content' => $item]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
if (!is_array($values)) { |
|
130
|
|
|
$values = ArrayUtils::iteratorToArray($values); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
if (isset($item['values'])) { |
|
134
|
|
|
$values = ArrayUtils::merge($item['values'], $values); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if (isset($item['content'])) { |
|
138
|
|
|
$content = $item['content']; |
|
139
|
|
|
|
|
140
|
|
|
foreach ($values as $key => $val) { |
|
141
|
|
|
$content = str_replace('%' . $key . '%', $val, $content); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $content; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (isset($item['template'])) { |
|
148
|
|
|
return $this->partials->__invoke($item['template'], $values); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
throw new \UnexpectedValueException('Snippet item must either be a string or an array with at least the keys "content" or "template".'); |
|
152
|
|
|
} |
|
153
|
|
|
} |