Skip to content
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

Revert "[11.x] Allow non-ContextualAttribute attributes to have an after callback" #52281

Merged
merged 1 commit into from
Jul 26, 2024
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
8 changes: 6 additions & 2 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,8 +1366,12 @@ protected function fireAfterResolvingCallbacks($abstract, $object)
protected function fireAfterResolvingAttributeCallbacks(array $attributes, $object)
{
foreach ($attributes as $attribute) {
if (method_exists($instance = $attribute->newInstance(), 'after')) {
$instance->after($instance, $object, $this);
if (is_a($attribute->getName(), ContextualAttribute::class, true)) {
$instance = $attribute->newInstance();

if (method_exists($instance, 'after')) {
$instance->after($instance, $object, $this);
}
}

$callbacks = $this->getCallbacksForType(
Expand Down
24 changes: 1 addition & 23 deletions tests/Container/AfterResolvingAttributeCallbackTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,18 @@ class AfterResolvingAttributeCallbackTest extends TestCase
public function testCallbackIsCalledAfterDependencyResolutionWithAttribute()
{
$container = new Container();
$stack = [];

$container->afterResolvingAttribute(ContainerTestOnTenant::class, function (ContainerTestOnTenant $attribute, HasTenantImpl $hasTenantImpl, Container $container) use (&$stack) {
$container->afterResolvingAttribute(ContainerTestOnTenant::class, function (ContainerTestOnTenant $attribute, HasTenantImpl $hasTenantImpl, Container $container) {
$hasTenantImpl->onTenant($attribute->tenant);
$stack[] = $attribute->tenant;
});

$hasTenantA = $container->make(ContainerTestHasTenantImplPropertyWithTenantA::class);
$this->assertInstanceOf(HasTenantImpl::class, $hasTenantA->property);
$this->assertEquals(Tenant::TenantA, $hasTenantA->property->tenant);
$this->assertContains(Tenant::TenantA, $stack);

$hasTenantB = $container->make(ContainerTestHasTenantImplPropertyWithTenantB::class);
$this->assertInstanceOf(HasTenantImpl::class, $hasTenantB->property);
$this->assertEquals(Tenant::TenantB, $hasTenantB->property->tenant);
$this->assertContains(Tenant::TenantB, $stack);
}

public function testCallbackIsCalledAfterClassWithAttributeIsResolved()
Expand Down Expand Up @@ -61,19 +57,6 @@ public function testCallbackIsCalledAfterClassWithConstructorAndAttributeIsResol
$this->assertInstanceOf(ContainerTestHasSelfConfiguringAttributeAndConstructor::class, $instance);
$this->assertEquals('the-right-value', $instance->value);
}

public function testAfterCallbackIsCalled()
{
$container = new Container();

$hasTenantA = $container->make(ContainerTestHasTenantImplPropertyWithTenantA::class);
$this->assertInstanceOf(HasTenantImpl::class, $hasTenantA->property);
$this->assertEquals(Tenant::TenantA, $hasTenantA->property->tenant);

$hasTenantB = $container->make(ContainerTestHasTenantImplPropertyWithTenantB::class);
$this->assertInstanceOf(HasTenantImpl::class, $hasTenantB->property);
$this->assertEquals(Tenant::TenantB, $hasTenantB->property->tenant);
}
}

#[Attribute(Attribute::TARGET_PARAMETER)]
Expand All @@ -83,11 +66,6 @@ public function __construct(
public readonly Tenant $tenant
) {
}

public function after(self $attribute, HasTenantImpl $class, Container $container): void
{
$class->onTenant($attribute->tenant);
}
}

enum Tenant
Expand Down