1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Dontdrinkandroot\RestBundle\Metadata\Driver; |
4
|
|
|
|
5
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\ClassMetadata; |
6
|
|
|
use Dontdrinkandroot\RestBundle\Metadata\PropertyMetadata; |
7
|
|
|
use Metadata\Driver\AbstractFileDriver; |
8
|
|
|
use Metadata\Driver\DriverInterface; |
9
|
|
|
use Metadata\Driver\FileLocatorInterface; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
class YamlDriver extends AbstractFileDriver |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var DriverInterface |
16
|
|
|
*/ |
17
|
|
|
private $doctrineDriver; |
18
|
|
|
|
19
|
34 |
|
public function __construct(FileLocatorInterface $locator, DriverInterface $doctrineDriver) |
20
|
|
|
{ |
21
|
34 |
|
parent::__construct($locator); |
22
|
34 |
|
$this->doctrineDriver = $doctrineDriver; |
23
|
34 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
14 |
|
protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
29
|
|
|
{ |
30
|
|
|
/** @var ClassMetadata $ddrRestClassMetadata */ |
31
|
14 |
|
$classMetadata = $this->doctrineDriver->loadMetadataForClass($class); |
32
|
14 |
|
if (null === $classMetadata) { |
33
|
|
|
$classMetadata = new ClassMetadata($class->getName()); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
14 |
|
$config = Yaml::parse(file_get_contents($file)); |
37
|
14 |
|
$className = key($config); |
38
|
|
|
|
39
|
14 |
|
if ($className !== $class->name) { |
40
|
|
|
throw new \RuntimeException( |
41
|
|
|
sprintf('Class definition mismatch for "%s" in "%s": %s', $class->getName(), $file, key($config)) |
|
|
|
|
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
14 |
|
$config = $config[$className]; |
46
|
14 |
|
if (!is_array($config)) { |
47
|
|
|
$config = []; |
48
|
|
|
} |
49
|
|
|
|
50
|
14 |
|
if (array_key_exists('rootResource', $config) && true === $config['rootResource']) { |
51
|
14 |
|
$classMetadata->setRestResource(true); |
52
|
|
|
} |
53
|
|
|
|
54
|
14 |
|
if (array_key_exists('service', $config)) { |
55
|
4 |
|
$classMetadata->setService($config['service']); |
56
|
|
|
} |
57
|
|
|
|
58
|
14 |
|
$propertyConfigs = []; |
59
|
14 |
|
if (array_key_exists('fields', $config)) { |
60
|
|
|
$propertyConfigs = $config['fields']; |
61
|
|
|
} |
62
|
|
|
|
63
|
14 |
|
foreach ($class->getProperties() as $reflectionProperty) { |
64
|
|
|
|
65
|
12 |
|
$propertyMetadata = $classMetadata->getPropertyMetadata($reflectionProperty->getName()); |
66
|
12 |
|
if (null === $propertyMetadata) { |
67
|
|
|
$propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName()); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
12 |
|
if (array_key_exists($reflectionProperty->getName(), $propertyConfigs)) { |
71
|
|
|
|
72
|
|
|
$propertyConfig = $propertyConfigs[$reflectionProperty->getName()]; |
73
|
|
|
|
74
|
|
|
if (array_key_exists('puttable', $propertyConfig) && true === $propertyConfig['puttable']) { |
75
|
|
|
$propertyMetadata->setPuttable(true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (array_key_exists('excluded', $propertyConfig) && true === $propertyConfig['excluded']) { |
79
|
|
|
$propertyMetadata->setExcluded(true); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (array_key_exists('postable', $propertyConfig) && true === $propertyConfig['postable']) { |
83
|
|
|
$propertyMetadata->setPostable(true); |
84
|
|
|
} |
85
|
|
|
} |
86
|
12 |
|
$classMetadata->addPropertyMetadata($propertyMetadata); |
87
|
|
|
} |
88
|
|
|
|
89
|
14 |
|
return $classMetadata; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
34 |
|
protected function getExtension() |
96
|
|
|
{ |
97
|
34 |
|
return 'rest.yml'; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|