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\Yaml; |
13
|
|
|
|
14
|
|
|
use Fidry\LaravelYaml\DependencyInjection\Builder\ContainerBuilder; |
15
|
|
|
use Fidry\LaravelYaml\Exception\FileLoader\Exception; |
16
|
|
|
use Fidry\LaravelYaml\Exception\FileLoader\InvalidArgumentException; |
17
|
|
|
use Fidry\LaravelYaml\FileLoader\FileLoaderInterface; |
18
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\ParserInterface; |
19
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\DefinitionsParser; |
20
|
|
|
use Fidry\LaravelYaml\FileLoader\Parser\Yaml\ParametersParser; |
21
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
22
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
23
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This loader is able to load YAML files. Parsed values are interpreted and added to the {@see ContainerBuilder} to be |
27
|
|
|
* loaded to the Application later on. |
28
|
|
|
* |
29
|
|
|
* @author Théo FIDRY <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
final class YamlFileLoader implements FileLoaderInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var ContainerBuilder |
35
|
|
|
*/ |
36
|
|
|
private $container; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var FileLocatorInterface |
40
|
|
|
*/ |
41
|
|
|
private $fileLocator; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var DefinitionsParser |
45
|
|
|
*/ |
46
|
|
|
private $definitionsParser; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var ParametersParser |
50
|
|
|
*/ |
51
|
|
|
private $parametersParser; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var YamlParser |
55
|
|
|
*/ |
56
|
|
|
private $yamlParser; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var YamlValidator |
60
|
|
|
*/ |
61
|
|
|
private $yamlValidator; |
62
|
|
|
|
63
|
|
|
public function __construct( |
64
|
|
|
ContainerBuilder $container, |
65
|
|
|
FileLocatorInterface $fileLocator, |
66
|
|
|
ParserInterface $definitionsParser = null, |
67
|
|
|
ParserInterface $parametersParser = null, |
68
|
|
|
YamlParser $yamlParser = null, |
69
|
|
|
YamlValidator $yamlValidator = null |
70
|
|
|
) { |
71
|
|
|
$this->container = $container; |
72
|
|
|
$this->fileLocator = $fileLocator; |
73
|
|
|
|
74
|
|
|
$this->definitionsParser = (null === $definitionsParser) ? new DefinitionsParser() : $definitionsParser; |
|
|
|
|
75
|
|
|
$this->parametersParser = (null === $parametersParser) ? new ParametersParser() : $parametersParser; |
|
|
|
|
76
|
|
|
$this->yamlParser = (null === $yamlParser) ? new YamlParser() : $yamlParser; |
77
|
|
|
$this->yamlValidator = (null === $yamlValidator) ? new YamlValidator() : $yamlValidator; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
* |
83
|
|
|
* @param string $resource file name |
84
|
|
|
* |
85
|
|
|
* @example |
86
|
|
|
* ::load('services.yml') |
87
|
|
|
* |
88
|
|
|
* @throws InvalidArgumentException |
89
|
|
|
* @throws Exception |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function load($resource) |
93
|
|
|
{ |
94
|
|
|
$path = $this->fileLocator->locate($resource); |
95
|
|
|
$content = $this->loadFile($path); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$this->parametersParser->parse($this->container, $content, $resource); |
98
|
|
|
$this->definitionsParser->parse($this->container, $content, $resource); |
99
|
|
|
|
100
|
|
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $filePath |
105
|
|
|
* |
106
|
|
|
* @return array The file content |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
*/ |
109
|
|
|
private function loadFile($filePath) |
110
|
|
|
{ |
111
|
|
|
if (false === stream_is_local($filePath)) { |
112
|
|
|
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $filePath)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if (false === file_exists($filePath)) { |
116
|
|
|
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $filePath)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
try { |
120
|
|
|
$configuration = $this->yamlParser->parse(file_get_contents($filePath)); |
121
|
|
|
} catch (ParseException $exception) { |
122
|
|
|
throw new InvalidArgumentException( |
123
|
|
|
sprintf('The file "%s" does not contain valid YAML.', $filePath), 0, $exception |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->yamlValidator->validate($configuration, $filePath); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.