| <?php |
|
|
| namespace Kanboard\Core\Http; |
|
|
| use Kanboard\Core\Base; |
|
|
| |
| |
| |
| |
| |
| |
| class Route extends Base |
| { |
| |
| |
| |
| |
| |
| |
| private $activated = false; |
|
|
| |
| |
| |
| |
| |
| |
| private $paths = array(); |
|
|
| |
| |
| |
| |
| |
| |
| private $urls = array(); |
|
|
| |
| |
| |
| |
| |
| |
| public function enable() |
| { |
| $this->activated = true; |
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function addRoute($path, $controller, $action, $plugin = '') |
| { |
| if ($this->activated) { |
| $path = ltrim($path, '/'); |
| $items = explode('/', $path); |
| $params = $this->findParams($items); |
|
|
| $this->paths[] = array( |
| 'items' => $items, |
| 'count' => count($items), |
| 'controller' => $controller, |
| 'action' => $action, |
| 'plugin' => $plugin, |
| ); |
|
|
| $this->urls[$plugin][$controller][$action][] = array( |
| 'path' => $path, |
| 'params' => $params, |
| 'count' => count($params), |
| ); |
| } |
|
|
| return $this; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function findRoute($path) |
| { |
| $items = explode('/', ltrim($path, '/')); |
| $count = count($items); |
|
|
| foreach ($this->paths as $route) { |
| if ($count === $route['count']) { |
| $params = array(); |
|
|
| for ($i = 0; $i < $count; $i++) { |
| if ($route['items'][$i][0] === ':') { |
| $params[substr($route['items'][$i], 1)] = urldecode($items[$i]); |
| } elseif ($route['items'][$i] !== $items[$i]) { |
| break; |
| } |
| } |
|
|
| if ($i === $count) { |
| $this->request->setParams($params); |
| return array( |
| 'controller' => $route['controller'], |
| 'action' => $route['action'], |
| 'plugin' => $route['plugin'], |
| ); |
| } |
| } |
| } |
|
|
| return array( |
| 'controller' => 'DashboardController', |
| 'action' => 'show', |
| 'plugin' => '', |
| ); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public function findUrl($controller, $action, array $params = array(), $plugin = '') |
| { |
| if ($plugin === '' && isset($params['plugin'])) { |
| $plugin = $params['plugin']; |
| unset($params['plugin']); |
| } |
|
|
| if (! isset($this->urls[$plugin][$controller][$action])) { |
| return ''; |
| } |
|
|
| foreach ($this->urls[$plugin][$controller][$action] as $route) { |
| if (array_diff_key($params, $route['params']) === array()) { |
| $url = $route['path']; |
| $i = 0; |
|
|
| foreach ($params as $variable => $value) { |
| $value = urlencode($value); |
| $url = str_replace(':'.$variable, $value, $url); |
| $i++; |
| } |
|
|
| if ($i === $route['count']) { |
| return $url; |
| } |
| } |
| } |
|
|
| return ''; |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| public function findParams(array $items) |
| { |
| $params = array(); |
|
|
| foreach ($items as $item) { |
| if ($item !== '' && $item[0] === ':') { |
| $params[substr($item, 1)] = true; |
| } |
| } |
|
|
| return $params; |
| } |
| } |
|
|