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; |
13
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder; |
15
|
|
|
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException; |
16
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface; |
17
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ResolverInterface; |
18
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Resolver\ServiceResolver; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Théo FIDRY <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class ParametersParser implements ParserInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var ResolverInterface |
27
|
|
|
*/ |
28
|
|
|
private $serviceResolver; |
29
|
|
|
|
30
|
14 |
|
public function __construct(ResolverInterface $serviceResolver = null) |
31
|
|
|
{ |
32
|
14 |
|
$this->serviceResolver = (null === $serviceResolver)? new ServiceResolver(): $serviceResolver; |
33
|
14 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Parses parameters and register them to the container. |
37
|
|
|
* |
38
|
|
|
* @param ContainerBuilder $container |
39
|
|
|
* @param array $content YAML file content |
40
|
|
|
* @param string $fileName file name |
41
|
|
|
* |
42
|
|
|
* @throws InvalidArgumentException |
43
|
|
|
*/ |
44
|
14 |
|
public function parse(ContainerBuilder $container, $content, $fileName) |
45
|
|
|
{ |
46
|
14 |
|
if (false === isset($content['parameters'])) { |
47
|
8 |
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
View Code Duplication |
if (false === is_array($content['parameters'])) { |
|
|
|
|
51
|
4 |
|
throw new InvalidArgumentException( |
52
|
4 |
|
sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $fileName) |
53
|
2 |
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
foreach ($content['parameters'] as $key => $value) { |
57
|
2 |
|
$container->setParameter(strtolower($key), $this->serviceResolver->resolve($value)); |
58
|
1 |
|
} |
59
|
2 |
|
} |
60
|
|
|
} |
61
|
|
|
|
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.