1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ConfigCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015-2016 Yahoo Japan Corporation |
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 YahooJapan\ConfigCacheBundle\ConfigCache\Loader; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Yaml\Yaml; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* YamlFileLoader loads YAML files user configurations. |
18
|
|
|
*/ |
19
|
|
|
class YamlFileLoader extends Loader |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Returns true if this class supports the given resource. |
23
|
|
|
* |
24
|
|
|
* @SuppressWarnings(PHPMD.UnusedFormalParameter) |
25
|
|
|
* @param mixed $resource A resource |
26
|
|
|
* @param string $type The resource type |
27
|
|
|
* |
28
|
|
|
* @return bool true if this class supports the given resource, false otherwise |
29
|
|
|
*/ |
30
|
9 |
|
public function supports($resource, $type = null) |
31
|
|
|
{ |
32
|
9 |
|
return is_string($resource) && 'yml' === pathinfo($resource, PATHINFO_EXTENSION); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Loads internal. |
37
|
|
|
* |
38
|
|
|
* @param string $file |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
37 |
|
protected function loadFile($file) |
43
|
|
|
{ |
44
|
37 |
|
$this->validateArgument($file); |
45
|
|
|
|
46
|
|
|
try { |
47
|
34 |
|
$array = Yaml::parse(file_get_contents($file)); |
48
|
34 |
|
} catch (\Exception $e) { |
49
|
1 |
|
throw new \Exception(sprintf("Yaml parse failed[%s]", $file)); |
50
|
|
|
} |
51
|
|
|
|
52
|
33 |
|
foreach ($this->loaders as $loader) { |
53
|
5 |
|
$array = $loader->load($array); |
54
|
33 |
|
} |
55
|
|
|
|
56
|
33 |
|
return $array; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Validates a file argument. |
61
|
|
|
* |
62
|
|
|
* @param string $file |
63
|
|
|
* |
64
|
|
|
* @throws \InvalidArgumentException |
65
|
|
|
*/ |
66
|
37 |
|
protected function validateArgument($file) |
67
|
|
|
{ |
68
|
37 |
|
if (!stream_is_local($file)) { |
69
|
1 |
|
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); |
70
|
|
|
} |
71
|
|
|
|
72
|
36 |
|
if (!file_exists($file)) { |
73
|
2 |
|
throw new \InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); |
74
|
|
|
} |
75
|
34 |
|
} |
76
|
|
|
} |
77
|
|
|
|