1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cmobi\RabbitmqBundle\Routing\Loader; |
4
|
|
|
|
5
|
|
|
use Cmobi\RabbitmqBundle\Routing\Method; |
6
|
|
|
use Cmobi\RabbitmqBundle\Routing\MethodCollection; |
7
|
|
|
use Symfony\Component\Config\Loader\FileLoader; |
8
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
9
|
|
|
use Symfony\Component\Yaml\Exception\ParseException; |
10
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
11
|
|
|
|
12
|
|
|
class YamlRpcLoader extends FileLoader |
13
|
|
|
{ |
14
|
|
|
private static $availableKeys = array( |
15
|
|
|
'method', 'type', 'defaults', 'resource' |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
private $yamlParser; |
19
|
|
|
|
20
|
|
|
public function load($file, $type = null) |
21
|
|
|
{ |
22
|
|
|
$path = $this->locator->locate($file); |
23
|
|
|
|
24
|
|
|
if (!stream_is_local($path)) { |
25
|
|
|
throw new \InvalidArgumentException(sprintf('This is not a local file "%s".', $path)); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
if (!file_exists($path)) { |
29
|
|
|
throw new \InvalidArgumentException(sprintf('File "%s" not found.', $path)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if (null === $this->yamlParser) { |
33
|
|
|
$this->yamlParser = new YamlParser(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
try { |
37
|
|
|
$parsedConfig = $this->yamlParser->parse(file_get_contents($path)); |
38
|
|
|
} catch (ParseException $e) { |
39
|
|
|
throw new \InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $path), 0, $e); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$collection = new MethodCollection(); |
43
|
|
|
$collection->addResource(new FileResource($path)); |
|
|
|
|
44
|
|
|
|
45
|
|
|
// empty file |
46
|
|
|
if (null === $parsedConfig) { |
47
|
|
|
return $collection; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// not an array |
51
|
|
|
if (!is_array($parsedConfig)) { |
52
|
|
|
throw new \InvalidArgumentException(sprintf('The file "%s" must contain a YAML array.', $path)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
foreach ($parsedConfig as $name => $config) { |
56
|
|
|
|
57
|
|
|
$this->validate($config, $name, $path); |
58
|
|
|
|
59
|
|
|
if (isset($config['resource'])) { |
60
|
|
|
$this->parseImport($collection, $config, $path, $file); |
61
|
|
|
} else { |
62
|
|
|
$this->parseRoute($collection, $name, $config); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $collection; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function parseImport(MethodCollection $collection, array $config, $path, $file) |
70
|
|
|
{ |
71
|
|
|
$defaults = []; |
72
|
|
|
|
73
|
|
|
if (isset($config['defaults'])) { |
74
|
|
|
$defaults = $config['defaults']; |
75
|
|
|
} |
76
|
|
|
$type = null; |
77
|
|
|
|
78
|
|
|
if (isset($config['type'])) { |
79
|
|
|
$type = $config['type']; |
80
|
|
|
} |
81
|
|
|
$this->setCurrentDir(dirname($path)); |
82
|
|
|
$subCollection = $this->import($config['resource'], $type, false, $file); |
83
|
|
|
$subCollection->addDefaults($defaults); |
84
|
|
|
|
85
|
|
|
$collection->addCollection($subCollection); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function parseRoute(MethodCollection $collection, $name, array $config) |
89
|
|
|
{ |
90
|
|
|
$defaults = []; |
91
|
|
|
|
92
|
|
|
if (isset($config['defaults'])) { |
93
|
|
|
$defaults = $config['defaults']; |
94
|
|
|
} |
95
|
|
|
$route = new Method(null, $config['method'], $defaults); |
96
|
|
|
$collection->add($name, $route); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
protected function validate($config, $name, $path) |
100
|
|
|
{ |
101
|
|
|
if (!is_array($config)) { |
102
|
|
|
throw new \InvalidArgumentException(sprintf('The definition of "%s" in "%s" must be a YAML array.', $name, $path)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($extraKeys = array_diff(array_keys($config), self::$availableKeys)) { |
106
|
|
|
throw new \InvalidArgumentException(sprintf( |
107
|
|
|
'The routing file "%s" contains unsupported keys for "%s": "%s". Expected one of: "%s".', |
108
|
|
|
$path, $name, implode('", "', $extraKeys), implode('", "', self::$availableKeys) |
109
|
|
|
)); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
View Code Duplication |
if (isset($config['resource']) && isset($config['method'])) { |
|
|
|
|
113
|
|
|
throw new \InvalidArgumentException(sprintf( |
114
|
|
|
'The routing file "%s" must not specify both the "resource" key and the "method" key for "%s". Choose between an import and a route definition.', |
115
|
|
|
$path, $name |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
View Code Duplication |
if (!isset($config['resource']) && isset($config['type'])) { |
|
|
|
|
120
|
|
|
throw new \InvalidArgumentException(sprintf( |
121
|
|
|
'The "type" key for the route definition "%s" in "%s" is unsupported. It is only available for imports in combination with the "resource" key.', |
122
|
|
|
$name, $path |
123
|
|
|
)); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function supports($resource, $type = null) |
131
|
|
|
{ |
132
|
|
|
return is_string($resource) && in_array(pathinfo($resource, PATHINFO_EXTENSION), array('yml', 'yaml'), true) && (!$type || 'yaml' === $type); |
|
|
|
|
133
|
|
|
} |
134
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.