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
|
|
|
use Symfony\Component\Yaml\Parser as YamlParser; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* The YamlDriver reads a configuration file (config.yaml) rather than annotations. |
13
|
|
|
*/ |
14
|
|
|
class YamlDriver extends PhpDriver |
15
|
|
|
{ |
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
|
|
|
* Get all the metadata class names known to this driver. |
35
|
|
|
* @return array |
36
|
|
|
* @throws DrestException |
37
|
|
|
* @throws DriverException |
38
|
|
|
*/ |
39
|
4 |
|
public function getAllClassNames() |
40
|
|
|
{ |
41
|
4 |
|
if (empty($this->classes)) { |
42
|
4 |
|
if (empty($this->paths)) { |
43
|
|
|
throw DrestException::pathToConfigFilesRequired(); |
44
|
|
|
} |
45
|
|
|
|
46
|
4 |
|
$yamlParser = new YamlParser(); |
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 = $yamlParser->parse(file_get_contents($path)); |
54
|
|
|
|
55
|
2 |
|
if($resources === false || empty($resources)) { |
56
|
1 |
|
throw DriverException::configurationFileIsInvalid('Yaml'); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$this->classes = array_merge($this->classes, (array) $resources); |
60
|
1 |
|
} |
61
|
1 |
|
} |
62
|
|
|
|
63
|
1 |
|
return array_keys($this->classes); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|