-
Notifications
You must be signed in to change notification settings - Fork 0
/
UrlDispatcher.php
197 lines (154 loc) · 5.16 KB
/
UrlDispatcher.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace BaseXMS;
use BaseXMS\Stdlib\SimpleXMLElement;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
/*
* The ControllerDispatcher has the request path as in input parameter.
* Base on the input it returns a simple array containing the response code
* and the response data. It is not responsible to parse the data which comes
* from the XML database.
*
* TODO: just return a simpleXML response object containing the return code etc
* TODO: Consider a "DispatcherResult" class instead of the return array
*/
class UrlDispatcher implements ServiceLocatorAwareInterface
{
protected $requestPath;
protected $services;
private $returnFormat = '
<node>
<code>{$code}</code>
<id>{$x/@id/string()}</id>
<contentclass>{$x/@class/string()}</contentclass>
<path>{let $pathParts := $x/ancestor-or-self::*/@id/string() return if( count( $pathParts ) > 1 ) then string-join( $pathParts, "/" ) else "/"}</path>
</node>';
/**
* Dispatch the url and returns a class name and the tree context Id
*
* @param string $path
* @return multitype:number string
*/
public function dispatch( $path )
{
$this->services->get( 'accumulator' )->start( 'URL Dispatch', microtime( true ) );
$this->setRequestPath( $path );
try
{
$response = $this->getResponseByPath();
if( !$response )
{
$response = $this->getResponseByFullPath();
}
if( !$response )
{
$response = $this->getRedirectResponse();
}
if( !$response )
{
$response = $this->getRedirectResponseByFullPath();
}
if( !$response )
{
$response = new \BaseXMS\Stdlib\SimpleXMLElement( '<node><code>400</code></node>' );
}
}
catch( \Exception $e )
{
$response = new \BaseXMS\Stdlib\SimpleXMLElement( '<node><code>500</code></node>' );
}
$this->services->get( 'accumulator' )->stop( 'URL Dispatch' );
return $response;
}
protected function setRequestPath( $path )
{
// Special case for root
if( $path == '/' )
{
$pathParts = array( '' );
}
else
{
$pathParts = explode( '/', $path );
}
$this->requestPath = $pathParts;
}
protected function getResponseByPath()
{
$nodePath = '';
foreach( $this->requestPath as $part )
{
$nodePath .= '/node[accessPaths//entry[ ( @type="alt" or @type="main" ) and @path="' . $part . '"]]';
}
$query = 'let $code := 200 return let $x := ' . $nodePath . 'return if( $x ) then ' . $this->returnFormat . ' else null';
$result = $this->services->get( 'xmldb' )->execute( $query, 'simplexml' );
return $result ? $result : false;
}
protected function getResponseByFullPath()
{
$return = false;
$fullPath = implode( '/', $this->requestPath );
$nodePath = '//node[accessPaths//entry[@type="altFull" and @path="' . $fullPath . '"]]';
$query = 'let $code := 200 return let $x := ' . $nodePath . 'return if( $x ) then ' . $this->returnFormat . ' else null';
$result = $this->services->get( 'xmldb' )->execute( $query, 'simplexml' );
return $result ? $result : false;
}
public function getRedirectResponse()
{
$return = false;
// Check if it matches a redirect path
$nodePath = '';
foreach( $this->requestPath as $part )
{
$nodePath .= '/node[accessPaths//entry[ ( @type="alt" or @type="main" or @type="old" ) and @path="' . $part . '"]]';
}
$query = 'let $code := 000 return let $x := ' . $nodePath . 'return if( $x ) then ' . $this->returnFormat . ' else null';
$orgRequest = $this->services->get( 'xmldb' )->execute( $query, 'xml' );
if( $orgRequest instanceof \DOMDocument )
{
// build an array with involved node ids - reverse order
$idPathParts = array_reverse( explode( '/', $orgRequest->queryToValue( '/node/path' ) ) );
// translate requestPath
$newRequestPath = array();
foreach( array_reverse( $this->requestPath ) as $index => $part )
{
$query = '//node[@id=' . $idPathParts[ $index ] .']/accessPaths//entry[@type="old" and @path="' . $part . '"]/string()';
$newPath = $this->services->get( 'xmldb' )->execute( $query );
if( $newPath )
{
$newRequestPath[] = $newPath;
}
else
{
$newRequestPath[] = $part;
}
}
$return = new SimpleXMLElement( '<node><code>301</code><id></id><contentclass></contentclass><path>'. implode( '/', array_reverse( $newRequestPath ) ) .'</path></node>' );
}
return $return;
}
protected function getRedirectResponseByFullPath()
{
$return = false;
$fullPath = implode( '/', $this->requestPath );
$query = 'let $code := 301 return let $x := //node[accessPaths//entry[@type="oldFull" and @path="' . $fullPath . '"]] ';
$query .= 'return if( $x ) then ' . $this->returnFormat . ' else null';
$result = $this->services->get( 'xmldb' )->execute( $query, 'simplexml' );
return $result ? $result : false;
}
/**
* @param ServiceLocatorInterface $serviceLocator
*/
public function setServiceLocator( ServiceLocatorInterface $serviceLocator )
{
$this->services = $serviceLocator;
}
/**
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->services;
}
}
?>