Skip to content

Commit 9bf9d0f

Browse files
authored
Add contract to DI container (#9)
1 parent 0e4f8fa commit 9bf9d0f

20 files changed

+89
-89
lines changed

packages/DI/src/ContainerInterface.php renamed to packages/Contracts/DI/Container.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI;
12+
namespace Aether\Contracts\DI;
1313

1414
use Psr\Container\ContainerInterface as BaseContainerInterface;
1515

16-
interface ContainerInterface extends BaseContainerInterface, FactoryInterface, InvokerInterface
16+
interface Container extends BaseContainerInterface, Factory, Invoker
1717
{
1818
/**
1919
* Returns an entry of the container by its id.

packages/DI/src/Exception/ContainerException.php renamed to packages/Contracts/DI/Exception/ContainerException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI\Exception;
12+
namespace Aether\Contracts\DI\Exception;
1313

1414
use Psr\Container\ContainerExceptionInterface;
1515

packages/DI/src/Exception/EntryNotFoundException.php renamed to packages/Contracts/DI/Exception/EntryNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI\Exception;
12+
namespace Aether\Contracts\DI\Exception;
1313

1414
use Psr\Container\NotFoundExceptionInterface;
1515

packages/DI/src/Exception/ResolvingException.php renamed to packages/Contracts/DI/Exception/ResolvingException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI\Exception;
12+
namespace Aether\Contracts\DI\Exception;
1313

1414
class ResolvingException extends ContainerException
1515
{

packages/DI/src/Exception/RuntimeException.php renamed to packages/Contracts/DI/Exception/RuntimeException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI\Exception;
12+
namespace Aether\Contracts\DI\Exception;
1313

1414
class RuntimeException extends \RuntimeException
1515
{

packages/DI/src/FactoryInterface.php renamed to packages/Contracts/DI/Factory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI;
12+
namespace Aether\Contracts\DI;
1313

14-
interface FactoryInterface
14+
interface Factory
1515
{
1616
/**
1717
* Initializes a new instance of requested class using binding and set of parameters.
@@ -24,7 +24,7 @@ interface FactoryInterface
2424
* @return ($abstract is class-string ? T : int|float|string|callable|object|null)
2525
*
2626
* @throws \Aether\DI\Definition\Exception\CircularDependencyException
27-
* @throws \Aether\DI\Exception\EntryNotFoundException
27+
* @throws \Aether\Contracts\DI\Exception\EntryNotFoundException
2828
*
2929
* @template T of object
3030
*/

packages/DI/src/InvokerInterface.php renamed to packages/Contracts/DI/Invoker.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI;
12+
namespace Aether\Contracts\DI;
1313

14-
interface InvokerInterface
14+
interface Invoker
1515
{
1616
/**
1717
* Calls the given function using the given parameters. Missing parameters will be resolved from the container.

packages/DI/src/Container.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@
1111

1212
namespace Aether\DI;
1313

14+
use Aether\Contracts\DI\Container as ContainerContract;
15+
use Aether\Contracts\DI\Exception\EntryNotFoundException;
16+
use Aether\Contracts\DI\Factory as FactoryContract;
17+
use Aether\Contracts\DI\Invoker as InvokerContract;
18+
use Aether\DI\Definition\Binding\Alias;
19+
use Aether\DI\Definition\Binding\Factory;
20+
use Aether\DI\Definition\Binding\Scalar;
21+
use Aether\DI\Definition\Binding\Shared;
22+
use Aether\DI\Definition\Binding\WeakReference;
23+
use Aether\DI\Definition\Exception\CircularDependencyException;
24+
use Aether\DI\Definition\Resolver\DefinitionResolver;
25+
use Aether\DI\Definition\State;
26+
1427
use function array_key_exists;
1528

1629
use Closure;
@@ -22,36 +35,23 @@
2235
use function is_string;
2336
use function property_exists;
2437

25-
use Aether\DI\Definition\Binding\Alias;
26-
use Aether\DI\Definition\Binding\Factory;
27-
use Aether\DI\Definition\Binding\Scalar;
28-
use Aether\DI\Definition\Binding\Shared;
29-
use Aether\DI\Definition\Binding\WeakReference;
30-
31-
use Aether\DI\Definition\Exception\CircularDependencyException;
32-
use Aether\DI\Definition\Resolver\DefinitionResolver;
33-
use Aether\DI\Definition\State;
34-
use Aether\DI\Exception\EntryNotFoundException;
35-
use Aether\DI\Invoker\Invoker;
36-
37-
class Container implements ContainerInterface
38+
class Container implements ContainerContract
3839
{
3940
private State $state;
40-
private FactoryInterface $factory;
41+
private FactoryContract $factory;
4142

4243
public function __construct()
4344
{
44-
4545
$this->state = new State();
4646
$this->factory = new DefinitionResolver($this->state, $this);
4747

4848
$shared = new Alias(self::class);
4949

5050
$this->state->bindings = [
5151
self::class => new WeakReference(\WeakReference::create($this)),
52-
ContainerInterface::class => $shared,
53-
FactoryInterface::class => $shared,
54-
InvokerInterface::class => $shared
52+
ContainerContract::class => $shared,
53+
Factory::class => $shared,
54+
Invoker::class => $shared,
5555
];
5656
}
5757

@@ -85,6 +85,7 @@ public function get(string $id): int|float|string|callable|object
8585
public function instance(string $id, int|float|string|callable|object $instance): int|float|string|callable|object
8686
{
8787
$this->state->instances[$id] = $instance;
88+
8889
return $instance;
8990
}
9091

@@ -147,7 +148,7 @@ public function call(callable|array|string $callable, array $parameters = []): m
147148
return $this->getInvoker()->call($callable, $parameters);
148149
}
149150

150-
private function getInvoker(): InvokerInterface
151+
private function getInvoker(): InvokerContract
151152
{
152153
return new Invoker($this);
153154
}

packages/DI/src/Definition/Binding/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Aether\DI\Definition\Binding;
1313

14-
use Closure;
1514
use Aether\DI\Definition\Definition;
15+
use Closure;
1616

1717
final readonly class Factory implements Definition
1818
{

packages/DI/src/Definition/Exception/CircularDependencyException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Aether\DI\Definition\Exception;
1313

14-
use Aether\DI\Exception\ContainerException;
14+
use Aether\Contracts\DI\Exception\ContainerException;
1515

1616
class CircularDependencyException extends ContainerException
1717
{

packages/DI/src/Definition/Exception/DependencyException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace Aether\DI\Definition\Exception;
1313

14-
use Aether\DI\Exception\ContainerException;
14+
use Aether\Contracts\DI\Exception\ContainerException;
1515

1616
class DependencyException extends ContainerException
1717
{

packages/DI/src/Definition/Resolver/DefinitionResolver.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,41 @@
1111

1212
namespace Aether\DI\Definition\Resolver;
1313

14-
use function array_key_exists;
15-
use function class_exists;
16-
use function interface_exists;
17-
18-
use ReflectionClass;
19-
use Aether\DI\ContainerInterface;
14+
use Aether\Contracts\DI\Container;
15+
use Aether\Contracts\DI\Exception\ContainerException;
16+
use Aether\Contracts\DI\Exception\EntryNotFoundException;
17+
use Aether\Contracts\DI\Factory;
2018
use Aether\DI\Definition\Binding\Alias;
2119
use Aether\DI\Definition\Binding\Factory as FactoryBinding;
2220
use Aether\DI\Definition\Binding\WeakReference;
2321
use Aether\DI\Definition\Exception\CircularDependencyException;
2422
use Aether\DI\Definition\State;
2523

26-
use Aether\DI\Exception\ContainerException;
27-
use Aether\DI\Exception\EntryNotFoundException;
28-
use Aether\DI\FactoryInterface;
24+
use function array_key_exists;
25+
use function class_exists;
26+
use function interface_exists;
27+
28+
use ReflectionClass;
2929

30-
final class DefinitionResolver implements FactoryInterface
30+
final class DefinitionResolver implements Factory
3131
{
3232
/**
3333
* The stack of concrete currently being built.
3434
*
35-
* @var array<string, bool> $buildStack
35+
* @var array<string, bool>
3636
*/
3737
private array $buildStack = [];
3838

3939
/**
4040
* The parameter resolver.
4141
*
42-
* @var \Aether\DI\Definition\Resolver\ParameterResolverInterface $parameterResolver
42+
* @var \Aether\DI\Definition\Resolver\ParameterResolverInterface
4343
*/
4444
private ParameterResolverInterface $parameterResolver;
4545

4646
public function __construct(
4747
private readonly State $state,
48-
private readonly ContainerInterface $container
48+
private readonly Container $container
4949
) {
5050
$this->parameterResolver = new ParameterResolver($this->container);
5151
}
@@ -84,7 +84,7 @@ public function make(string $abstract, array $parameters = []): int|float|string
8484
return $concrete->value->get();
8585
}
8686

87-
if (!\property_exists($concrete, 'value')) {
87+
if (! \property_exists($concrete, 'value')) {
8888
return null;
8989
}
9090

@@ -112,13 +112,13 @@ private function autowire(string $abstract, array $parameters = []): object
112112
);
113113
}
114114

115-
if (!(class_exists($abstract) || interface_exists($abstract))) {
115+
if (! (class_exists($abstract) || interface_exists($abstract))) {
116116
throw new EntryNotFoundException($abstract);
117117
}
118118

119119
$reflection = new ReflectionClass($abstract);
120120

121-
if (!$reflection->isInstantiable()) {
121+
if (! $reflection->isInstantiable()) {
122122
throw new ContainerException(
123123
"$abstract is not instantiable."
124124
);

packages/DI/src/Definition/Resolver/ParameterResolver.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111

1212
namespace Aether\DI\Definition\Resolver;
1313

14+
use Aether\Contracts\DI\Container;
15+
use Aether\DI\Definition\Exception\DependencyException;
16+
1417
use function array_key_exists;
1518

1619
use ReflectionFunctionAbstract;
17-
use Aether\DI\ContainerInterface;
18-
19-
use Aether\DI\Definition\Exception\DependencyException;
2020

2121
final class ParameterResolver implements ParameterResolverInterface
2222
{
2323
public function __construct(
24-
private readonly ContainerInterface $container
24+
private readonly Container $container
2525
) {
2626
}
2727

@@ -44,11 +44,12 @@ public function resolveParameters(
4444

4545
if (array_key_exists($parameter->getName(), $parameters)) {
4646
$arguments[] = $parameters[$parameter->getName()];
47-
} elseif ($type !== null && !$type->isBuiltin()) {
47+
} elseif ($type !== null && ! $type->isBuiltin()) {
4848
$arguments[] = $this->container->make($type->getName(), $parameters);
4949
} else {
5050
if ($parameter->isDefaultValueAvailable() || $parameter->isOptional()) {
5151
$arguments[] = $parameter->getDefaultValue();
52+
5253
continue;
5354
}
5455

packages/DI/src/Definition/State.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ final class State
1616
/**
1717
* The bindings.
1818
*
19-
* @var array<string, \Aether\DI\Definition\Definition> $bindings
19+
* @var array<string, \Aether\DI\Definition\Definition>
2020
*/
2121
public array $bindings = [];
2222

2323
/**
2424
* The shared instances.
2525
*
26-
* @var array<string, int|float|string|callable|object> $instances
26+
* @var array<string, int|float|string|callable|object>
2727
*/
2828
public array $instances = [];
2929
}

packages/DI/src/Invoker/Invoker.php renamed to packages/DI/src/Invoker.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,14 @@
99
* of the MIT license. See the LICENSE file for details.
1010
*/
1111

12-
namespace Aether\DI\Invoker;
12+
namespace Aether\DI;
1313

14+
use Aether\Contracts\DI\Container;
15+
use Aether\Contracts\DI\Exception\ContainerException;
16+
use Aether\Contracts\DI\Exception\RuntimeException;
17+
use Aether\Contracts\DI\Invoker as InvokerContract;
18+
use Aether\DI\Definition\Resolver\ParameterResolver;
19+
use Aether\DI\Definition\Resolver\ParameterResolverInterface;
1420
use Closure;
1521

1622
use function is_array;
@@ -20,20 +26,13 @@
2026
use ReflectionException;
2127
use ReflectionFunction;
2228
use ReflectionMethod;
23-
use Aether\DI\ContainerInterface;
24-
use Aether\DI\Definition\Resolver\ParameterResolver;
25-
use Aether\DI\Definition\Resolver\ParameterResolverInterface;
26-
27-
use Aether\DI\Exception\ContainerException;
28-
use Aether\DI\Exception\RuntimeException;
29-
use Aether\DI\InvokerInterface;
3029

31-
final class Invoker implements InvokerInterface
30+
final class Invoker implements InvokerContract
3231
{
3332
private readonly ParameterResolverInterface $resolver;
3433

3534
public function __construct(
36-
private readonly ContainerInterface $container
35+
private readonly Container $container
3736
) {
3837
$this->resolver = new ParameterResolver($container);
3938
}
@@ -44,7 +43,7 @@ public function __construct(
4443
public function call(callable|array|string $callable, array $parameters = []): mixed
4544
{
4645
if (is_array($callable)) {
47-
if (!isset($callable[1])) {
46+
if (! isset($callable[1])) {
4847
$callable[1] = '__invoke';
4948
}
5049

packages/DI/tests/ContainerTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,16 @@
1111

1212
namespace Aether\Tests\DI;
1313

14-
use PHPUnit\Framework\Attributes\CoversClass;
15-
use PHPUnit\Framework\TestCase;
1614
use Aether\DI\Container;
17-
use Aether\DI\ContainerInterface;
1815
use Aether\DI\Definition\Binding\Alias;
1916
use Aether\DI\Definition\Binding\Factory;
2017
use Aether\DI\Definition\Binding\Shared;
2118
use Aether\DI\Definition\Resolver\DefinitionResolver;
2219
use Aether\DI\Definition\Resolver\ParameterResolver;
2320
use Aether\Tests\DI\Fixtures\SampleClass;
2421
use Aether\Tests\DI\Fixtures\SampleInterface;
22+
use PHPUnit\Framework\Attributes\CoversClass;
23+
use PHPUnit\Framework\TestCase;
2524

2625
#[CoversClass(Alias::class)]
2726
#[CoversClass(Factory::class)]
@@ -32,7 +31,7 @@
3231
class ContainerTest extends TestCase
3332
{
3433
/** @psalm-suppress PropertyNotSetInConstructor */
35-
private ContainerInterface $container;
34+
private Container $container;
3635

3736
public function setUp(): void
3837
{

0 commit comments

Comments
 (0)