Skip to content

Commit 91ffaed

Browse files
authored
Merge pull request #1221: optimize container
2 parents 41e710b + 279a209 commit 91ffaed

40 files changed

+1156
-671
lines changed

psalm-baseline.xml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<files psalm-version="6.9.6@61488aa2849793acfb72411829d114b4aa670941">
3+
<file src="src/Core/src/Internal/Actor.php">
4+
<InvalidReturnStatement>
5+
<code><![CDATA[$actor->registerInstance($ctx, $instance)]]></code>
6+
</InvalidReturnStatement>
7+
<NoValue>
8+
<code><![CDATA[$injector]]></code>
9+
<code><![CDATA[$singleton]]></code>
10+
</NoValue>
11+
<TooManyArguments>
12+
<code><![CDATA[make]]></code>
13+
</TooManyArguments>
14+
<UnnecessaryVarAnnotation>
15+
<code><![CDATA[Attribute\Scope]]></code>
16+
</UnnecessaryVarAnnotation>
17+
</file>
18+
<file src="src/Filters/src/Attribute/Input/AbstractInput.php">
19+
<UndefinedThisPropertyFetch>
20+
<code><![CDATA[$this->key]]></code>
21+
</UndefinedThisPropertyFetch>
22+
</file>
23+
<file src="src/Queue/src/RetryPolicy.php">
24+
<InvalidOperand>
25+
<code><![CDATA[$this->delay * $this->multiplier ** $attempts]]></code>
26+
<code><![CDATA[$this->multiplier ** $attempts]]></code>
27+
</InvalidOperand>
28+
</file>
29+
</files>

psalm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
addParamDefaultToDocblockType="true"
1414
findUnusedBaselineEntry="true"
1515
findUnusedCode="false"
16+
errorBaseline="psalm-baseline.xml"
1617
>
1718
<projectFiles>
1819
<directory name="src/*/src" />
@@ -22,6 +23,7 @@
2223
</ignoreFiles>
2324
</projectFiles>
2425
<issueHandlers>
26+
<MissingOverrideAttribute errorLevel="suppress" />
2527
<UnusedClosureParam errorLevel="suppress" />
2628
<UnusedPsalmSuppress errorLevel="suppress" />
2729
<UnsupportedPropertyReferenceUsage errorLevel="suppress" />

src/Core/src/Config.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Spiral\Core\Internal\Binder;
1010
use Spiral\Core\Internal\Container;
1111
use Spiral\Core\Internal\Factory;
12+
use Spiral\Core\Internal\Actor;
1213
use Spiral\Core\Internal\Invoker;
1314
use Spiral\Core\Internal\Resolver;
1415
use Spiral\Core\Internal\Scope;
@@ -22,7 +23,7 @@
2223
*
2324
* @implements IteratorAggregate<
2425
* non-empty-string,
25-
* class-string<State>|class-string<ResolverInterface>|class-string<FactoryInterface>|class-string<ContainerInterface>|class-string<BinderInterface>|class-string<InvokerInterface>|class-string<Tracer>|class-string<Scope>
26+
* class-string<State>|class-string<ResolverInterface>|class-string<FactoryInterface>|class-string<ContainerInterface>|class-string<BinderInterface>|class-string<InvokerInterface>|class-string<Actor>|class-string<Scope>
2627
* >
2728
*/
2829
class Config implements \IteratorAggregate
@@ -32,6 +33,7 @@ class Config implements \IteratorAggregate
3233

3334
public readonly Internal\Config\StateStorage $scopedBindings;
3435
private bool $rootLocked = true;
36+
public readonly string $actor;
3537

3638
/**
3739
* @param class-string<State> $state
@@ -40,7 +42,6 @@ class Config implements \IteratorAggregate
4042
* @param class-string<ContainerInterface> $container
4143
* @param class-string<BinderInterface> $binder
4244
* @param class-string<InvokerInterface> $invoker
43-
* @param class-string<Tracer> $tracer
4445
*/
4546
public function __construct(
4647
public readonly string $state = State::class,
@@ -49,10 +50,10 @@ public function __construct(
4950
public readonly string $container = Container::class,
5051
public readonly string $binder = Binder::class,
5152
public readonly string $invoker = Invoker::class,
52-
public readonly string $tracer = Tracer::class,
5353
) {
5454
$this->scope = Scope::class;
5555
$this->scopedBindings = new Internal\Config\StateStorage();
56+
$this->actor = Actor::class;
5657
}
5758

5859
public function getIterator(): \Traversable
@@ -63,8 +64,8 @@ public function getIterator(): \Traversable
6364
yield 'container' => $this->container;
6465
yield 'binder' => $this->binder;
6566
yield 'invoker' => $this->invoker;
66-
yield 'tracer' => $this->tracer;
6767
yield 'scope' => $this->scope;
68+
yield 'actor' => $this->actor;
6869
}
6970

