forked from dannyvankooten/PHP-Router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoute.php
More file actions
executable file
·165 lines (131 loc) · 3.35 KB
/
Route.php
File metadata and controls
executable file
·165 lines (131 loc) · 3.35 KB
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
<?php
namespace PHPRouter;
use PHPRouter\DI\ContainerInterface;
use PHPRouter\DI\InjectableInterface;
use PHPRouter\DI\Exceptions\InjectionException;
class Route
{
/**
* URL of this Route
* @var string
*/
private $_url;
/**
* Accepted HTTP methods for this route
* @var array
*/
private $_methods = array('GET', 'POST', 'PUT', 'DELETE');
/**
* Target for this route, can be anything.
* @var mixed
*/
private $_target;
/**
* The name of this route, used for reversed routing
* @var string
*/
private $_name;
/**
* Custom parameter filters for this route
* @var array
*/
private $_filters = array();
/**
* Array containing parameters passed through request URL
* @var array
*/
private $_parameters = array();
/**
* Service container
* @var object
*/
private $_container;
/**
* @param $resource
* @param array $config
*/
public function __construct($resource, array $config, ContainerInterface $container = null)
{
$this->_url = $resource;
$this->_config = $config;
$this->_container= $container;
$this->_methods = isset($config['methods']) ? $config['methods'] : array();
$this->_target = isset($config['target']) ? $config['target'] : null;
}
public function getUrl()
{
return $this->_url;
}
public function setUrl($url)
{
$url = (string) $url;
// make sure that the URL is suffixed with a forward slash
if(substr($url,-1) !== '/') $url .= '/';
$this->_url = $url;
}
public function getTarget()
{
return $this->_target;
}
public function setTarget($target)
{
$this->_target = $target;
}
public function getMethods()
{
return $this->_methods;
}
public function setMethods(array $methods)
{
$this->_methods = $methods;
}
public function getName()
{
return $this->_name;
}
public function setName($name)
{
$this->_name = (string) $name;
}
public function setFilters(array $filters)
{
$this->_filters = $filters;
}
public function getRegex()
{
return preg_replace_callback("/:(\w+)/", array(&$this, 'substituteFilter'), $this->_url);
}
private function substituteFilter($matches)
{
if (isset($matches[1]) && isset($this->_filters[$matches[1]])) {
return $this->_filters[$matches[1]];
}
return "([\w-%]+)";
}
public function getParameters()
{
return $this->_parameters;
}
/**
* @return object
*/
public function getContainer()
{
return $this->_container;
}
public function setParameters(array $parameters)
{
$this->_parameters = $parameters;
}
public function dispatch()
{
$action = explode('::', $this->_config['_controller']);
$instance = new $action[0];
if($this->getContainer() && $instance instanceof InjectableInterface){
$instance->setServiceContainer($this->getContainer());
}else{
throw new InjectionException('To inject container you need to implement InjectableInterface');
}
call_user_func_array(array($instance, $action[1]), $this->_parameters);
}
}