|
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\Exception\FileLoader\InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
|
16
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Théo FIDRY <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
final class YamlSingleFileLoader |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var YamlParser |
|
25
|
|
|
*/ |
|
26
|
|
|
private $yamlParser; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var YamlValidator |
|
30
|
|
|
*/ |
|
31
|
|
|
private $yamlValidator; |
|
32
|
|
|
|
|
33
|
10 |
|
public function __construct(YamlParser $yamlParser, YamlValidator $yamlValidator) |
|
34
|
|
|
{ |
|
35
|
10 |
|
$this->yamlParser = $yamlParser; |
|
36
|
10 |
|
$this->yamlValidator = $yamlValidator; |
|
37
|
10 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $filePath |
|
41
|
|
|
* |
|
42
|
|
|
* @return array The file content |
|
43
|
|
|
* @throws InvalidArgumentException |
|
44
|
|
|
*/ |
|
45
|
8 |
|
public function loadFile($filePath) |
|
46
|
|
|
{ |
|
47
|
8 |
|
if (false === stream_is_local($filePath)) { |
|
48
|
|
|
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $filePath)); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
8 |
|
if (false === file_exists($filePath)) { |
|
52
|
2 |
|
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $filePath)); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
6 |
|
$configuration = $this->yamlParser->parse(file_get_contents($filePath)); |
|
57
|
4 |
|
} catch (ParseException $exception) { |
|
58
|
2 |
|
throw new InvalidArgumentException( |
|
59
|
2 |
|
sprintf('The file "%s" does not contain valid YAML.', $filePath), 0, $exception |
|
60
|
1 |
|
); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
return $this->yamlValidator->validate($configuration, $filePath); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|