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\Alias; |
16
|
|
|
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException; |
17
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Théo FIDRY <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
final class DefinitionParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ServiceParser |
26
|
|
|
*/ |
27
|
|
|
private $serviceParser; |
28
|
|
|
|
29
|
|
|
public function __construct(ResolverInterface $serviceResolver) |
30
|
|
|
{ |
31
|
|
|
$this->serviceParser = new ServiceParser($serviceResolver); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Parses a service definition and register it to the container. |
36
|
|
|
* |
37
|
|
|
* @param ContainerBuilder $container |
38
|
|
|
* @param string $id |
39
|
|
|
* @param array|string $service |
40
|
|
|
* @param string $fileName file name |
41
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
|
|
public function parse(ContainerBuilder $container, $id, $service, $fileName) |
45
|
|
|
{ |
46
|
|
|
if (is_string($service) && 0 === strpos($service, '@')) { |
47
|
|
|
$alias = new Alias($id, substr($service, 1)); |
48
|
|
|
$container->addAlias($alias); |
49
|
|
|
|
50
|
|
|
return; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
View Code Duplication |
if (false === is_array($service)) { |
|
|
|
|
54
|
|
|
throw new InvalidArgumentException( |
55
|
|
|
sprintf( |
56
|
|
|
'A service definition must be an array or a string starting with "@" but %s found for service "%s" in %s. Check your YAML syntax.', |
57
|
|
|
gettype($service), |
58
|
|
|
$id, |
59
|
|
|
$fileName |
60
|
|
|
) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->serviceParser->parse($container, $id, $service, $fileName); |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.