|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Bldr.io |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Aaron Scherer <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Bldr\DependencyInjection\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
16
|
|
|
use Zend\Json\Json; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Aaron Scherer <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class JsonFileLoader extends YamlFileLoader |
|
22
|
|
|
{ |
|
23
|
|
|
use SupportsTrait; |
|
24
|
|
|
|
|
25
|
|
|
private $jsonParser; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Loads the Json File |
|
29
|
|
|
* |
|
30
|
|
|
* @param string $file |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \Zend\Json\Exception\RuntimeException |
|
33
|
|
|
* @throws \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function loadFile($file) |
|
37
|
|
|
{ |
|
38
|
|
|
if (!stream_is_local($file)) { |
|
39
|
|
|
throw new InvalidArgumentException(sprintf('This is not a local file "%s".', $file)); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
if (!file_exists($file)) { |
|
43
|
|
|
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
if (null === $this->jsonParser) { |
|
47
|
|
|
$this->jsonParser = new Json(); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $this->validate($this->jsonParser->decode(file_get_contents($file), true), $file); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Validates a JSON file. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $content |
|
57
|
|
|
* @param string $file |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
* |
|
61
|
|
|
* @throws InvalidArgumentException When service file is not valid |
|
62
|
|
|
*/ |
|
63
|
|
|
private function validate($content, $file) |
|
64
|
|
|
{ |
|
65
|
|
|
if (null === $content) { |
|
66
|
|
|
return $content; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!is_array($content)) { |
|
70
|
|
|
throw new InvalidArgumentException(sprintf('The service file "%s" is not valid.', $file)); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
foreach (array_keys($content) as $namespace) { |
|
74
|
|
|
if (in_array($namespace, ['imports', 'parameters', 'services'])) { |
|
75
|
|
|
continue; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!$this->container->hasExtension($namespace)) { |
|
79
|
|
|
$extensionNamespaces = array_filter( |
|
80
|
|
|
array_map( |
|
81
|
|
|
function (ExtensionInterface $ext) { |
|
82
|
|
|
return $ext->getAlias(); |
|
83
|
|
|
}, |
|
84
|
|
|
$this->container->getExtensions() |
|
85
|
|
|
) |
|
86
|
|
|
); |
|
87
|
|
|
throw new InvalidArgumentException( |
|
88
|
|
|
sprintf( |
|
89
|
|
|
'There is no extension able to load the configuration for "%s" (in %s). ' . |
|
90
|
|
|
'Looked for namespace "%s", found %s', |
|
91
|
|
|
$namespace, |
|
92
|
|
|
$file, |
|
93
|
|
|
$namespace, |
|
94
|
|
|
$extensionNamespaces ? sprintf('"%s"', implode('", "', $extensionNamespaces)) : 'none' |
|
95
|
|
|
) |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $content; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|