Skip to content

Commit 34d19a1

Browse files
committed
Inline component props steps
1 parent 45e8814 commit 34d19a1

File tree

3 files changed

+58
-34
lines changed

3 files changed

+58
-34
lines changed

src/Illuminate/Collections/Arr.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ class Arr
1212
{
1313
use Macroable;
1414

15+
public static function extractPropNames($keys)
16+
{
17+
$props = [];
18+
19+
foreach ($keys as $key => $defaultValue) {
20+
$key = is_numeric($key) ? $defaultValue : $key;
21+
22+
$props[] = $key;
23+
$props[] = \Illuminate\Support\Str::kebab($key);
24+
}
25+
26+
return $props;
27+
}
28+
1529
/**
1630
* Determine whether the given value is array accessible.
1731
*

src/Illuminate/View/Compilers/Concerns/CompilesComponents.php

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function compileClassComponentOpening(string $component, string $a
6868
return implode("\n", [
6969
'<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>',
7070
'<?php if (isset($attributes)) { $__attributesOriginal'.$hash.' = $attributes; } ?>',
71-
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>',
71+
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>',
7272
'<?php $component->withName('.$alias.'); ?>',
7373
'<?php if ($component->shouldRender()): ?>',
7474
'<?php $__env->startComponent($component->resolveView(), $component->data()); ?>',
@@ -157,19 +157,41 @@ protected function compileEndComponentFirst()
157157
*/
158158
protected function compileProps($expression)
159159
{
160-
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?>
161-
<?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) {
162-
\$\$__key = \$\$__key ?? \$__value;
163-
} ?>
164-
<?php \$attributes = \$attributes->exceptProps{$expression}; ?>
165-
<?php foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
160+
return "<?php
161+
\$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
162+
163+
\$__newAttributes = [];
164+
165+
\$__propNames = \Illuminate\Support\Arr::extractPropNames([
166+
'class',
167+
'name',
168+
'id' => -1,
169+
]);
170+
171+
foreach (\$attributes->all() as \$__key => \$__value) {
172+
if (in_array(\$__key, \$__propNames)) {
173+
\$\$__key = \$__key ?? \$__value;
174+
} else {
175+
\$__newAttributes[\$__key] = \$__value;
176+
}
177+
}
178+
179+
\$attributes = new \Illuminate\View\ComponentAttributeBag(\$__newAttributes);
180+
181+
unset(\$__propNames);
182+
unset(\$__newAttributes);
183+
184+
foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
166185
\$\$__key = \$\$__key ?? \$__value;
167-
} ?>
168-
<?php \$__defined_vars = get_defined_vars(); ?>
169-
<?php foreach (\$attributes as \$__key => \$__value) {
186+
}
187+
188+
\$__defined_vars = get_defined_vars();
189+
190+
foreach (\$attributes as \$__key => \$__value) {
170191
if (array_key_exists(\$__key, \$__defined_vars)) unset(\$\$__key);
171-
} ?>
172-
<?php unset(\$__defined_vars); ?>";
192+
}
193+
194+
unset(\$__defined_vars); ?>";
173195
}
174196

175197
/**

src/Illuminate/View/ComponentAttributeBag.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSeria
2626
*/
2727
protected $attributes = [];
2828

29+
/**
30+
* Get all values to save time converting from iterator to array.
31+
*/
32+
public function all(): array
33+
{
34+
return $this->attributes;
35+
}
36+
2937
/**
3038
* Create a new component attribute bag instance.
3139
*
@@ -207,7 +215,7 @@ public function thatStartWith($needles)
207215
*/
208216
public function onlyProps($keys)
209217
{
210-
return $this->only($this->extractPropNames($keys));
218+
return $this->only(Arr::extractPropNames($keys));
211219
}
212220

213221
/**
@@ -218,27 +226,7 @@ public function onlyProps($keys)
218226
*/
219227
public function exceptProps($keys)
220228
{
221-
return $this->except($this->extractPropNames($keys));
222-
}
223-
224-
/**
225-
* Extract prop names from given keys.
226-
*
227-
* @param mixed|array $keys
228-
* @return array
229-
*/
230-
protected function extractPropNames($keys)
231-
{
232-
$props = [];
233-
234-
foreach ($keys as $key => $defaultValue) {
235-
$key = is_numeric($key) ? $defaultValue : $key;
236-
237-
$props[] = $key;
238-
$props[] = Str::kebab($key);
239-
}
240-
241-
return $props;
229+
return $this->except(Arr::extractPropNames($keys));
242230
}
243231

244232
/**

0 commit comments

Comments
 (0)