1 | <?php |
||
17 | class XmlDriver extends AbstractFileDriver |
||
18 | { |
||
19 | const NAMESPACE_URI = 'https://github.com/willdurand/Hateoas'; |
||
20 | |||
21 | /** |
||
22 | * {@inheritdoc} |
||
23 | */ |
||
24 | protected function loadMetadataFromFile(\ReflectionClass $class, $file) |
||
25 | { |
||
26 | $previous = libxml_use_internal_errors(true); |
||
27 | $root = simplexml_load_file($file); |
||
28 | libxml_use_internal_errors($previous); |
||
29 | |||
30 | if (false === $root) { |
||
31 | throw new XmlErrorException(libxml_get_last_error()); |
||
32 | } |
||
33 | |||
34 | $name = $class->getName(); |
||
35 | if (!$exists = $root->xpath("./class[@name = '" . $name . "']")) { |
||
36 | throw new \RuntimeException(sprintf('Expected metadata for class %s to be defined in %s.', $name, $file)); |
||
37 | } |
||
38 | |||
39 | $classMetadata = new ClassMetadata($name); |
||
40 | $classMetadata->fileResources[] = $file; |
||
41 | $classMetadata->fileResources[] = $class->getFileName(); |
||
42 | |||
43 | if ($exists[0]->attributes(self::NAMESPACE_URI)->providers) { |
||
44 | $providers = preg_split('/\s*,\s*/', (string) $exists[0]->attributes(self::NAMESPACE_URI)->providers); |
||
45 | |||
46 | foreach ($providers as $relationProvider) { |
||
47 | $classMetadata->addRelationProvider(new RelationProvider($relationProvider)); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | $elements = $exists[0]->children(self::NAMESPACE_URI); |
||
52 | |||
53 | foreach ($elements->relation as $relation) { |
||
54 | $name = (string) $relation->attributes('')->rel; |
||
55 | |||
56 | $href = null; |
||
57 | if (isset($relation->href)) { |
||
58 | $href = $this->createHref($relation->href, $name); |
||
59 | } |
||
60 | |||
61 | $embedded = null; |
||
62 | if (isset($relation->embedded)) { |
||
63 | $embedded = $this->createEmbedded($relation->embedded); |
||
64 | } |
||
65 | |||
66 | $attributes = array(); |
||
67 | foreach ($relation->attribute as $attribute) { |
||
68 | $attributes[(string) $attribute->attributes('')->name] = (string) $attribute->attributes('')->value; |
||
69 | } |
||
70 | |||
71 | $exclusion = isset($relation->exclusion) ? $this->parseExclusion($relation->exclusion) : null; |
||
72 | |||
73 | $classMetadata->addRelation( |
||
74 | new Relation( |
||
75 | $name, |
||
76 | $href, |
||
77 | $embedded, |
||
78 | $attributes, |
||
79 | $exclusion |
||
80 | ) |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | return $classMetadata; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | protected function getExtension() |
||
94 | |||
95 | private function parseExclusion(\SimpleXMLElement $exclusion) |
||
105 | |||
106 | private function createHref($href, $name) |
||
131 | |||
132 | private function createEmbedded($embedded) |
||
143 | } |
||
144 |