From 6f9af3659c9146a2ac1b08d70610bf96398ec073 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Tue, 22 Dec 2015 19:06:03 +0100 Subject: Added the possiblity to define custom routes from plugins --- app/Core/Http/Route.php | 188 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 app/Core/Http/Route.php (limited to 'app/Core/Http/Route.php') diff --git a/app/Core/Http/Route.php b/app/Core/Http/Route.php new file mode 100644 index 00000000..ed831467 --- /dev/null +++ b/app/Core/Http/Route.php @@ -0,0 +1,188 @@ +activated = true; + return $this; + } + + /** + * Add route + * + * @access public + * @param string $path + * @param string $controller + * @param string $action + * @param string $plugin + * @return Route + */ + 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; + } + + /** + * Find a route according to the given path + * + * @access public + * @param string $path + * @return array + */ + 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)] = $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' => 'app', + 'action' => 'index', + 'plugin' => '', + ); + } + + /** + * Find route url + * + * @access public + * @param string $controller + * @param string $action + * @param array $params + * @param string $plugin + * @return string + */ + 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) { + $url = str_replace(':'.$variable, $value, $url); + $i++; + } + + if ($i === $route['count']) { + return $url; + } + } + } + + return ''; + } + + /** + * Find url params + * + * @access public + * @param array $items + * @return array + */ + public function findParams(array $items) + { + $params = array(); + + foreach ($items as $item) { + if ($item !== '' && $item{0} === ':') { + $params[substr($item, 1)] = true; + } + } + + return $params; + } +} -- cgit v1.2.3 From 27b9b7a727de7a9608d85bce6ca94e81bbdf7ffb Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 16 Jan 2016 22:29:29 -0500 Subject: Minor code improvements --- app/Action/TaskClose.php | 2 -- app/Core/Http/Route.php | 1 - app/Core/Ldap/User.php | 2 +- app/Model/Config.php | 1 - app/Model/Group.php | 3 --- app/Model/ProjectUserRole.php | 2 +- app/ServiceProvider/EventDispatcherProvider.php | 1 - app/Validator/AuthValidator.php | 29 ++++++++----------------- 8 files changed, 11 insertions(+), 30 deletions(-) (limited to 'app/Core/Http/Route.php') diff --git a/app/Action/TaskClose.php b/app/Action/TaskClose.php index b8c5e175..cf91e83e 100644 --- a/app/Action/TaskClose.php +++ b/app/Action/TaskClose.php @@ -2,8 +2,6 @@ namespace Kanboard\Action; -use Kanboard\Model\Task; - /** * Close automatically a task * diff --git a/app/Core/Http/Route.php b/app/Core/Http/Route.php index ed831467..7836146d 100644 --- a/app/Core/Http/Route.php +++ b/app/Core/Http/Route.php @@ -2,7 +2,6 @@ namespace Kanboard\Core\Http; -use RuntimeException; use Kanboard\Core\Base; /** diff --git a/app/Core/Ldap/User.php b/app/Core/Ldap/User.php index 04c4cc7e..d36d6f34 100644 --- a/app/Core/Ldap/User.php +++ b/app/Core/Ldap/User.php @@ -40,7 +40,7 @@ class User * @access public * @param Client $client * @param string $username - * @return array + * @return LdapUserProvider */ public static function getUser(Client $client, $username) { diff --git a/app/Model/Config.php b/app/Model/Config.php index d815246f..55999310 100644 --- a/app/Model/Config.php +++ b/app/Model/Config.php @@ -4,7 +4,6 @@ namespace Kanboard\Model; use Kanboard\Core\Translator; use Kanboard\Core\Security\Token; -use Kanboard\Core\Session\SessionManager; /** * Config model diff --git a/app/Model/Group.php b/app/Model/Group.php index ce8c0284..67899503 100644 --- a/app/Model/Group.php +++ b/app/Model/Group.php @@ -2,9 +2,6 @@ namespace Kanboard\Model; -use SimpleValidator\Validator; -use SimpleValidator\Validators; - /** * Group Model * diff --git a/app/Model/ProjectUserRole.php b/app/Model/ProjectUserRole.php index 6b9c23b0..8149a253 100644 --- a/app/Model/ProjectUserRole.php +++ b/app/Model/ProjectUserRole.php @@ -28,7 +28,7 @@ class ProjectUserRole extends Base */ public function getActiveProjectsByUser($user_id) { - return $this->getProjectsByUser($user_id, $status = array(Project::ACTIVE)); + return $this->getProjectsByUser($user_id, array(Project::ACTIVE)); } /** diff --git a/app/ServiceProvider/EventDispatcherProvider.php b/app/ServiceProvider/EventDispatcherProvider.php index 8280a138..880caa41 100644 --- a/app/ServiceProvider/EventDispatcherProvider.php +++ b/app/ServiceProvider/EventDispatcherProvider.php @@ -11,7 +11,6 @@ use Kanboard\Subscriber\NotificationSubscriber; use Kanboard\Subscriber\ProjectDailySummarySubscriber; use Kanboard\Subscriber\ProjectModificationDateSubscriber; use Kanboard\Subscriber\SubtaskTimeTrackingSubscriber; -use Kanboard\Subscriber\TaskMovedDateSubscriber; use Kanboard\Subscriber\TransitionSubscriber; use Kanboard\Subscriber\RecurringTaskSubscriber; diff --git a/app/Validator/AuthValidator.php b/app/Validator/AuthValidator.php index e77a88c8..36ccdff0 100644 --- a/app/Validator/AuthValidator.php +++ b/app/Validator/AuthValidator.php @@ -23,28 +23,17 @@ class AuthValidator extends Base */ public function validateForm(array $values) { - $result = false; - $errors = array(); - - foreach (array('validateFields', 'validateLocking', 'validateCaptcha', 'validateCredentials') as $method) { - list($result, $errors) = $this->$method($values); - - if (! $result) { - break; - } - } - - return array($result, $errors); + return $this->executeValidators(array('validateFields', 'validateLocking', 'validateCaptcha', 'validateCredentials'), $values); } /** * Validate credentials syntax * - * @access private + * @access protected * @param array $values Form values * @return array $valid, $errors [0] = Success or not, [1] = List of errors */ - private function validateFields(array $values) + protected function validateFields(array $values) { $v = new Validator($values, array( new Validators\Required('username', t('The username is required')), @@ -61,11 +50,11 @@ class AuthValidator extends Base /** * Validate user locking * - * @access private + * @access protected * @param array $values Form values * @return array $valid, $errors [0] = Success or not, [1] = List of errors */ - private function validateLocking(array $values) + protected function validateLocking(array $values) { $result = true; $errors = array(); @@ -82,11 +71,11 @@ class AuthValidator extends Base /** * Validate password syntax * - * @access private + * @access protected * @param array $values Form values * @return array $valid, $errors [0] = Success or not, [1] = List of errors */ - private function validateCredentials(array $values) + protected function validateCredentials(array $values) { $result = true; $errors = array(); @@ -102,11 +91,11 @@ class AuthValidator extends Base /** * Validate captcha * - * @access private + * @access protected * @param array $values Form values * @return boolean */ - private function validateCaptcha(array $values) + protected function validateCaptcha(array $values) { $result = true; $errors = array(); -- cgit v1.2.3