[3.x] Fix conflicts with PHP's predefined classes #8253
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes the problem described in discussion #8248.
Problem
When you have a component in Livewire's root class namespace that matches any of PHP's predefined classes, subsequent requests won't work.
So, an initial request for the component
App\Livewire\Directory
works, but subsequent requests won't. Those will trigger Livewire'sComponentNotFoundException
:Note: when the component is located as
App\Livewire\Foo\Directory
, also subsequent requests will work.Cause
For subsequent requests, Livewire will try to resolve the requested component's location.
Initially, it will check if a class exists for the requested component:
livewire/src/Mechanisms/ComponentRegistry.php
Line 90 in 4f86fc1
For
App\Livewire\Directory
(which will be requested asdirectory
), this will return true, becauseclass_exists('directory')
returns true as it matches PHP's predefined class. Note: that is also the explanation why locatingApp\Livewire\Foo\Directory
works, becauseclass_exists('foo.directory')
won't conflict.Solution
We can extend the class existance-check with a check if the found class also extends Livewire's
Component
class. Obviously PHP's predefined class does not comply with that, whileApp\Livewire\Directory
will.