|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* load definitions from a source |
|
4
|
|
|
* |
|
5
|
|
|
* This Loader implements the following strategies. |
|
6
|
|
|
* - file |
|
7
|
|
|
* - directory |
|
8
|
|
|
* - scan |
|
9
|
|
|
* - mongodb |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Graviton\GeneratorBundle\Definition\Loader; |
|
13
|
|
|
|
|
14
|
|
|
use Graviton\GeneratorBundle\Definition\Loader\Strategy\StrategyInterface; |
|
15
|
|
|
use Graviton\GeneratorBundle\Definition\JsonDefinition; |
|
16
|
|
|
use Graviton\JsonSchemaBundle\Exception\ValidationException; |
|
17
|
|
|
use Graviton\JsonSchemaBundle\Validator\InvalidJsonException; |
|
18
|
|
|
use Graviton\JsonSchemaBundle\Validator\ValidatorInterface; |
|
19
|
|
|
use JMS\Serializer\SerializerInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @author List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> |
|
23
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GNU Public License |
|
24
|
|
|
* @link http://swisscom.ch |
|
25
|
|
|
*/ |
|
26
|
|
|
class Loader implements LoaderInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var StrategyInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $strategies = []; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var SerializerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $serializer; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var ValidatorInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
private $validator; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Constructor |
|
43
|
|
|
* |
|
44
|
|
|
* @param ValidatorInterface $validator Validator |
|
45
|
|
|
* @param SerializerInterface $serializer Serializer |
|
46
|
|
|
*/ |
|
47
|
10 |
|
public function __construct(ValidatorInterface $validator, SerializerInterface $serializer) |
|
48
|
|
|
{ |
|
49
|
10 |
|
$this->validator = $validator; |
|
50
|
10 |
|
$this->serializer = $serializer; |
|
51
|
10 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* add a strategy to the loader |
|
55
|
|
|
* |
|
56
|
|
|
* @param StrategyInterface $strategy strategy to add |
|
57
|
|
|
* |
|
58
|
|
|
* @return Loader |
|
|
|
|
|
|
59
|
|
|
*/ |
|
60
|
10 |
|
public function addStrategy(StrategyInterface $strategy) |
|
61
|
|
|
{ |
|
62
|
10 |
|
$this->strategies[] = $strategy; |
|
63
|
10 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* load from input |
|
67
|
|
|
* |
|
68
|
|
|
* @param string|null $input input from command |
|
69
|
|
|
* |
|
70
|
|
|
* @return JsonDefinition[] |
|
71
|
|
|
*/ |
|
72
|
6 |
|
public function load($input) |
|
73
|
|
|
{ |
|
74
|
6 |
|
foreach ($this->strategies as $strategy) { |
|
75
|
6 |
|
if ($strategy->supports($input)) { |
|
76
|
6 |
|
return array_map([$this, 'createJsonDefinition'], $strategy->load($input)); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Deserialize JSON definition |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $json JSON code |
|
87
|
|
|
* @return JsonDefinition |
|
88
|
|
|
* @throws InvalidJsonException If JSON is invalid |
|
89
|
|
|
* @throws ValidationException If definition is not valid |
|
90
|
|
|
*/ |
|
91
|
6 |
|
protected function createJsonDefinition($json) |
|
92
|
|
|
{ |
|
93
|
6 |
|
$errors = $this->validator->validateJsonDefinition($json); |
|
94
|
4 |
|
if (!empty($errors)) { |
|
95
|
2 |
|
throw new ValidationException($errors); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
2 |
|
$definition = $this->serializer->deserialize( |
|
99
|
|
|
$json, |
|
100
|
2 |
|
'Graviton\\GeneratorBundle\\Definition\\Schema\\Definition', |
|
101
|
2 |
|
'json' |
|
102
|
|
|
); |
|
103
|
|
|
|
|
104
|
2 |
|
return new JsonDefinition($definition); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.