Skip to content

Commit 9bf9d0f

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

20 files changed

Lines changed: 89 additions & 89 deletions
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
{
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
*/
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
{

0 commit comments

Comments
 (0)