|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Leonidas\Framework; |
|
4
|
|
|
|
|
5
|
|
|
use Leonidas\Contracts\Extension\WpExtensionInterface; |
|
6
|
|
|
use Leonidas\Framework\Plugin\Plugin; |
|
7
|
|
|
use Leonidas\Framework\Theme\Theme; |
|
8
|
|
|
use Psr\Container\ContainerInterface; |
|
9
|
|
|
|
|
10
|
|
|
class WpExtension implements WpExtensionInterface |
|
11
|
|
|
{ |
|
12
|
|
|
protected string $name; |
|
13
|
|
|
|
|
14
|
|
|
protected string $version; |
|
15
|
|
|
|
|
16
|
|
|
protected string $slug; |
|
17
|
|
|
|
|
18
|
|
|
protected string $namespace; |
|
19
|
|
|
|
|
20
|
|
|
protected string $prefix; |
|
21
|
|
|
|
|
22
|
|
|
protected string $description; |
|
23
|
|
|
|
|
24
|
|
|
protected string $path; |
|
25
|
|
|
|
|
26
|
|
|
protected string $url; |
|
27
|
|
|
|
|
28
|
|
|
protected string $type; |
|
29
|
|
|
|
|
30
|
|
|
protected ContainerInterface $container; |
|
31
|
|
|
|
|
32
|
|
|
protected bool $isInDev; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(string $type, string $path, string $url, ContainerInterface $container) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->type = $type; |
|
37
|
|
|
$this->path = $path; |
|
38
|
|
|
$this->url = $url; |
|
39
|
|
|
$this->container = $container; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getName(): string |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->name ??= $this->config('app.name'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* {@inheritDoc} |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getVersion(): string |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->version ??= $this->config('app.version'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* {@inheritDoc} |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getSlug(): string |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->slug ??= $this->config('app.slug'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getNamespace(): string |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->namespace ??= $this->config('app.namespace'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* {@inheritDoc} |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getPrefix(): string |
|
78
|
|
|
{ |
|
79
|
|
|
return $this->prefix ??= $this->config('app.prefix'); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* {@inheritDoc} |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getDescription(): string |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->description ??= $this->config('app.description'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritDoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPath(): string |
|
94
|
|
|
{ |
|
95
|
|
|
return $this->path; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* {@inheritDoc} |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getUrl(): string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->url; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* {@inheritDoc} |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getType(): string |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->type; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritDoc} |
|
116
|
|
|
*/ |
|
117
|
|
|
public function isInDev(): bool |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->isInDev ??= $this->config('app.dev'); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritDoc} |
|
124
|
|
|
*/ |
|
125
|
|
|
public function get(string $id) |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->container->get($id); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritDoc} |
|
132
|
|
|
*/ |
|
133
|
|
|
public function has(string $id): bool |
|
134
|
|
|
{ |
|
135
|
|
|
return $this->container->has($id); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritDoc} |
|
140
|
|
|
*/ |
|
141
|
|
|
public function header(string $header): ?string |
|
142
|
|
|
{ |
|
143
|
|
|
switch ($this->getType()) { |
|
144
|
|
|
case 'plugin': |
|
145
|
|
|
$headers = Plugin::headers(static::absPath('/plugin.php')); |
|
|
|
|
|
|
146
|
|
|
|
|
147
|
|
|
break; |
|
148
|
|
|
|
|
149
|
|
|
case 'theme': |
|
150
|
|
|
$headers = Theme::headers(static::absPath('/style.css')); |
|
151
|
|
|
|
|
152
|
|
|
break; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
return $headers[$header] ?? null; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* {@inheritDoc} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function config(string $key, $default = null) |
|
162
|
|
|
{ |
|
163
|
|
|
return $this->get('config')->get($key, $default); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* {@inheritDoc} |
|
168
|
|
|
*/ |
|
169
|
|
|
public function hasConfig(string $key): bool |
|
170
|
|
|
{ |
|
171
|
|
|
return $this->get('config')->has($key); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* {@inheritDoc} |
|
176
|
|
|
*/ |
|
177
|
|
|
public function relPath(?string $file = null): ?string |
|
178
|
|
|
{ |
|
179
|
|
|
return null; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* {@inheritDoc} |
|
184
|
|
|
*/ |
|
185
|
|
|
public function absPath(?string $file = null): ?string |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->getPath() . $file; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* {@inheritDoc} |
|
192
|
|
|
*/ |
|
193
|
|
|
public function url(?string $route = null): string |
|
194
|
|
|
{ |
|
195
|
|
|
return $this->getUrl() . $route; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* {@inheritDoc} |
|
200
|
|
|
*/ |
|
201
|
|
|
public function namespace(string $value, string $separator = '/'): string |
|
202
|
|
|
{ |
|
203
|
|
|
return $this->getNamespace() . $separator . $value; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
/** |
|
207
|
|
|
* {@inheritDoc} |
|
208
|
|
|
*/ |
|
209
|
|
|
public function prefix(string $value, string $separator = '_'): string |
|
210
|
|
|
{ |
|
211
|
|
|
return $this->getPrefix() . $separator . $value; |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* {@inheritDoc} |
|
216
|
|
|
*/ |
|
217
|
|
|
public function doAction(string $event, ...$data): void |
|
218
|
|
|
{ |
|
219
|
|
|
do_action($this->namespace($event, '/'), ...$data); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* {@inheritDoc} |
|
224
|
|
|
*/ |
|
225
|
|
|
public function applyFilters(string $attribute, $value, ...$data): void |
|
226
|
|
|
{ |
|
227
|
|
|
apply_filters($this->namespace($attribute, '/'), $value, ...$data); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns a new instance of WpExtension using values in the array passed. |
|
232
|
|
|
* |
|
233
|
|
|
* @param array $args |
|
234
|
|
|
* |
|
235
|
|
|
* @return WpExtension |
|
236
|
|
|
*/ |
|
237
|
|
|
public static function create(array $args): WpExtension |
|
238
|
|
|
{ |
|
239
|
|
|
return new static( |
|
240
|
|
|
$args['type'], |
|
241
|
|
|
$args['path'], |
|
242
|
|
|
$args['url'], |
|
243
|
|
|
$args['container'], |
|
244
|
|
|
); |
|
245
|
|
|
} |
|
246
|
|
|
} |
|
247
|
|
|
|