Skip to content

Commit

Permalink
Inline component props steps
Browse files Browse the repository at this point in the history
  • Loading branch information
assertchris committed Apr 19, 2024
1 parent 45e8814 commit 7d18bf4
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 34 deletions.
17 changes: 17 additions & 0 deletions src/Illuminate/Collections/Arr.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,23 @@ class Arr
{
use Macroable;

/**
* Extract prop names from given keys.
*/
public static function extractPropNames(array $keys): array
{
$props = [];

foreach ($keys as $key => $defaultValue) {
$key = is_numeric($key) ? $defaultValue : $key;

$props[] = $key;
$props[] = Str::kebab($key);
}

return $props;
}

/**
* Determine whether the given value is array accessible.
*
Expand Down
42 changes: 30 additions & 12 deletions src/Illuminate/View/Compilers/Concerns/CompilesComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function compileClassComponentOpening(string $component, string $a
return implode("\n", [
'<?php if (isset($component)) { $__componentOriginal'.$hash.' = $component; } ?>',
'<?php if (isset($attributes)) { $__attributesOriginal'.$hash.' = $attributes; } ?>',
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? (array) $attributes->getIterator() : [])); ?>',
'<?php $component = '.$component.'::resolve('.($data ?: '[]').' + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>',
'<?php $component->withName('.$alias.'); ?>',
'<?php if ($component->shouldRender()): ?>',
'<?php $__env->startComponent($component->resolveView(), $component->data()); ?>',
Expand Down Expand Up @@ -157,19 +157,37 @@ protected function compileEndComponentFirst()
*/
protected function compileProps($expression)
{
return "<?php \$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag; ?>
<?php foreach(\$attributes->onlyProps{$expression} as \$__key => \$__value) {
\$\$__key = \$\$__key ?? \$__value;
} ?>
<?php \$attributes = \$attributes->exceptProps{$expression}; ?>
<?php foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
return "<?php
\$attributes ??= new \\Illuminate\\View\\ComponentAttributeBag;
\$__newAttributes = [];
\$__propNames = \Illuminate\Support\Arr::extractPropNames({$expression});
foreach (\$attributes->all() as \$__key => \$__value) {
if (in_array(\$__key, \$__propNames)) {
\$\$__key = \$\$__key ?? \$__value;
} else {
\$__newAttributes[\$__key] = \$__value;
}
}
\$attributes = new \Illuminate\View\ComponentAttributeBag(\$__newAttributes);
unset(\$__propNames);
unset(\$__newAttributes);
foreach (array_filter({$expression}, 'is_string', ARRAY_FILTER_USE_KEY) as \$__key => \$__value) {
\$\$__key = \$\$__key ?? \$__value;
} ?>
<?php \$__defined_vars = get_defined_vars(); ?>
<?php foreach (\$attributes as \$__key => \$__value) {
}
\$__defined_vars = get_defined_vars();
foreach (\$attributes->all() as \$__key => \$__value) {
if (array_key_exists(\$__key, \$__defined_vars)) unset(\$\$__key);
} ?>
<?php unset(\$__defined_vars); ?>";
}
unset(\$__defined_vars); ?>";
}

/**
Expand Down
32 changes: 10 additions & 22 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ class ComponentAttributeBag implements ArrayAccess, IteratorAggregate, JsonSeria
*/
protected $attributes = [];

/**
* Get all values to save time converting from iterator to array.
*/
public function all(): array
{
return $this->attributes;
}

/**
* Create a new component attribute bag instance.
*
Expand Down Expand Up @@ -207,7 +215,7 @@ public function thatStartWith($needles)
*/
public function onlyProps($keys)
{
return $this->only($this->extractPropNames($keys));
return $this->only(Arr::extractPropNames($keys));
}

/**
Expand All @@ -218,27 +226,7 @@ public function onlyProps($keys)
*/
public function exceptProps($keys)
{
return $this->except($this->extractPropNames($keys));
}

/**
* Extract prop names from given keys.
*
* @param mixed|array $keys
* @return array
*/
protected function extractPropNames($keys)
{
$props = [];

foreach ($keys as $key => $defaultValue) {
$key = is_numeric($key) ? $defaultValue : $key;

$props[] = $key;
$props[] = Str::kebab($key);
}

return $props;
return $this->except(Arr::extractPropNames($keys));
}

/**
Expand Down

0 comments on commit 7d18bf4

Please sign in to comment.