14 * Accepted HTTP methods for this route.
21 * Target for this route, can be anything.
27 * The name of this route, used for reversed routing
33 * Custom parameter filters for this route
36 private $filters = array();
39 * Array containing parameters passed through request URL
42 private $parameters = array();
61 * @param array $config
63 public function __construct($resource, array $config)
65 $this->url = $resource;
66 $this->config = $config;
67 $this->methods = isset($config['methods']) ? (array)$config['methods'] : array();
68 $this->target = isset($config['target']) ? $config['target'] : null;
69 $this->name = isset($config['name']) ? $config['name'] : null;
70 $this->parameters = isset($config['parameters']) ? $config['parameters'] : array();
71 $this->filters = isset($config['filters']) ? $config['filters'] : array();
72 $action = explode('::', $this->config['_controller']);
73 $this->class = isset($action[0]) ? $action[0] : null;
74 $this->action = isset($action[1]) ? $action[1] : null;
77 public function getUrl()
82 public function setUrl($url)
86 // make sure that the URL is suffixed with a forward slash
87 if (substr($url, -1) !== '/') {
94 public function getTarget()
99 public function setTarget($target)
101 $this->target = $target;
104 public function getMethods()
106 return $this->methods;
109 public function setMethods(array $methods)
111 $this->methods = $methods;
114 public function getName()
119 public function setName($name)
121 $this->name = (string)$name;
124 public function setFilters(array $filters)
126 $this->filters = $filters;
129 public function getFilters()
131 return $this->filters;
134 public function getRegex()
136 return preg_replace_callback('/(:\w+)/', array(&$this, 'substituteFilter'), $this->url);
139 private function substituteFilter($matches)
141 if (isset($matches[1], $this->filters[$matches[1]])) {
142 return $this->filters[$matches[1]];
148 public function getParameters()
150 return $this->parameters;
153 public function setParameters(array $parameters)
155 $this->parameters = $parameters;
158 public function getValidController()
160 $class = $this->class;
161 $method = $this->action;
163 if (!class_exists($class)) {
167 if (empty($method) || trim($method) === '') {
168 $method = "__invoke";
172 $classRexl = new \ReflectionClass($class);
173 } catch (\ReflectionException $ex) {
178 $classRexl->getMethod($method);
179 } catch (\ReflectionException $ex) {
183 return $this->config['_controller'];
187 * sort parameters according the the method's arguments
191 * @throws \ReflectionException
193 private function sortParameters()
195 $class = $this->class;
196 $method = $this->action;
197 $parameters = $this->parameters;
198 $arguments = array();
200 if (empty($method) || trim($method) === '') {
201 $method = "__invoke";
204 $rexl = new \ReflectionMethod($class, $method);
206 foreach ($rexl->getParameters() as $methArgs) {
207 $arg = $methArgs->getName();
209 if (array_key_exists($arg, $parameters)) {
210 $arguments[$arg] = $parameters[$arg];
212 unset($parameters[$arg]);
214 // argument is not in the parameters
215 $arguments[$arg] = null;
219 if (count($parameters) > 0) {
220 // fill the unset arguments
221 foreach ($arguments as $arg => &$v) {
223 //$key = array_keys($parameters)[0];
225 $v = array_shift($parameters);
228 if (count($parameters) <= 0) {
234 // merge the remaining parameters
235 return array_merge($arguments, $parameters);
238 public function dispatch($instance = null)
240 is_null($instance) and $instance = new $this->class();
242 $param = $this->sortParameters();
246 if (empty($this->action) || trim($this->action) === '') {
247 // __invoke on a class
248 call_user_func_array($instance, $param);
250 call_user_func_array(array($instance, $this->action), $param);
253 $result = ob_get_clean();
258 public function getAction()
260 return $this->action;
263 public function getClass()