Skip to content

Commit 216f37a

Browse files
committed
Introduce DependencyResolver interface
Useful for outer libraries to rely on the interface rather than on the concrete implementation itself, leaving the door open for user-land customizations.
1 parent 3206cff commit 216f37a

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace Technically\DependencyResolver\Contracts;
4+
5+
use ArgumentCountError;
6+
use InvalidArgumentException;
7+
use Psr\Container\ContainerExceptionInterface;
8+
use Technically\DependencyResolver\Exceptions\CannotAutowireArgument;
9+
use Technically\DependencyResolver\Exceptions\CannotAutowireDependencyArgument;
10+
use Technically\DependencyResolver\Exceptions\ClassCannotBeInstantiated;
11+
12+
interface DependencyResolver
13+
{
14+
/**
15+
* Resolve the given class instance from the container.
16+
*
17+
* It can be either found in the container or constructed on the fly,
18+
* recursively auto-resolving the required parameters.
19+
*
20+
* @param class-string $className
21+
* @return mixed
22+
*
23+
* @throws InvalidArgumentException If the class does not exist.
24+
* @throws ContainerExceptionInterface If an error occurs while retrieving the existing entry from the container.
25+
* @throws ClassCannotBeInstantiated If the class cannot be instantiated.
26+
* @throws CannotAutowireDependencyArgument If a dependency (of any nesting level) cannot be resolved.
27+
*/
28+
public function resolve(string $className): mixed;
29+
30+
/**
31+
* Force-construct the given class instance using container state for auto-wiring dependencies.
32+
*
33+
* Even if the container already has the instance bound,
34+
* it will still be instantiated.
35+
*
36+
* @param class-string $className
37+
* @param array<string,mixed> $bindings
38+
* @return mixed
39+
*
40+
* @throws ClassCannotBeInstantiated
41+
* @throws CannotAutowireDependencyArgument
42+
*/
43+
public function construct(string $className, array $bindings = []): mixed;
44+
45+
/**
46+
* Call the given callable with its arguments automatically wired using the container state.
47+
*
48+
* @template T
49+
* @param callable():T $callable
50+
* @param array<string,mixed> $bindings
51+
* @return T
52+
*
53+
* @throws ArgumentCountError
54+
* @throws CannotAutowireArgument
55+
*/
56+
public function call(callable $callable, array $bindings = []): mixed;
57+
}

src/DependencyResolver.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
use Psr\Container\ContainerInterface;
99
use Technically\CallableReflection\CallableReflection;
1010
use Technically\CallableReflection\Parameters\ParameterReflection;
11+
use Technically\DependencyResolver\Contracts\DependencyResolver as DependencyResolverInterface;
1112
use Technically\DependencyResolver\Exceptions\CannotAutowireArgument;
1213
use Technically\DependencyResolver\Exceptions\CannotAutowireDependencyArgument;
1314
use Technically\DependencyResolver\Exceptions\ClassCannotBeInstantiated;
1415
use Technically\DependencyResolver\Exceptions\DependencyResolutionException;
1516
use Technically\NullContainer\NullContainer;
1617

17-
final class DependencyResolver
18+
final class DependencyResolver implements DependencyResolverInterface
1819
{
1920
private ContainerInterface $container;
2021

@@ -24,7 +25,7 @@ public function __construct(?ContainerInterface $container = null)
2425
}
2526

2627
/**
27-
* @param string $className
28+
* @param class-string $className
2829
* @return mixed
2930
*
3031
* @throws InvalidArgumentException If class does not exist.
@@ -46,8 +47,8 @@ public function resolve(string $className): mixed
4647
}
4748

4849
/**
49-
* @param string $className
50-
* @param array $bindings
50+
* @param class-string $className
51+
* @param array<string,mixed> $bindings
5152
* @return mixed
5253
*
5354
* @throws ClassCannotBeInstantiated
@@ -75,9 +76,10 @@ public function construct(string $className, array $bindings = []): mixed
7576
}
7677

7778
/**
78-
* @param callable $callable
79-
* @param array $bindings
80-
* @return mixed
79+
* @template T
80+
* @param callable():T $callable
81+
* @param array<string,mixed> $bindings
82+
* @return T
8183
*
8284
* @throws ArgumentCountError
8385
* @throws CannotAutowireArgument
@@ -93,7 +95,7 @@ public function call(callable $callable, array $bindings = []): mixed
9395

9496
/**
9597
* @param ParameterReflection[] $parameters
96-
* @param array $bindings
98+
* @param array<string,mixed> $bindings
9799
* @return array
98100
*
99101
* @throws CannotAutowireArgument

0 commit comments

Comments
 (0)