7071
/**

src/Core/src/Config/Binding.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,17 @@
77
/**
88
* @internal
99
*/
10-
abstract class Binding implements \Stringable {}
10+
abstract class Binding implements \Stringable
11+
{
12+
/**
13+
* Get the class or interface name of the object that should be returned.
14+
* May return null if the return type was not detected or is not a class.
15+
*
16+
* @return class-string|null
17+
* @internal
18+
*/
19+
public function getReturnClass(): ?string
20+
{
21+
return null;
22+
}
23+
}

src/Core/src/Config/DeprecationProxy.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public function __construct(
2828

2929
/**
3030
* @return class-string
31+
* @deprecated
3132
*/
3233
public function getInterface(): string
3334
{

src/Core/src/Config/Factory.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44

55
namespace Spiral\Core\Config;
66

7+
use Spiral\Core\Exception\Traits\ClosureRendererTrait;
8+
79
/**
810
* Make a value using a closure.
911
*/
1012
final class Factory extends Binding
1113
{
12-
use \Spiral\Core\Exception\Traits\ClosureRendererTrait;
14+
use ClosureRendererTrait;
1315

16+
/** @var class-string|null */
17+
private readonly ?string $returnClass;
1418
public readonly \Closure $factory;
1519
private readonly int $parametersCount;
1620
private ?string $definition;
@@ -20,7 +24,13 @@ public function __construct(
2024
public readonly bool $singleton = false,
2125
) {
2226
$this->factory = $callable(...);
23-
$this->parametersCount = (new \ReflectionFunction($this->factory))->getNumberOfParameters();
27+
$reflection = new \ReflectionFunction($this->factory);
28+
$this->parametersCount = $reflection->getNumberOfParameters();
29+
30+
// Detect the return type of the factory
31+
$returnType = (string) $reflection->getReturnType();
32+
$this->returnClass = \class_exists($returnType) ? $returnType : null;
33+
2434
/** @psalm-suppress TypeDoesNotContainType */
2535
$this->definition = match (true) {
2636
\is_string($callable) => $callable,
@@ -48,4 +58,13 @@ public function __toString(): string
4858
$this->definition,
4959
);
5060
}
61+
62+
/**
63+
* @return class-string|null
64+
* @internal
65+
*/
66+
public function getReturnClass(): ?string
67+
{
68+
return $this->returnClass;
69+
}
5170
}

src/Core/src/Config/Proxy.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,22 @@ public function __construct(
2929

3030
/**
3131
* @return class-string
32+
* @deprecated Use {@see getReturnClass()} instead.
3233
*/
3334
public function getInterface(): string
3435
{
3536
return $this->interface;
3637
}
3738

39+
/**
40+
* @return class-string
41+
* @internal
42+
*/
43+
public function getReturnClass(): string
44+
{
45+
return $this->interface;
46+
}
47+
3848
public function __toString(): string
3949
{
4050
return \sprintf('Proxy to `%s`', $this->interface);

src/Core/src/Config/Shared.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ public function __toString(): string
1818
{
1919
return 'Shared object of class ' . $this->value::class;
2020
}
21+
22+
/**
23+
* @return class-string
24+
* @internal
25+
*/
26+
public function getReturnClass(): string
27+
{
28+
return $this->value::class;
29+
}
2130
}

src/Core/src/Container.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ final class Container implements
5454
private BinderInterface|Internal\Binder $binder;
5555
private InvokerInterface|Internal\Invoker $invoker;
5656
private Internal\Scope $scope;
57+
private Internal\Actor $actor;
5758

5859
/**
5960
* Container constructor.
@@ -392,7 +393,7 @@ private function runIsolatedScope(Scope $config, callable $closure): mixed
392393
$container = new self($this->config, $config->name, $this->options);
393394

394395
// Configure scope
395-
$container->scope->setParent($this, $this->scope, $this->factory);
396+
$container->scope->setParent($this, $this->scope, $this->actor);
396397

397398
// Add specific bindings
398399
foreach ($config->bindings as $alias => $resolver) {

src/Core/src/Exception/Container/ArgumentException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Spiral\Core\Exception\Container;
66

77
/**
8-
* Unable to resolve argument value.
8+
* @deprecated
99
*/
1010
class ArgumentException extends AutowireException
1111
{

0 commit comments

Comments
 (0)