|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Container package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Miloš Đurić <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For full copyright and license information, please refer to the LICENSE file, |
|
9
|
|
|
* located at the package root folder. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Laganica\Di\Resolver; |
|
13
|
|
|
|
|
14
|
|
|
use Laganica\Di\Definition\AliasDefinition; |
|
15
|
|
|
use Laganica\Di\Definition\ClassDefinition; |
|
16
|
|
|
use Laganica\Di\Definition\ClosureDefinition; |
|
17
|
|
|
use Laganica\Di\Definition\DefinitionInterface; |
|
18
|
|
|
use Laganica\Di\Definition\FactoryDefinition; |
|
19
|
|
|
use Laganica\Di\Definition\ValueDefinition; |
|
20
|
|
|
use Laganica\Di\Exception\InvalidDefinitionException; |
|
21
|
|
|
use Psr\Container\ContainerInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class ResolverFactory |
|
25
|
|
|
* |
|
26
|
|
|
* @package Laganica\Di\Resolver |
|
27
|
|
|
*/ |
|
28
|
|
|
class ResolverFactory implements ResolverFactoryInterface |
|
29
|
|
|
{ |
|
30
|
|
|
private static $classMap = [ |
|
31
|
|
|
ClassDefinition::class => ClassResolver::class, |
|
32
|
|
|
FactoryDefinition::class => FactoryResolver::class, |
|
33
|
|
|
ValueDefinition::class => ValueResolver::class, |
|
34
|
|
|
AliasDefinition::class => AliasResolver::class, |
|
35
|
|
|
ClosureDefinition::class => ClosureResolver::class, |
|
36
|
|
|
]; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var ContainerInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $container; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var ResolverInterface[] |
|
45
|
|
|
*/ |
|
46
|
|
|
private $resolvers = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param ContainerInterface $container |
|
50
|
|
|
*/ |
|
51
|
21 |
|
public function setContainer(ContainerInterface $container): void |
|
52
|
|
|
{ |
|
53
|
21 |
|
$this->container = $container; |
|
54
|
21 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @inheritDoc |
|
58
|
|
|
*/ |
|
59
|
17 |
|
public function create(DefinitionInterface $definition): ResolverInterface |
|
60
|
|
|
{ |
|
61
|
17 |
|
$resolverClass = self::$classMap[get_class($definition)] ?? null; |
|
62
|
|
|
|
|
63
|
17 |
|
if ($resolverClass === null) { |
|
64
|
|
|
throw InvalidDefinitionException::create($definition); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
17 |
|
return $this->resolvers[$resolverClass] |
|
68
|
17 |
|
?? $this->resolvers[$resolverClass] = new $resolverClass($this->container); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|