|
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
|
32 |
|
public function __construct(FileLocatorInterface $locator, DriverInterface $doctrineDriver) |
|
20
|
|
|
{ |
|
21
|
32 |
|
parent::__construct($locator); |
|
22
|
32 |
|
$this->doctrineDriver = $doctrineDriver; |
|
23
|
32 |
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* {@inheritdoc} |
|
27
|
|
|
*/ |
|
28
|
12 |
|
protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
|
29
|
|
|
{ |
|
30
|
|
|
/** @var ClassMetadata $ddrRestClassMetadata */ |
|
31
|
12 |
|
$classMetadata = $this->doctrineDriver->loadMetadataForClass($class); |
|
32
|
12 |
|
if (null === $classMetadata) { |
|
33
|
|
|
$classMetadata = new ClassMetadata($class->getName()); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
12 |
|
$config = Yaml::parse(file_get_contents($file)); |
|
37
|
12 |
|
$className = key($config); |
|
38
|
|
|
|
|
39
|
12 |
|
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
|
12 |
|
$config = $config[$className]; |
|
46
|
12 |
|
if (!is_array($config)) { |
|
47
|
|
|
$config = []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
12 |
|
if (array_key_exists('rootResource', $config) && true === $config['rootResource']) { |
|
51
|
12 |
|
$classMetadata->setRestResource(true); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
12 |
|
$propertyConfigs = []; |
|
55
|
12 |
|
if (array_key_exists('properties', $config)) { |
|
56
|
|
|
$propertyConfigs = $config['properties']; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
foreach ($class->getProperties() as $reflectionProperty) { |
|
60
|
|
|
|
|
61
|
12 |
|
$propertyMetadata = $classMetadata->getPropertyMetadata($reflectionProperty->getName()); |
|
62
|
12 |
|
if (null === $propertyMetadata) { |
|
63
|
|
|
$propertyMetadata = new PropertyMetadata($class->getName(), $reflectionProperty->getName()); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
12 |
|
if (array_key_exists($reflectionProperty->getName(), $propertyConfigs)) { |
|
67
|
|
|
$propertyConfig = $propertyConfigs[$reflectionProperty->getName()]; |
|
68
|
|
|
if (array_key_exists('puttable', $propertyConfig) && true === $propertyConfig['puttable']) { |
|
69
|
|
|
$propertyMetadata->setPuttable(true); |
|
70
|
|
|
} |
|
71
|
|
|
if (array_key_exists('postable', $propertyConfig) && true === $propertyConfig['postable']) { |
|
72
|
|
|
$propertyMetadata->setPostable(true); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
12 |
|
$classMetadata->addPropertyMetadata($propertyMetadata); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
12 |
|
return $classMetadata; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
32 |
|
protected function getExtension() |
|
85
|
|
|
{ |
|
86
|
32 |
|
return 'rest.yml'; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|