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