diff --git a/src/DataProvider.php b/src/DataProvider.php index 47db17f..e264cd9 100644 --- a/src/DataProvider.php +++ b/src/DataProvider.php @@ -16,6 +16,7 @@ use ReflectionProperty; use Symfony\Component\VarDumper\VarDumper; use Webfox\InertiaDataProviders\AttributeNameFormatters\AttributeNameFormatter; +use Webfox\InertiaDataProviders\WrappingAttributes\WrappingAttribute; abstract class DataProvider implements Arrayable, Jsonable { @@ -41,11 +42,22 @@ public function toArray(): array $convertedMethods = collect($reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC)) ->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)]; } + 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])]; }); diff --git a/src/WrappingAttributes/InertiaMerge.php b/src/WrappingAttributes/InertiaMerge.php new file mode 100644 index 0000000..31b1033 --- /dev/null +++ b/src/WrappingAttributes/InertiaMerge.php @@ -0,0 +1,15 @@ +