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
|
|
|
public function __construct(ResolverInterface $serviceResolver = null) |
31
|
|
|
{ |
32
|
|
|
$this->serviceResolver = (null === $serviceResolver)? new ServiceResolver(): $serviceResolver; |
33
|
|
|
} |
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
|
|
|
public function parse(ContainerBuilder $container, $content, $fileName) |
45
|
|
|
{ |
46
|
|
|
if (false === isset($content['parameters'])) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if (false === is_array($content['parameters'])) { |
51
|
|
|
throw new InvalidArgumentException( |
52
|
|
|
sprintf('The "parameters" key should contain an array in %s. Check your YAML syntax.', $fileName) |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
foreach ($content['parameters'] as $key => $value) { |
57
|
|
|
$container->setParameter(strtolower($key), $this->serviceResolver->resolve($value)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|