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();
60 * @param string $pathInfo
61 * @param array $config
63 public function __construct($pathInfo, array $config)
65 $this->url = $pathInfo;
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 = isset ($this->config['_controller']) ? explode('::', $this->config['_controller']) : array();
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 $url = preg_quote($this->url);
138 return preg_replace_callback('/(\\\\(:\w+))/', array(&$this, 'substituteFilter'), $url);
141 private function substituteFilter($matches)
143 if (isset($matches[1], $this->filters[$matches[2]])) {
144 return $this->filters[$matches[2]];
150 public function getParameters()
152 return $this->parameters;
155 public function setParameters(array $parameters)
157 $this->parameters = $parameters;
160 public function getValidController()
162 $class = $this->class;
163 $method = $this->action;
165 if (!class_exists($class)) {
169 if (empty($method) || trim($method) === '') {
170 $method = "__invoke";
174 $classRexl = new \ReflectionClass($class);
175 } catch (\ReflectionException $ex) {
180 $methRexl = $classRexl->getMethod($method);
181 } catch (\ReflectionException $ex) {
185 // use only public methods
186 // to avoid calling inherited protected methods
187 if(!$methRexl->isPublic()) {
191 return $this->config['_controller'];
195 * sort parameters according the the method's arguments
199 * @throws \ReflectionException
201 private function sortParameters()
203 $class = $this->class;
204 $method = $this->action;
205 $parameters = $this->parameters;
206 $arguments = array();
208 if (empty($method) || trim($method) === '') {
209 $method = "__invoke";
212 $rexl = new \ReflectionMethod($class, $method);
214 foreach ($rexl->getParameters() as $methArgs) {
215 $arg = $methArgs->getName();
217 if (array_key_exists($arg, $parameters)) {
218 $arguments[$arg] = $parameters[$arg];
220 unset($parameters[$arg]);
222 // argument is not in the parameters
223 $arguments[$arg] = null;
227 if (count($parameters) > 0) {
228 // fill the unset arguments
229 foreach ($arguments as $arg => &$v) {
231 //$key = array_keys($parameters)[0];
233 $v = array_shift($parameters);
236 if (count($parameters) <= 0) {
242 // merge the remaining parameters
243 return array_merge($arguments, $parameters);
246 private function canBeEchoed($var)
248 return method_exists($var, '__toString') || (is_scalar($var) && !is_null($var));
251 public function dispatch($instance = null)
253 is_null($instance) and $instance = new $this->class();
255 $param = $this->sortParameters();
259 if (empty($this->action) || trim($this->action) === '') {
260 // __invoke on a class
261 $result = call_user_func_array($instance, $param);
263 $result = call_user_func_array(array($instance, $this->action), $param);
266 if ($this->canBeEchoed($result)) {
270 $buffer = ob_get_clean();
275 public function getAction()
277 return $this->action;
280 public function getClass()