Skip to content

[12.x] Add Singleton and Scoped attributes to Container #56334

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

Merged
merged 3 commits into from
Jul 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Illuminate/Container/Attributes/Scoped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
final class Scoped
{
}
10 changes: 10 additions & 0 deletions src/Illuminate/Container/Attributes/Singleton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Illuminate\Container\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
final class Singleton
{
}
32 changes: 29 additions & 3 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use ArrayAccess;
use Closure;
use Exception;
use Illuminate\Container\Attributes\Scoped;
use Illuminate\Container\Attributes\Singleton;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Container\CircularDependencyException;
use Illuminate\Contracts\Container\Container as ContainerContract;
Expand Down Expand Up @@ -252,9 +254,33 @@ public function resolved($abstract)
*/
public function isShared($abstract)
{
return isset($this->instances[$abstract]) ||
(isset($this->bindings[$abstract]['shared']) &&
$this->bindings[$abstract]['shared'] === true);
if (isset($this->instances[$abstract])) {
return true;
}

if (isset($this->bindings[$abstract]['shared']) && $this->bindings[$abstract]['shared'] === true) {
return true;
}

if (! class_exists($abstract)) {
return false;
}

$reflection = new ReflectionClass($abstract);

if (! empty($reflection->getAttributes(Singleton::class))) {
return true;
}

if (! empty($reflection->getAttributes(Scoped::class))) {
if (! in_array($abstract, $this->scopedInstances, true)) {
$this->scopedInstances[] = $abstract;
}

return true;
}

return false;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions tests/Container/ContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Illuminate\Tests\Container;

use Attribute;
use Illuminate\Container\Attributes\Scoped;
use Illuminate\Container\Attributes\Singleton;
use Illuminate\Container\Container;
use Illuminate\Container\EntryNotFoundException;
use Illuminate\Contracts\Container\BindingResolutionException;
Expand Down Expand Up @@ -740,6 +742,30 @@ public function testMethodLevelContextualBinding()
$this->assertInstanceOf(ContainerImplementationStub::class, $result);
}

public function testContainerSingletonAttribute()
{
$container = new Container;
$firstInstantiation = $container->get(ContainerSingletonAttribute::class);

$secondInstantiation = $container->get(ContainerSingletonAttribute::class);

$this->assertSame($firstInstantiation, $secondInstantiation);
}

public function testContainerScopedAttribute()
{
$container = new Container;
$firstInstantiation = $container->get(ContainerScopedAttribute::class);
$secondInstantiation = $container->get(ContainerScopedAttribute::class);

$this->assertSame($firstInstantiation, $secondInstantiation);

$container->forgetScopedInstances();

$thirdInstantiation = $container->get(ContainerScopedAttribute::class);
$this->assertNotSame($firstInstantiation, $thirdInstantiation);
}

// public function testContainerCanCatchCircularDependency()
// {
// $this->expectException(\Illuminate\Contracts\Container\CircularDependencyException::class);
Expand Down Expand Up @@ -899,3 +925,13 @@ public function __construct(
$this->currentlyResolving = $currentlyResolving;
}
}

#[Singleton]
class ContainerSingletonAttribute
{
}

#[Scoped]
class ContainerScopedAttribute
{
}
Loading