forked from symfony/symfony
/
XmlFileLoader.php
170 lines (145 loc) · 5.49 KB
/
XmlFileLoader.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
namespace Symfony\Components\Routing\Loader;
use Symfony\Components\Routing\RouteCollection;
use Symfony\Components\Routing\Route;
use Symfony\Components\Routing\FileResource;
/*
* This file is part of the Symfony framework.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
/**
* XmlFileLoader loads XML routing files.
*
* @package Symfony
* @subpackage Components_Routing
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
*/
class XmlFileLoader extends FileLoader
{
/**
* Loads an XML file.
*
* @param string $file A XML file path
*
* @return RouteCollection A RouteCollection instance
*
* @throws \InvalidArgumentException When a tag can't be parsed
*/
public function load($file)
{
$path = $this->findFile($file);
$xml = $this->loadFile($path);
$collection = new RouteCollection();
$collection->addResource(new FileResource($path));
// process routes and imports
foreach ($xml->documentElement->childNodes as $node) {
if (!$node instanceof \DOMElement) {
continue;
}
switch ($node->tagName) {
case 'route':
$this->parseRoute($collection, $node, $path);
break;
case 'import':
$this->parseImport($collection, $node, $path);
break;
default:
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
}
}
return $collection;
}
protected function parseRoute(RouteCollection $collection, $definition, $file)
{
$defaults = array();
$requirements = array();
$options = array();
foreach ($definition->childNodes as $node) {
if (!$node instanceof \DOMElement) {
continue;
}
switch ($node->tagName) {
case 'default':
$defaults[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
break;
case 'option':
$options[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
break;
case 'requirement':
$requirements[(string) $node->getAttribute('key')] = trim((string) $node->nodeValue);
break;
default:
throw new \InvalidArgumentException(sprintf('Unable to parse tag "%s"', $node->tagName));
}
}
$route = new Route((string) $definition->getAttribute('pattern'), $defaults, $requirements, $options);
$collection->addRoute((string) $definition->getAttribute('id'), $route);
}
protected function parseImport(RouteCollection $collection, $node, $file)
{
$class = null;
if ($node->hasAttribute('class') && $import->getAttribute('class') !== get_class($this)) {
$class = (string) $node->getAttribute('class');
} else {
// try to detect loader with the extension
switch (pathinfo((string) $node->getAttribute('resource'), PATHINFO_EXTENSION)) {
case 'yml':
$class = 'Symfony\\Components\\Routing\\Loader\\YamlFileLoader';
break;
}
}
$loader = null === $class ? $this : new $class($this->paths);
$importedFile = $this->getAbsolutePath((string) $node->getAttribute('resource'), dirname($file));
$collection->addCollection($loader->load($importedFile), (string) $node->getAttribute('prefix'));
}
/**
* @throws \InvalidArgumentException When loading of XML file returns error
*/
protected function loadFile($path)
{
$dom = new \DOMDocument();
libxml_use_internal_errors(true);
if (!$dom->load($path, LIBXML_COMPACT)) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
}
$dom->validateOnParse = true;
$dom->normalizeDocument();
libxml_use_internal_errors(false);
$this->validate($dom, $path);
return $dom;
}
/**
* @throws \InvalidArgumentException When xml doesn't validate its xsd schema
*/
protected function validate($dom, $file)
{
$parts = explode('/', str_replace('\\', '/', __DIR__.'/schema/routing/routing-1.0.xsd'));
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts) : '';
$location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
$current = libxml_use_internal_errors(true);
if (!$dom->schemaValidate($location)) {
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
}
libxml_use_internal_errors($current);
}
protected function getXmlErrors()
{
$errors = array();
foreach (libxml_get_errors() as $error) {
$errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
$error->code,
trim($error->message),
$error->file ? $error->file : 'n/a',
$error->line,
$error->column
);
}
libxml_clear_errors();
return $errors;
}
}