diff --git a/src/Features/SupportLockedProperties/SupportLockedProperties.php b/src/Features/SupportLockedProperties/SupportLockedProperties.php index 5d93b35..3a43894 100644 --- a/src/Features/SupportLockedProperties/SupportLockedProperties.php +++ b/src/Features/SupportLockedProperties/SupportLockedProperties.php @@ -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) diff --git a/src/Features/SupportLockedProperties/UnitTest.php b/src/Features/SupportLockedProperties/UnitTest.php index 60e6284..e3d34f7 100644 --- a/src/Features/SupportLockedProperties/UnitTest.php +++ b/src/Features/SupportLockedProperties/UnitTest.php @@ -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 { @@ -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 @@ -70,3 +122,7 @@ public function render() return '
'; } } + +class SpecificComponent extends TestComponent +{ +} diff --git a/src/LivewireStrict.php b/src/LivewireStrict.php index 52c9c31..3f9e79e 100644 --- a/src/LivewireStrict.php +++ b/src/LivewireStrict.php @@ -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)