1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Maiorano\Shortcodes\Manager; |
4
|
|
|
|
5
|
|
|
use Maiorano\Shortcodes\Contracts\AliasInterface; |
6
|
|
|
use Maiorano\Shortcodes\Contracts\AttributeInterface; |
7
|
|
|
use Maiorano\Shortcodes\Contracts\ContainerAwareInterface; |
8
|
|
|
use Maiorano\Shortcodes\Contracts\ShortcodeInterface; |
9
|
|
|
use Maiorano\Shortcodes\Exceptions\DeregisterException; |
10
|
|
|
use Maiorano\Shortcodes\Exceptions\RegisterException; |
11
|
|
|
use Maiorano\Shortcodes\Parsers\DefaultParser; |
12
|
|
|
use Maiorano\Shortcodes\Parsers\ParserInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ShortcodeManager. |
16
|
|
|
*/ |
17
|
|
|
final class ShortcodeManager extends BaseManager |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ParserInterface |
21
|
|
|
*/ |
22
|
|
|
protected $parser; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ShortcodeManager constructor. |
26
|
|
|
* |
27
|
|
|
* @param ShortcodeInterface[] $shortcodes |
28
|
|
|
* @param ParserInterface|null $parser |
29
|
|
|
* |
30
|
|
|
* @throws RegisterException |
31
|
|
|
*/ |
32
|
23 |
|
public function __construct(array $shortcodes = [], ParserInterface $parser = null) |
33
|
|
|
{ |
34
|
23 |
|
$this->parser = $parser ?? new DefaultParser(); |
35
|
23 |
|
$this->registerAll($shortcodes); |
36
|
23 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param ShortcodeInterface $shortcode |
40
|
|
|
* @param string|null $name |
41
|
|
|
* |
42
|
|
|
* @throws RegisterException |
43
|
|
|
* |
44
|
|
|
* @return ManagerInterface |
45
|
|
|
*/ |
46
|
19 |
|
public function register(ShortcodeInterface $shortcode, ?string $name = null): ManagerInterface |
47
|
|
|
{ |
48
|
19 |
|
parent::register($shortcode, $name); |
49
|
18 |
|
if ($shortcode instanceof AliasInterface) { |
50
|
6 |
|
foreach ($shortcode->getAlias() as $alias) { |
51
|
5 |
|
if (!$this->isRegistered($alias)) { |
52
|
5 |
|
parent::register($shortcode, $alias); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
18 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param ShortcodeInterface[] $shortcodes |
62
|
|
|
* |
63
|
|
|
* @throws RegisterException |
64
|
|
|
* |
65
|
|
|
* @return ManagerInterface |
66
|
|
|
*/ |
67
|
23 |
|
public function registerAll(array $shortcodes): ManagerInterface |
68
|
|
|
{ |
69
|
23 |
|
foreach ($shortcodes as $k => $s) { |
70
|
3 |
|
$this->register($s, $k); |
71
|
|
|
} |
72
|
|
|
|
73
|
23 |
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param string $name |
78
|
|
|
* @param bool $includeAlias |
79
|
|
|
* |
80
|
|
|
* @throws DeregisterException |
81
|
|
|
* |
82
|
|
|
* @return ManagerInterface |
83
|
|
|
*/ |
84
|
6 |
|
public function deregister(string $name, bool $includeAlias = true): ManagerInterface |
85
|
|
|
{ |
86
|
6 |
|
$shortcode = $this->shortcodes[$name] ?? false; |
87
|
6 |
|
if ($shortcode && $shortcode instanceof AliasInterface) { |
88
|
4 |
|
if ($name === $shortcode->getName() && $includeAlias) { |
89
|
2 |
|
foreach ($shortcode->getAlias() as $alias) { |
90
|
2 |
|
parent::deregister($alias); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
6 |
|
return parent::deregister($name); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param string $name |
100
|
|
|
* @param string $alias |
101
|
|
|
* |
102
|
|
|
* @throws RegisterException |
103
|
|
|
* |
104
|
|
|
* @return ManagerInterface |
105
|
|
|
*/ |
106
|
3 |
|
public function alias(string $name, string $alias): ManagerInterface |
107
|
|
|
{ |
108
|
3 |
|
if (!$this->isRegistered($name)) { |
109
|
1 |
|
throw RegisterException::missing($name); |
110
|
|
|
} |
111
|
2 |
|
if ($this[$name] instanceof AliasInterface) { |
112
|
1 |
|
$this[$name]->alias($alias); |
|
|
|
|
113
|
|
|
} else { |
114
|
1 |
|
throw RegisterException::noAlias(); |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
if (!$this[$name] instanceof ContainerAwareInterface) { |
118
|
1 |
|
parent::register($this[$name], $alias); |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $content |
126
|
|
|
* @param string|string[] $tags |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
2 |
|
public function hasShortcode(string $content, $tags = []): bool |
131
|
|
|
{ |
132
|
2 |
|
$tags = $this->preProcessTags($tags); |
133
|
2 |
|
$matches = $this->parser->parseShortcode($content, $tags); |
134
|
|
|
|
135
|
2 |
|
return !empty($matches); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $content |
140
|
|
|
* @param string|string[] $tags |
141
|
|
|
* @param bool $deep |
142
|
|
|
* |
143
|
|
|
* @return string |
144
|
|
|
*/ |
145
|
3 |
|
public function doShortcode(string $content, $tags = [], bool $deep = false): string |
146
|
|
|
{ |
147
|
3 |
|
$tags = $this->preProcessTags($tags); |
148
|
|
|
$handler = function (string $tag, ?string $content = null, array $atts = []) { |
149
|
2 |
|
$shortcode = $this[$tag]; |
150
|
|
|
|
151
|
2 |
|
if ($shortcode instanceof AttributeInterface) { |
152
|
1 |
|
$atts = array_merge($shortcode->getAttributes(), $atts); |
153
|
|
|
|
154
|
1 |
|
return $shortcode->handle($content, $atts); |
155
|
|
|
} |
156
|
|
|
|
157
|
1 |
|
return $shortcode->handle($content); |
158
|
3 |
|
}; |
159
|
|
|
|
160
|
3 |
|
$result = (string) $this->parser->parseShortcode($content, $tags, $handler); |
161
|
|
|
|
162
|
3 |
|
if ($deep && $this->hasShortcode($result, $tags)) { |
163
|
1 |
|
return $this->doShortcode($result, $tags, $deep); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
return $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param string|string[] $tags |
171
|
|
|
* |
172
|
|
|
* @return string[] |
173
|
|
|
*/ |
174
|
4 |
|
private function preProcessTags($tags): array |
175
|
|
|
{ |
176
|
4 |
|
if (!$tags) { |
177
|
4 |
|
return $this->getRegistered(); |
178
|
|
|
} |
179
|
|
|
|
180
|
2 |
|
if (is_string($tags)) { |
181
|
1 |
|
$tags = explode('|', $tags); |
182
|
|
|
} |
183
|
|
|
|
184
|
2 |
|
return array_filter($tags, [$this, 'isRegistered']); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|