From d283977f267143983806b67a52d6b65d616eff1a Mon Sep 17 00:00:00 2001 From: jim Date: Thu, 17 Apr 2025 14:24:29 +1200 Subject: [PATCH 1/2] Add wrapping attribute and update DataProvider method to use it --- src/DataProvider.php | 36 +++++++++++++------- src/WrappingAttributes/InertiaMerge.php | 16 +++++++++ src/WrappingAttributes/WrappingAttribute.php | 14 ++++++++ 3 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 src/WrappingAttributes/InertiaMerge.php create mode 100644 src/WrappingAttributes/WrappingAttribute.php diff --git a/src/DataProvider.php b/src/DataProvider.php index 47db17f..e32d7d4 100644 --- a/src/DataProvider.php +++ b/src/DataProvider.php @@ -5,16 +5,17 @@ namespace Webfox\InertiaDataProviders; use Closure; -use Illuminate\Contracts\Support\Arrayable; -use Illuminate\Contracts\Support\Jsonable; -use Inertia\DeferProp; +use ReflectionClass; use Inertia\LazyProp; use Inertia\Response; -use ReflectionClass; use ReflectionMethod; -use ReflectionNamedType; +use Inertia\DeferProp; use ReflectionProperty; +use ReflectionNamedType; +use Illuminate\Contracts\Support\Jsonable; use Symfony\Component\VarDumper\VarDumper; +use Illuminate\Contracts\Support\Arrayable; +use Webfox\InertiaDataProviders\WrappingAttributes\WrappingAttribute; use Webfox\InertiaDataProviders\AttributeNameFormatters\AttributeNameFormatter; abstract class DataProvider implements Arrayable, Jsonable @@ -30,30 +31,41 @@ public static function collection(DataProvider|array ...$dataProviders): DataPro public function toArray(): array { - $staticData = $this->staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData; + $staticData = $this->staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData; $reflectionClass = (new ReflectionClass($this)); $convertedProperties = collect($reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC)) - ->filter(fn (ReflectionProperty $property) => ! $property->isStatic()) - ->mapWithKeys(fn (ReflectionProperty $property) => [$property->getName() => $property->getValue($this)]) - ->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value); + ->filter(fn(ReflectionProperty $property) => !$property->isStatic()) + ->mapWithKeys(fn(ReflectionProperty $property) => [$property->getName() => $property->getValue($this)]) + ->map(fn($value) => $value instanceof Arrayable ? $value->toArray() : $value); $convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC)) - ->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods)) + ->filter(fn(ReflectionMethod $method) => !$method->isStatic() && !in_array($method->name, $this->excludedMethods)) ->mapWithKeys(function (ReflectionMethod $method) { + $attributes = $method->getAttributes(); $returnType = $method->getReturnType(); + if ($returnType instanceof ReflectionNamedType && in_array($returnType->getName(), [DeferProp::class, LazyProp::class, Closure::class])) { return [$method->name => $method->invoke($this)]; } - return [$method->name => fn () => app()->call([$this, $method->name])]; + if (count($attributes) > 0) { + foreach ($attributes as $attribute) { + $attributeInstance = $attribute->newInstance(); + if ($attributeInstance instanceof WrappingAttribute) { + return [$method->name => $attributeInstance(fn() => app()->call([$this, $method->name]))]; + } + } + } + + return [$method->name => fn() => app()->call([$this, $method->name])]; }); return collect() ->merge($staticData) ->merge($convertedProperties) ->merge($convertedMethods) - ->mapWithKeys(fn ($value, $key) => [$this->attributeNameFormatter()($key) => $value]) + ->mapWithKeys(fn($value, $key) => [$this->attributeNameFormatter()($key) => $value]) ->toArray(); } diff --git a/src/WrappingAttributes/InertiaMerge.php b/src/WrappingAttributes/InertiaMerge.php new file mode 100644 index 0000000..330205d --- /dev/null +++ b/src/WrappingAttributes/InertiaMerge.php @@ -0,0 +1,16 @@ + Date: Thu, 17 Apr 2025 02:24:45 +0000 Subject: [PATCH 2/2] Fix styling --- src/DataProvider.php | 28 ++++++++++++------------- src/WrappingAttributes/InertiaMerge.php | 1 - 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/DataProvider.php b/src/DataProvider.php index e32d7d4..e264cd9 100644 --- a/src/DataProvider.php +++ b/src/DataProvider.php @@ -5,18 +5,18 @@ namespace Webfox\InertiaDataProviders; use Closure; -use ReflectionClass; +use Illuminate\Contracts\Support\Arrayable; +use Illuminate\Contracts\Support\Jsonable; +use Inertia\DeferProp; use Inertia\LazyProp; use Inertia\Response; +use ReflectionClass; use ReflectionMethod; -use Inertia\DeferProp; -use ReflectionProperty; use ReflectionNamedType; -use Illuminate\Contracts\Support\Jsonable; +use ReflectionProperty; use Symfony\Component\VarDumper\VarDumper; -use Illuminate\Contracts\Support\Arrayable; -use Webfox\InertiaDataProviders\WrappingAttributes\WrappingAttribute; use Webfox\InertiaDataProviders\AttributeNameFormatters\AttributeNameFormatter; +use Webfox\InertiaDataProviders\WrappingAttributes\WrappingAttribute; abstract class DataProvider implements Arrayable, Jsonable { @@ -31,16 +31,16 @@ public static function collection(DataProvider|array ...$dataProviders): DataPro public function toArray(): array { - $staticData = $this->staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData; + $staticData = $this->staticData instanceof Arrayable ? $this->staticData->toArray() : $this->staticData; $reflectionClass = (new ReflectionClass($this)); $convertedProperties = collect($reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC)) - ->filter(fn(ReflectionProperty $property) => !$property->isStatic()) - ->mapWithKeys(fn(ReflectionProperty $property) => [$property->getName() => $property->getValue($this)]) - ->map(fn($value) => $value instanceof Arrayable ? $value->toArray() : $value); + ->filter(fn (ReflectionProperty $property) => ! $property->isStatic()) + ->mapWithKeys(fn (ReflectionProperty $property) => [$property->getName() => $property->getValue($this)]) + ->map(fn ($value) => $value instanceof Arrayable ? $value->toArray() : $value); $convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC)) - ->filter(fn(ReflectionMethod $method) => !$method->isStatic() && !in_array($method->name, $this->excludedMethods)) + ->filter(fn (ReflectionMethod $method) => ! $method->isStatic() && ! in_array($method->name, $this->excludedMethods)) ->mapWithKeys(function (ReflectionMethod $method) { $attributes = $method->getAttributes(); $returnType = $method->getReturnType(); @@ -53,19 +53,19 @@ public function toArray(): array foreach ($attributes as $attribute) { $attributeInstance = $attribute->newInstance(); if ($attributeInstance instanceof WrappingAttribute) { - return [$method->name => $attributeInstance(fn() => app()->call([$this, $method->name]))]; + return [$method->name => $attributeInstance(fn () => app()->call([$this, $method->name]))]; } } } - return [$method->name => fn() => app()->call([$this, $method->name])]; + return [$method->name => fn () => app()->call([$this, $method->name])]; }); return collect() ->merge($staticData) ->merge($convertedProperties) ->merge($convertedMethods) - ->mapWithKeys(fn($value, $key) => [$this->attributeNameFormatter()($key) => $value]) + ->mapWithKeys(fn ($value, $key) => [$this->attributeNameFormatter()($key) => $value]) ->toArray(); } diff --git a/src/WrappingAttributes/InertiaMerge.php b/src/WrappingAttributes/InertiaMerge.php index 330205d..31b1033 100644 --- a/src/WrappingAttributes/InertiaMerge.php +++ b/src/WrappingAttributes/InertiaMerge.php @@ -2,7 +2,6 @@ namespace Webfox\InertiaDataProviders\WrappingAttributes; - use Attribute; use Inertia\Inertia;