Skip to content

Commit

Permalink
Support Livewire-namespaced traits. Resolve #1
Browse files Browse the repository at this point in the history
  • Loading branch information
stancl committed Apr 25, 2021
1 parent 2cc9881 commit 0e45cfd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/WithExplicitAccess.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@

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));
}
}
28 changes: 28 additions & 0 deletions tests/PaginatedComponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Lean\LivewireAccess\Tests;

use Lean\LivewireAccess\WithExplicitAccess;
use Lean\LivewireAccess\FrontendAccess;
use Livewire\Component;
use Livewire\WithPagination;

class PaginatedComponent extends Component
{
use WithExplicitAccess, WithPagination;

public string $foo = 'foo';

#[FrontendAccess]
public string $bar = 'bar';

public function abc() {}

#[FrontendAccess]
public function def() {}

public function render()
{
return '';
}
}
22 changes: 22 additions & 0 deletions tests/PaginationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Lean\LivewireAccess\Tests;

use Livewire\Livewire;

class PaginationTest extends TestCase
{
/** @test */
public function livewire_trait_methods_are_supported()
{
Livewire::test(PaginatedComponent::class)
->call('gotoPage', 2);
}

/** @test */
public function livewire_trait_properties_are_supported()
{
Livewire::test(PaginatedComponent::class)
->call('$set', 'page', 3);
}
}

0 comments on commit 0e45cfd

Please sign in to comment.