|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Drest\Mapping\Driver; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations; |
|
6
|
|
|
use Drest\DrestException; |
|
7
|
|
|
use Drest\Mapping\Annotation; |
|
8
|
|
|
use Drest\Mapping; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* The JsonDriver reads a configuration file (config.json) rather than utilizing annotations. |
|
12
|
|
|
*/ |
|
13
|
|
|
class JsonDriver extends PhpDriver |
|
14
|
|
|
{ |
|
15
|
|
|
protected $paths = []; |
|
16
|
|
|
|
|
17
|
5 |
|
public function __construct($paths) |
|
18
|
|
|
{ |
|
19
|
5 |
|
parent::__construct($paths); |
|
20
|
5 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Factory method for the Annotation Driver |
|
24
|
|
|
* |
|
25
|
|
|
* @param array|string $paths |
|
26
|
|
|
* @return self |
|
27
|
|
|
*/ |
|
28
|
5 |
|
public static function create($paths = []) |
|
29
|
|
|
{ |
|
30
|
5 |
|
return new self($paths); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get all the metadata class names known to this driver. |
|
36
|
|
|
* @return array |
|
37
|
|
|
* @throws DrestException |
|
38
|
|
|
* @throws DriverException |
|
39
|
|
|
*/ |
|
40
|
4 |
|
public function getAllClassNames() |
|
41
|
|
|
{ |
|
42
|
4 |
|
if (empty($this->classes)) { |
|
43
|
4 |
|
if (empty($this->paths)) { |
|
44
|
|
|
throw DrestException::pathToConfigFilesRequired(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
foreach ($this->paths as $path) |
|
48
|
|
|
{ |
|
49
|
4 |
|
if(!file_exists($path)) { |
|
50
|
2 |
|
throw DriverException::configurationFileDoesntExist($path); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
2 |
|
$resources = json_decode(file_get_contents($path), true); |
|
54
|
|
|
|
|
55
|
2 |
|
if($resources === null) { |
|
56
|
1 |
|
throw DriverException::configurationFileIsInvalid('Json'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
$entities = []; |
|
60
|
1 |
|
foreach($resources['resources'] as $resource) { |
|
61
|
1 |
|
$entity = $resource['entity']; |
|
62
|
1 |
|
$entities[$entity] = $resource; |
|
63
|
1 |
|
unset($entities[$entity]['entity']); |
|
64
|
1 |
|
} |
|
65
|
|
|
|
|
66
|
1 |
|
$this->classes = array_merge($this->classes, $entities); |
|
67
|
1 |
|
} |
|
68
|
1 |
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return array_keys($this->classes); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |