Skip to content

Commit

Permalink
feat: allow non-contextual attributes to have an after callback (la…
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi authored Jul 18, 2024
1 parent a128fc5 commit 45592e6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
8 changes: 2 additions & 6 deletions src/Illuminate/Container/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -1366,12 +1366,8 @@ protected function fireAfterResolvingCallbacks($abstract, $object)
protected function fireAfterResolvingAttributeCallbacks(array $attributes, $object)
{
foreach ($attributes as $attribute) {
if (is_a($attribute->getName(), ContextualAttribute::class, true)) {
$instance = $attribute->newInstance();

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

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

$container->afterResolvingAttribute(ContainerTestOnTenant::class, function (ContainerTestOnTenant $attribute, HasTenantImpl $hasTenantImpl, Container $container) {
$container->afterResolvingAttribute(ContainerTestOnTenant::class, function (ContainerTestOnTenant $attribute, HasTenantImpl $hasTenantImpl, Container $container) use (&$stack) {
$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 @@ -57,6 +61,19 @@ 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 @@ -66,6 +83,11 @@ 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

0 comments on commit 45592e6

Please sign in to comment.