Skip to content

Commit

Permalink
Set components or namespaces to lock
Browse files Browse the repository at this point in the history
  • Loading branch information
PhiloNL committed Jul 29, 2024
1 parent 727216e commit 4a0da20
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/Features/SupportLockedProperties/SupportLockedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,30 @@ class SupportLockedProperties extends ComponentHook
{
public static bool $locked = false;

public static array $components = [];

public function update($propertyName, $fullPath, $newValue)
{
if (self::$locked === false) {
return;
}

$checkIsRequired = false;

foreach (self::$components as $component) {
if (str($component)->contains('*') && str($this->component::class)->is($component)) {
$checkIsRequired = true;
}

if ($component === $this->component::class) {
$checkIsRequired = true;
}
}

if (! $checkIsRequired) {
return;
}

$componentIsUnlocked = $this->component
->getAttributes()
->whereInstanceOf(Unlocked::class)
Expand Down
72 changes: 64 additions & 8 deletions src/Features/SupportLockedProperties/UnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,13 @@

class UnitTest extends \Tests\TestCase
{
public function tearDown(): void
{
//SupportLockedProperties::$locked = false;

parent::tearDown();
}

public function test_cant_update_globally_locked_property()
{
$this->expectExceptionMessage(
'Cannot update locked property: [count]'
);

LivewireStrict::lockProperties();
LivewireStrict::lockProperties(components: 'WireElements\*');

Livewire::test(new class extends TestComponent
{
Expand Down Expand Up @@ -61,6 +54,65 @@ public function test_can_update_unlocked_component()
->assertSetStrict('count', 1)
->set('count', 2);
}

public function test_only_enabled_for_specific_namespace()
{
$this->expectExceptionMessage(
'Cannot update locked property: [count]'
);

LivewireStrict::lockProperties(components: 'WireElements\*');

Livewire::test(new class extends SpecificComponent
{
public $count = 1;

public function increment()
{
$this->count++;
}
})
->assertSetStrict('count', 1)
->set('count', 2);
}

public function test_only_enabled_for_specific_components()
{
$this->expectExceptionMessage(
'Cannot update locked property: [count]'
);

LivewireStrict::lockProperties(components: 'WireElements\LivewireStrict\Features\SupportLockedProperties\SpecificComponent*');

Livewire::test(new class extends SpecificComponent
{
public $count = 1;

public function increment()
{
$this->count++;
}
})
->assertSetStrict('count', 1)
->set('count', 2);
}

public function test_it_ignores_other_components()
{
LivewireStrict::lockProperties(components: 'App/*');

Livewire::test(new class extends SpecificComponent
{
public $count = 1;

public function increment()
{
$this->count++;
}
})
->assertSetStrict('count', 1)
->set('count', 2);
}
}

class TestComponent extends Component
Expand All @@ -70,3 +122,7 @@ public function render()
return '<div></div>';
}
}

class SpecificComponent extends TestComponent
{
}
6 changes: 4 additions & 2 deletions src/LivewireStrict.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace WireElements\LivewireStrict;

use Illuminate\Support\Arr;
use WireElements\LivewireStrict\Features\SupportLockedProperties\SupportLockedProperties;

class LivewireStrict
{
public static function lockProperties($condition = true)
public static function lockProperties($shouldLockProperties = true, $components = ['App\Livewire\*'])
{
SupportLockedProperties::$locked = $condition;
SupportLockedProperties::$locked = $shouldLockProperties;
SupportLockedProperties::$components = Arr::wrap($components);
}

public static function enableAll($condition = true)
Expand Down

0 comments on commit 4a0da20

Please sign in to comment.