-
Notifications
You must be signed in to change notification settings - Fork 577
/
TwillBlocks.php
335 lines (284 loc) · 10.2 KB
/
TwillBlocks.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
<?php
namespace A17\Twill;
use A17\Twill\Services\Blocks\Block;
use A17\Twill\Services\Blocks\BlockCollection;
use A17\Twill\Services\Forms\InlineRepeater;
use A17\Twill\View\Components\Blocks\TwillBlockComponent;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class TwillBlocks
{
/**
* @var array<string, array>
*/
public static $blockDirectories = [];
/**
* @var array<string, array>
*/
public static $repeatersDirectories = [];
/**
* @var array<string, string>
*/
public static $componentBlockNamespaces = [];
/**
* @var array<string, InlineRepeater>
*/
public static $dynamicRepeaters = [];
/**
* @var array
*/
public static $loadedDynamicRepeaters = [];
/**
* @var array<string, string>
*/
public static $manualBlocks = [];
/**
* @return A17\Twill\Services\Blocks\BlockCollection
*/
private ?BlockCollection $blockCollection = null;
private array $cropConfigs = [];
/**
* Registers a blocks directory.
*
* When the blockCollection is already initialized, we read the blocks and merge them in.
* If the blockCollection is not yet initialized, we add it to the local static so that we
* can process it once the collection is needed.
*/
public function registerPackageBlocksDirectory(string $path, string $renderNamespace = null): void
{
if (! isset(self::$blockDirectories[$path])) {
if (isset($this->blockCollection)) {
$this->getBlockCollection()->merge(
$this->readBlocksFromDirectory($path, Block::SOURCE_VENDOR, Block::TYPE_BLOCK)
);
} else {
self::$blockDirectories[$path] = [
'source' => Block::SOURCE_VENDOR,
'renderNamespace' => $renderNamespace,
];
}
}
}
public function registerDynamicRepeater(string $name, InlineRepeater $repeater): void
{
self::$dynamicRepeaters[$name] = $repeater;
}
public function discoverDynamicRepeaters(Collection $collection): void
{
/** @var Block $item */
foreach ($collection as $item) {
if ($item->componentClass) {
$component = new $item->componentClass();
$component->getForm()->registerDynamicRepeaters();
}
}
}
public function getAvailableRepeaters(): string
{
$baseList = $this->getBlockCollection()->getRepeaters()->mapWithKeys(function (Block $repeater) {
return [$repeater->name => $repeater->toList()];
});
return $baseList->toJson();
}
public function registerComponentBlocks(string $namespace, string $path): void
{
if (! Str::startsWith($namespace, '\\')) {
$namespace = '\\' . $namespace;
}
self::$componentBlockNamespaces[$namespace] = $path;
}
/**
* Registers a repeaters directory.
*
* When the blockCollection is already initialized, we read the repeaters and merge them in.
* If the blockCollection is not yet initialized, we add it to the local static so that we
* can process it once the collection is needed.
*/
public function registerPackageRepeatersDirectory(string $path, string $renderNamespace = null): void
{
if (! isset(self::$repeatersDirectories[$path])) {
if (isset($this->blockCollection)) {
$this->getBlockCollection()->merge(
$this->readBlocksFromDirectory($path, Block::SOURCE_VENDOR, Block::TYPE_REPEATER)
);
} else {
self::$repeatersDirectories[$path] = [
'source' => Block::SOURCE_VENDOR,
'renderNamespace' => $renderNamespace,
];
}
}
}
/**
* Only when the block collection is actually requested we parse all the information.
*/
public function getBlockCollection(): BlockCollection
{
if (! isset($this->blockCollection)) {
$this->blockCollection = new BlockCollection();
}
// Consume the repeatersDirectories. We act a bit dumb here by not taking into account duplicates
// as a package should only register a directory once.
foreach (self::$repeatersDirectories as $repeaterDir => $data) {
foreach (
$this->readBlocksFromDirectory(
$repeaterDir,
$data['source'],
Block::TYPE_REPEATER,
$data['renderNamespace']
) as $repeater
) {
$this->blockCollection->add($repeater);
}
unset(self::$repeatersDirectories[$repeaterDir]);
}
foreach (self::$blockDirectories as $blockDir => $data) {
foreach (
$this->readBlocksFromDirectory(
$blockDir,
$data['source'],
Block::TYPE_BLOCK,
$data['renderNamespace']
) as $block
) {
$this->blockCollection->add($block);
}
unset(self::$blockDirectories[$blockDir]);
}
foreach (self::$componentBlockNamespaces as $namespace => $path) {
if (file_exists($path)) {
$disk = Storage::build([
'driver' => 'local',
'root' => $path,
]);
foreach ($disk->allFiles() as $file) {
$class = $namespace . '\\' . Str::replace('/', '\\', Str::before($file, '.'));
if (is_subclass_of($class, TwillBlockComponent::class)) {
$this->blockCollection->add(
Block::forComponent($class)
);
}
}
}
unset(self::$componentBlockNamespaces[$namespace]);
}
foreach (self::$manualBlocks as $class) {
$this->blockCollection->add(
Block::forComponent($class)
);
unset(self::$manualBlocks[$class]);
}
$this->discoverDynamicRepeaters($this->blockCollection);
foreach (self::$dynamicRepeaters as $name => $dynamicRepeater) {
if (! isset(self::$loadedDynamicRepeaters[$name])) {
$this->blockCollection->add($dynamicRepeater->asBlock());
self::$loadedDynamicRepeaters[$name] = true;
}
}
// remove duplicate Twill blocks
$appBlocks = $this->blockCollection->where('source', '!=', Block::SOURCE_TWILL);
$this->blockCollection = $this->blockCollection->filter(function ($item) use ($appBlocks) {
return ! $appBlocks->contains(function ($block) use ($item) {
return $item->source === Block::SOURCE_TWILL && $item->name === $block->name;
});
});
return $this->blockCollection;
}
public function registerManualBlock(string $blockClass): void
{
self::$manualBlocks[$blockClass] = $blockClass;
}
public function findByName(string $name): ?Block
{
return $this->getAll()->first(function (Block $block) use ($name) {
return $block->name === $name;
});
}
public function findRepeaterByName(string $name): ?Block
{
$repeater = $this->getRepeaters()->first(function (Block $block) use ($name) {
return $block->name === $name;
});
if ($repeater === null) {
// Search for the dynamic one.
$repeater = $this->getRepeaters()->first(function (Block $block) use ($name) {
return $block->name === 'dynamic-repeater-' . $name;
});
}
return $repeater;
}
/**
* Gets all blocks and repeaters.
*
* @return Collection|Block[]
*/
public function getAll(): Collection
{
return $this->getBlocks()->merge($this->getRepeaters())->merge($this->getSettingsBlocks());
}
/**
* @return Collection|Block[]
*/
public function getBlocks(bool $withSettingsBlocks = false): Collection
{
$blocks = $this->getBlockCollection()->getBlockList();
if ($withSettingsBlocks) {
return $blocks->merge($this->getSettingsBlocks());
}
return $blocks;
}
public function getSettingsBlocks(): Collection
{
return $this->getBlockCollection()->getSettingsList();
}
/**
* @return Collection|Block[]
*/
public function getRepeaters(): Collection
{
return $this->getBlockCollection()->getRepeaters();
}
/**
* Gets the collection of Block objects from a given directory.
*/
public function readBlocksFromDirectory(
string $directory,
string $source,
string $type,
?string $renderNamespace = null
): Collection {
if (! File::exists($directory)) {
return new Collection();
}
return collect(File::files($directory))
->map(function ($file) use ($source, $type, $renderNamespace) {
return Block::make($file, $type, $source, null, $renderNamespace);
});
}
/**
* Gets all the crop configs, also those of component blocks.
*/
public function getAllCropConfigs($prefixKey = false): array
{
if (! $this->cropConfigs) {
$this->cropConfigs = config()->get('twill.block_editor.crops');
/** @var Block $block */
foreach ($this->getBlockCollection() as $block) {
if (! $block->componentClass) {
continue;
}
$crops = $block->componentClass::getCrops();
if ($crops !== []) {
$this->cropConfigs = array_merge($this->cropConfigs, $crops);
}
}
}
if ($prefixKey) {
return Arr::prependKeysWith($this->cropConfigs, 'block_');
}
return $this->cropConfigs;
}
}