| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
namespace DI\Definition\Resolver;
|
|
|
6 |
|
|
|
7 |
use DI\Definition\Definition;
|
|
|
8 |
use DI\Definition\InstanceDefinition;
|
|
|
9 |
use DI\DependencyException;
|
|
|
10 |
use Psr\Container\NotFoundExceptionInterface;
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* Injects dependencies on an existing instance.
|
|
|
14 |
*
|
|
|
15 |
* @template-implements DefinitionResolver<InstanceDefinition>
|
|
|
16 |
*
|
|
|
17 |
* @since 5.0
|
|
|
18 |
* @author Matthieu Napoli <matthieu@mnapoli.fr>
|
|
|
19 |
*/
|
|
|
20 |
class InstanceInjector extends ObjectCreator implements DefinitionResolver
|
|
|
21 |
{
|
|
|
22 |
/**
|
|
|
23 |
* Injects dependencies on an existing instance.
|
|
|
24 |
*
|
|
|
25 |
* @param InstanceDefinition $definition
|
|
|
26 |
* @psalm-suppress ImplementedParamTypeMismatch
|
|
|
27 |
*/
|
|
|
28 |
public function resolve(Definition $definition, array $parameters = []) : ?object
|
|
|
29 |
{
|
|
|
30 |
/** @psalm-suppress InvalidCatch */
|
|
|
31 |
try {
|
|
|
32 |
$this->injectMethodsAndProperties($definition->getInstance(), $definition->getObjectDefinition());
|
|
|
33 |
} catch (NotFoundExceptionInterface $e) {
|
|
|
34 |
$message = sprintf(
|
|
|
35 |
'Error while injecting dependencies into %s: %s',
|
|
|
36 |
get_class($definition->getInstance()),
|
|
|
37 |
$e->getMessage()
|
|
|
38 |
);
|
|
|
39 |
|
|
|
40 |
throw new DependencyException($message, 0, $e);
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
return $definition;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
public function isResolvable(Definition $definition, array $parameters = []) : bool
|
|
|
47 |
{
|
|
|
48 |
return true;
|
|
|
49 |
}
|
|
|
50 |
}
|