Skip to content

Commit

Permalink
Core: fix scope discarding when resolving a service from Autowire object
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk committed Dec 12, 2024
1 parent 0550252 commit 20a660d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/Core/src/Container/Autowire.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ final class Autowire
* @param non-empty-string|class-string<TObject> $alias
*/
public function __construct(
private readonly string $alias,
private readonly array $parameters = []
public readonly string $alias,
public readonly array $parameters = []
) {
}

Expand Down
8 changes: 6 additions & 2 deletions src/Core/src/Internal/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,13 @@ private function resolveAutowire(
Stringable|string|null $context,
array $arguments,
): mixed {
$instance = $binding->autowire->resolve($this, $arguments);
$target = $binding->autowire->alias;
$ctx = new Ctx(alias: $alias, class: $target, context: $context, singleton: $binding->singleton);

$instance = $alias === $target
? $this->autowire($ctx, \array_merge($binding->autowire->parameters, $arguments))
: $binding->autowire->resolve($this, $arguments);

$ctx = new Ctx(alias: $alias, class: $alias, context: $context, singleton: $binding->singleton);
return $this->validateNewInstance($instance, $ctx, $arguments);
}

Expand Down
32 changes: 32 additions & 0 deletions src/Core/tests/Scope/SideEffectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

namespace Spiral\Tests\Core\Scope;

use Spiral\Core\Config\Alias;
use Spiral\Core\Container;
use Spiral\Core\Scope;
use Spiral\Tests\Core\Scope\Stub\Factory;
use Spiral\Tests\Core\Scope\Stub\FileLogger;
use Spiral\Tests\Core\Scope\Stub\KVLogger;
use Spiral\Tests\Core\Scope\Stub\LoggerCarrier;
use Spiral\Tests\Core\Scope\Stub\LoggerInterface;
use Spiral\Tests\Core\Scope\Stub\ScopeIndicatorLogger;

final class SideEffectTest extends BaseTestCase
{
Expand Down Expand Up @@ -49,4 +51,34 @@ public function testFactory(): void
);
});
}

public function testAutowireWithScope(): void
{
$root = new Container();
$root
->getBinder('test')
->bind(ScopeIndicatorLogger::class, new Container\Autowire(ScopeIndicatorLogger::class));

$logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger) {
return $logger;
});

$this->assertNotNull($logger);
$this->assertSame('test', $logger->getName());
}

public function testAliasWithScope(): void
{
$root = new Container();
$root
->getBinder('test')
->bind(ScopeIndicatorLogger::class, new Alias(ScopeIndicatorLogger::class));

$logger = $root->runScope(new Scope('test'), static function (?ScopeIndicatorLogger $logger) {
return $logger;
});

$this->assertNotNull($logger);
$this->assertSame('test', $logger->getName());
}
}
26 changes: 26 additions & 0 deletions src/Core/tests/Scope/Stub/ScopeIndicatorLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Spiral\Tests\Core\Scope\Stub;

use Psr\Container\ContainerInterface;
use Spiral\Core\Internal\Introspector;

/**
* Returns the scope name where it was created
*/
final class ScopeIndicatorLogger implements LoggerInterface
{
private ?string $scope;

public function __construct(ContainerInterface $container)
{
$this->scope = Introspector::scopeName($container);
}

public function getName(): string
{
return $this->scope ?? '-NULL-';
}
}

0 comments on commit 20a660d

Please sign in to comment.