1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LaravelYaml package. |
5
|
|
|
* |
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Fidry\LaravelYaml\FileLoader\Parser\Yaml\Util; |
13
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder; |
15
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Decoration; |
16
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\DecorationInterface; |
17
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\FactoryInterface; |
18
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Factory; |
19
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\Service; |
20
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Definition\ServiceInterface; |
21
|
|
|
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException; |
22
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @author Théo FIDRY <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ServiceParser |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var AliasParser |
31
|
|
|
*/ |
32
|
|
|
private $aliasParser; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var AutowiringTypesParser |
36
|
|
|
*/ |
37
|
|
|
private $autowiringTypesParser; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var ClassParser |
41
|
|
|
*/ |
42
|
|
|
private $classParser; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var DecorationParser |
46
|
|
|
*/ |
47
|
|
|
private $decorationParser; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var FactoryParser |
51
|
|
|
*/ |
52
|
|
|
private $factoryParser; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var ResolverInterface |
56
|
|
|
*/ |
57
|
|
|
private $serviceResolver; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var TagsParser |
61
|
|
|
*/ |
62
|
|
|
private $tagsParser; |
63
|
|
|
|
64
|
|
|
public function __construct(ResolverInterface $serviceResolver) |
65
|
|
|
{ |
66
|
|
|
$this->serviceResolver = $serviceResolver; |
67
|
|
|
|
68
|
|
|
$this->aliasParser = new AliasParser(); |
69
|
|
|
$this->classParser = new ClassParser(); |
70
|
|
|
$this->tagsParser = new TagsParser(); |
71
|
|
|
$this->autowiringTypesParser = new AutowiringTypesParser(); |
72
|
|
|
$this->factoryParser = new FactoryParser($serviceResolver); |
73
|
|
|
$this->decorationParser = new DecorationParser(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Parses a service definition and register it to the container. |
78
|
|
|
* |
79
|
|
|
* @param ContainerBuilder $container |
80
|
|
|
* @param string $id |
81
|
|
|
* @param array|string $service |
82
|
|
|
* @param string $fileName file name |
83
|
|
|
* |
84
|
|
|
* @throws InvalidArgumentException |
85
|
|
|
*/ |
86
|
|
|
public function parse(ContainerBuilder $container, $id, array $service, $fileName) |
87
|
|
|
{ |
88
|
|
|
$alias = $this->aliasParser->parse($id, $service, $fileName); |
89
|
|
|
if (null !== $alias) { |
90
|
|
|
$container->addAlias($alias); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$class = $this->classParser->parse($id, $service, $fileName); |
94
|
|
|
$arguments = (isset($service['arguments'])) |
95
|
|
|
? $this->serviceResolver->resolve($service['arguments']) |
96
|
|
|
: [] |
97
|
|
|
; |
98
|
|
|
$tags = $this->tagsParser->parse($id, $service, $fileName); |
99
|
|
|
$autowiringTypes = $this->autowiringTypesParser->parse($id, $service, $fileName); |
100
|
|
|
|
101
|
|
|
$serviceDefinition = new Service($id, $class, $arguments, $autowiringTypes, $tags); |
102
|
|
|
|
103
|
|
|
if (isset($service['factory'])) { |
104
|
|
|
$serviceDefinition = $this->factoryParser->parse($serviceDefinition, $service['factory'], $fileName); |
105
|
|
|
} elseif (isset($service['decorates'])) { |
106
|
|
|
$serviceDefinition = $this->decorationParser->parse($serviceDefinition, $service, $fileName); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$container->addService($serviceDefinition); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|