1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Services\Blocks; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
class Block |
9
|
|
|
{ |
10
|
|
|
const SOURCE_APP = 'app'; |
11
|
|
|
|
12
|
|
|
const SOURCE_TWILL = 'twill'; |
13
|
|
|
|
14
|
|
|
const SOURCE_CUSTOM = 'custom'; |
15
|
|
|
|
16
|
|
|
const TYPE_BLOCK = 'block'; |
17
|
|
|
|
18
|
|
|
const TYPE_REPEATER = 'repeater'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
public $title; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
public $trigger; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $source; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $name; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
public $group; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string |
47
|
|
|
*/ |
48
|
|
|
public $type; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
public $icon; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var boolean |
57
|
|
|
*/ |
58
|
|
|
public $compiled; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var string |
62
|
|
|
*/ |
63
|
|
|
public $component; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var integer |
67
|
|
|
*/ |
68
|
|
|
public $max = 999; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @var boolean |
72
|
|
|
*/ |
73
|
|
|
public $isNewFormat; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @var \Symfony\Component\Finder\SplFileInfo |
77
|
|
|
*/ |
78
|
|
|
public $file; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
public $fileName; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
public $contents; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Block constructor. |
92
|
|
|
* @param $file |
93
|
|
|
* @param $type |
94
|
|
|
* @param $source |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
69 |
|
public function __construct($file, $type, $source) |
98
|
|
|
{ |
99
|
69 |
|
$this->file = $file; |
100
|
|
|
|
101
|
69 |
|
$this->type = $type; |
102
|
|
|
|
103
|
69 |
|
$this->source = $source; |
104
|
|
|
|
105
|
69 |
|
$this->fileName = $this->getFilename(); |
106
|
|
|
|
107
|
69 |
|
$this->name = $name = Str::before( |
|
|
|
|
108
|
69 |
|
$this->file->getFilename(), |
109
|
69 |
|
'.blade.php' |
110
|
|
|
); |
111
|
|
|
|
112
|
69 |
|
if ($type === self::TYPE_BLOCK |
113
|
69 |
|
&& config('twill.block_editor.repeaters.' . $this->name) !== null |
114
|
|
|
) { |
115
|
|
|
$this->type = self::TYPE_REPEATER; |
116
|
|
|
} |
117
|
|
|
|
118
|
69 |
|
$this->parse(); |
119
|
69 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $source |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
69 |
|
public function setSource($source) |
126
|
|
|
{ |
127
|
69 |
|
$this->source = $source; |
128
|
|
|
|
129
|
69 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return \Illuminate\Support\Collection |
134
|
|
|
*/ |
135
|
11 |
|
public function toList() |
136
|
|
|
{ |
137
|
11 |
|
return collect([ |
138
|
11 |
|
'title' => $this->title, |
139
|
11 |
|
'trigger' => $this->trigger, |
140
|
11 |
|
'name' => $this->name, |
141
|
11 |
|
'group' => $this->group, |
142
|
11 |
|
'type' => $this->type, |
143
|
11 |
|
'icon' => $this->icon, |
144
|
11 |
|
'compiled' => $this->compiled ? 'yes' : '-', |
145
|
11 |
|
'source' => $this->source, |
146
|
11 |
|
'new_format' => $this->isNewFormat ? 'yes' : '-', |
147
|
11 |
|
'file' => $this->file->getFilename(), |
148
|
11 |
|
'component' => $this->component, |
149
|
11 |
|
'max' => $this->type === self::TYPE_REPEATER ? $this->max : null, |
150
|
|
|
]); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @return \Illuminate\Support\Collection |
155
|
|
|
*/ |
156
|
|
|
public function toShortList() |
157
|
|
|
{ |
158
|
|
|
return collect([ |
159
|
|
|
'title' => $this->title, |
160
|
|
|
'name' => $this->name, |
161
|
|
|
'group' => $this->group, |
162
|
|
|
'type' => $this->type, |
163
|
|
|
'icon' => $this->icon, |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param $name |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
3 |
|
public function makeName($name) |
172
|
|
|
{ |
173
|
3 |
|
return Str::kebab($name); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return $this |
178
|
|
|
* @throws \Exception |
179
|
|
|
*/ |
180
|
69 |
|
public function parse() |
181
|
|
|
{ |
182
|
69 |
|
$contents = file_get_contents((string) $this->file->getPathName()); |
183
|
|
|
|
184
|
69 |
|
$this->title = $this->parseProperty('title', $contents, $this->name); |
|
|
|
|
185
|
69 |
|
$this->trigger = $this->parseProperty('trigger', $contents, $this->name); |
|
|
|
|
186
|
69 |
|
$this->max = (int) $this->parseProperty('max', $contents, $this->name, 999); |
187
|
69 |
|
$this->group = $this->parseProperty('group', $contents, $this->name, 'app'); |
|
|
|
|
188
|
69 |
|
$this->icon = $this->parseProperty('icon', $contents, $this->name); |
|
|
|
|
189
|
69 |
|
$this->compiled = (boolean) $this->parseProperty('compiled', $contents, $this->name, false); |
190
|
69 |
|
$this->component = $this->parseProperty('component', $contents, $this->name, "a17-block-{$this->name}"); |
|
|
|
|
191
|
69 |
|
$this->isNewFormat = $this->isNewFormat($contents); |
192
|
69 |
|
$this->contents = $contents; |
193
|
|
|
|
194
|
69 |
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @param $property |
199
|
|
|
* @param $block |
200
|
|
|
* @param $blockName |
201
|
|
|
* @param null $default |
|
|
|
|
202
|
|
|
* @return array |
203
|
|
|
* @throws \Exception |
204
|
|
|
*/ |
205
|
69 |
|
public function parseProperty( |
206
|
|
|
$property, |
207
|
|
|
$block, |
208
|
|
|
$blockName, |
209
|
|
|
$default = null |
210
|
|
|
) { |
211
|
69 |
|
$bladeProperty = ucfirst($property); |
212
|
|
|
|
213
|
69 |
|
foreach (['twillProp', 'twillBlock', 'twillRepeater'] as $pattern) { |
214
|
69 |
|
preg_match("/@{$pattern}{$bladeProperty}\('(.*)'\)/", $block, $matches); |
215
|
|
|
|
216
|
69 |
|
if (filled($matches)) { |
217
|
69 |
|
return $matches[1]; |
218
|
|
|
} |
219
|
|
|
}; |
220
|
|
|
|
221
|
|
|
if ( |
222
|
69 |
|
$value = config( |
223
|
69 |
|
"twill.block_editor.blocks.{$blockName}.{$property}" |
224
|
|
|
) |
225
|
|
|
) { |
226
|
|
|
return $value; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if ( |
230
|
69 |
|
$value = config( |
231
|
69 |
|
"twill.block_editor.repeaters.{$blockName}.{$property}" |
232
|
|
|
) |
233
|
|
|
) { |
234
|
|
|
return $value; |
235
|
|
|
} |
236
|
|
|
|
237
|
69 |
|
if ($property !== 'title') { |
238
|
69 |
|
return $default; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// Title is mandatory |
242
|
|
|
throw new Exception( |
243
|
|
|
"Property '{$property}' not found on block {$blockName}." |
244
|
|
|
); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param $block |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
69 |
|
public function isNewFormat($block) |
252
|
|
|
{ |
253
|
69 |
|
preg_match("/@twill(Prop|Block|Repeater).*\('(.*)'\)/", $block, $propMatches); |
254
|
|
|
|
255
|
69 |
|
return filled($propMatches); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* @return string |
260
|
|
|
*/ |
261
|
69 |
|
public function getFileName() |
262
|
|
|
{ |
263
|
69 |
|
return $this->file->getFileName(); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return string |
268
|
|
|
* @throws \Symfony\Component\Debug\Exception\FatalThrowableError |
269
|
|
|
*/ |
270
|
5 |
|
public function render() |
271
|
|
|
{ |
272
|
5 |
|
return BladeCompiler::render( |
273
|
5 |
|
$this->removeSpecialBladeTags($this->contents), |
274
|
|
|
[ |
275
|
5 |
|
'renderForBlocks' => true, |
276
|
|
|
] |
277
|
|
|
); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @param $contents |
282
|
|
|
* @return string |
283
|
|
|
*/ |
284
|
5 |
|
public function removeSpecialBladeTags($contents) |
285
|
|
|
{ |
286
|
5 |
|
return preg_replace([ |
287
|
5 |
|
"/@twillProp.*\('(.*)'\)/", |
288
|
|
|
"/@twillBlock.*\('(.*)'\)/", |
289
|
|
|
"/@twillRepeater.*\('(.*)'\)/", |
290
|
5 |
|
], '', $contents); |
291
|
|
|
} |
292
|
|
|
} |
293
|
|
|
|