-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[12.x] Fix usage of Scoped and Singleton on interfaces
#56620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
Thanks for submitting a PR! Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface. Pull requests that are abandoned in draft may be closed due to inactivity. |
Bind with Scoped and Singleton attributesScoped and Singleton on interfaces
|
For @taylorotwell's reference: The docs says:
Reference: https://laravel.com/docs/12.x/container#bind-attribute But within a new project, the following assertion fails: <?php // ./routes/console.php
use Illuminate\Container\Attributes\Bind;
use Illuminate\Container\Attributes\Singleton;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
Artisan::command('app:test', function () {
$one = \resolve(ISingleton::class);
$other = \resolve(ISingleton::class);
\assert($one->id === $other->id);
});
#[Bind(ConcreteSingleton::class)]
#[Singleton]
interface ISingleton
{
}
class ConcreteSingleton implements ISingleton
{
public readonly string $id;
public function __construct() {
$this->id = Str::random();
}
}$ php artisan app:test
AssertionError
assert($one->id === $other->id)
at routes/console.php:12As a workaround, in the current implementation (without this PR), if one applies the But as this PR states, it is different from what is recommended in the docs. |
Noticed earlier that this doesn't work as described in the docs 😰
The methodology is that we only check for the Scoped/Singleton on classes (interfaces, abstract classes) that also have Bind.
This raises a few questions:
resolve(InterfaceB::class), should it be marking it as a singleton?