From 0e45cfdc6ea6c5e478e084003b8e9474c3c545ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 25 Apr 2021 19:44:35 +0200 Subject: [PATCH] Support Livewire-namespaced traits. Resolve #1 --- src/WithExplicitAccess.php | 18 ++++++++++++++++-- tests/PaginatedComponent.php | 28 ++++++++++++++++++++++++++++ tests/PaginationTest.php | 22 ++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/PaginatedComponent.php create mode 100644 tests/PaginationTest.php diff --git a/src/WithExplicitAccess.php b/src/WithExplicitAccess.php index b6f5a28..c73da03 100644 --- a/src/WithExplicitAccess.php +++ b/src/WithExplicitAccess.php @@ -6,18 +6,32 @@ namespace Lean\LivewireAccess; use ReflectionMethod; use ReflectionProperty; +use Illuminate\Support\Str; +use ReflectionClass; trait WithExplicitAccess { protected function methodIsPublicAndNotDefinedOnBaseClass($methodName) { + $livewireMethods = collect((new ReflectionClass($this))->getTraits()) // Get all traits + ->filter(fn ($reflection, $traitName) => Str::startsWith($traitName, 'Livewire\\')) // Filter those in Livewire namespace + ->map(fn (ReflectionClass $trait) => $trait->getMethods()) // Get their methods + ->map(fn (array $methods) => collect($methods)->map(fn (ReflectionMethod $method) => $method->getName())) // Convert the methods to collections of method names + ->flatten(); // Flatten the collection to get merge the inner collections with method names + return parent::methodIsPublicAndNotDefinedOnBaseClass($methodName) - && count((new ReflectionMethod($this, $methodName))->getAttributes(FrontendAccess::class)) > 0; + && ($livewireMethods->contains($methodName) || (count((new ReflectionMethod($this, $methodName))->getAttributes(FrontendAccess::class)) > 0)); } public function propertyIsPublicAndNotDefinedOnBaseClass($propertyName) { + $livewireProperties = collect((new ReflectionClass($this))->getTraits()) // Get all traits + ->filter(fn ($reflection, $traitName) => Str::startsWith($traitName, 'Livewire\\')) // Filter those in Livewire namespace + ->map(fn (ReflectionClass $trait) => $trait->getProperties()) // Get their properties + ->map(fn (array $properties) => collect($properties)->map(fn (ReflectionProperty $method) => $method->getName())) // Convert the methods to collections of property names + ->flatten(); // Flatten the collection to get merge the inner collections with property names + return parent::propertyIsPublicAndNotDefinedOnBaseClass($propertyName) - && count((new ReflectionProperty($this, $propertyName))->getAttributes(FrontendAccess::class)) > 0; + && ($livewireProperties->contains($propertyName) || (count((new ReflectionProperty($this, $propertyName))->getAttributes(FrontendAccess::class)) > 0)); } } diff --git a/tests/PaginatedComponent.php b/tests/PaginatedComponent.php new file mode 100644 index 0000000..73bb151 --- /dev/null +++ b/tests/PaginatedComponent.php @@ -0,0 +1,28 @@ +call('gotoPage', 2); + } + + /** @test */ + public function livewire_trait_properties_are_supported() + { + Livewire::test(PaginatedComponent::class) + ->call('$set', 'page', 3); + } +}