-
Notifications
You must be signed in to change notification settings - Fork 8
/
AbstractXmlDriver.php
132 lines (111 loc) · 4.42 KB
/
AbstractXmlDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* (c) FSi sp. z o.o. <info@fsi.pl>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace FSi\DoctrineExtensions\Mapping\Driver;
use DOMDocument;
use DOMXPath;
use FSi\DoctrineExtensions\Metadata\ClassMetadataInterface;
use FSi\DoctrineExtensions\Uploadable\Exception\MappingException;
use ReflectionClass;
use SimpleXmlElement;
abstract class AbstractXmlDriver extends AbstractFileDriver
{
const DOCTRINE_NAMESPACE_URI = 'http://doctrine-project.org/schemas/orm/doctrine-mapping';
const FSI_NAMESPACE_URI = 'http://fsi.pl/schemas/orm/doctrine-extensions-mapping';
protected function getFileMapping(ClassMetadataInterface $extendedClassMetadata): ?SimpleXmlElement
{
$fileLocation = $this->findMappingFile($extendedClassMetadata);
$dom = new DOMDocument();
$dom->load($fileLocation);
if (!$this->validateFile($dom)) {
throw new MappingException(sprintf(
'There are wrong mappings in XML mapping for class "%s" in file "%s"',
$extendedClassMetadata->getClassName(),
$fileLocation
));
}
$xmlElement = simplexml_load_file($fileLocation);
$xmlElement = $xmlElement->children(self::DOCTRINE_NAMESPACE_URI);
$className = $extendedClassMetadata->getClassName();
if (isset($xmlElement->entity)) {
foreach ($xmlElement->entity as $entityElement) {
if ($this->getAttribute($entityElement, 'name') === $className) {
return $entityElement;
}
}
} elseif (isset($xmlElement->{'mapped-superclass'})) {
foreach ($xmlElement->{'mapped-superclass'} as $mappedSuperClass) {
if ($this->getAttribute($mappedSuperClass, 'name') === $className) {
return $mappedSuperClass;
}
}
}
return null;
}
protected function getAttribute(SimpleXmlElement $node, string $name): ?string
{
$attributes = $node->attributes();
if (!isset($attributes[$name])) {
return null;
}
return (string) $attributes[$name];
}
private function validateFile(DOMDocument $dom): bool
{
// Schemas for validation.
$schemaLocations = [
'http://fsi.pl/schemas/orm/doctrine-extensions-mapping' => realpath(__DIR__ . '/../../../../../doctrine-extensions-mapping.xsd'),
'http://doctrine-project.org/schemas/orm/doctrine-mapping' => $this->getDoctrineSchemePath(),
];
// Elements from unknown namespaces are removed before validation.
$known = [
'http://fsi.pl/schemas/orm/doctrine-extensions-mapping',
'http://doctrine-project.org/schemas/orm/doctrine-mapping',
'http://www.w3.org/XML/1998/namespace',
'http://www.w3.org/2001/XMLSchema-instance',
];
$xpath = new DOMXPath($dom);
foreach ($xpath->query('namespace::*', $dom->documentElement) as $xmlns) {
if (in_array($xmlns->nodeValue, $known)) {
continue;
}
$domNodeList = $dom->getElementsByTagNameNS($xmlns->nodeValue, '*');
for ($i = $domNodeList->length; --$i >= 0; ) {
$element = $domNodeList->item($i);
$element->parentNode->removeChild($element);
}
}
// Importing schemas.
$imports = '';
foreach ($schemaLocations as $namespace => $location) {
$imports .= sprintf('<xsd:import namespace="%s" schemaLocation="%s" />'."\n", $namespace, $location);
}
$source = <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema xmlns="http://symfony.com/schema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://symfony.com/schema"
elementFormDefault="qualified">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
$imports
</xsd:schema>
EOF
;
return @$dom->schemaValidateSource($source);
}
private function getDoctrineSchemePath(): string
{
static $path;
if ($path) {
return $path;
}
$reflector = new ReflectionClass('Doctrine\\ORM\\UnitOfWork');
$path = realpath(dirname($reflector->getFileName()) . '/../../../doctrine-mapping.xsd');
return $path;
}
}