/
TwillBlocks.php
160 lines (142 loc) · 5.05 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
<?php
namespace A17\Twill;
use A17\Twill\Services\Blocks\Block;
use A17\Twill\Services\Blocks\BlockCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\File;
class TwillBlocks
{
public const DIRECTORY_TYPE_VENDOR = 'vendor';
/**
* @var array<string, string>
*/
public static $blockDirectories = [];
/**
* @var array<string, string>
*/
public static $repeatersDirectories = [];
/**
* @return A17\Twill\Services\Blocks\BlockCollection
*/
private $blockCollection;
/**
* 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, self::DIRECTORY_TYPE_VENDOR, Block::TYPE_BLOCK)
);
} else {
self::$blockDirectories[$path] = [
'type' => self::DIRECTORY_TYPE_VENDOR,
'renderNamespace' => $renderNamespace,
];
}
}
}
/**
* 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, self::DIRECTORY_TYPE_VENDOR, Block::TYPE_REPEATER)
);
} else {
self::$repeatersDirectories[$path] = [
'type' => self::DIRECTORY_TYPE_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['type'], 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['type'], Block::TYPE_BLOCK, $data['renderNamespace']) as $block) {
$this->blockCollection->add($block);
}
unset(self::$blockDirectories[$blockDir]);
}
return $this->blockCollection;
}
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
{
return $this->getRepeaters()->first(function (Block $block) use ($name) {
return $block->name === $name;
});
}
/**
* Gets all blocks and repeaters.
*
* @return Collection|Block[]
*/
public function getAll(): Collection
{
return $this->getBlocks()->merge($this->getRepeaters());
}
/**
* @return Collection|Block[]
*/
public function getBlocks(): Collection
{
return $this->getBlockCollection()->getBlockList();
}
/**
* @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);
});
}
}