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