From cbe1af0ab934eae9fdc02c1de76911026b75d325 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20Sessing=C3=B8?= Date: Tue, 6 Oct 2015 19:46:07 +0200 Subject: [PATCH] [FEATURE] Added ressource controller type --- src/Pecee/SimpleRouter/RouterBase.php | 2 +- src/Pecee/SimpleRouter/RouterController.php | 5 -- src/Pecee/SimpleRouter/RouterEntry.php | 5 +- src/Pecee/SimpleRouter/RouterRessource.php | 91 +++++++++++++++++++++ src/Pecee/SimpleRouter/SimpleRouter.php | 8 +- 5 files changed, 99 insertions(+), 12 deletions(-) create mode 100644 src/Pecee/SimpleRouter/RouterRessource.php diff --git a/src/Pecee/SimpleRouter/RouterBase.php b/src/Pecee/SimpleRouter/RouterBase.php index b1bc66a..3830137 100644 --- a/src/Pecee/SimpleRouter/RouterBase.php +++ b/src/Pecee/SimpleRouter/RouterBase.php @@ -22,7 +22,7 @@ class RouterBase { $this->backstack = array(); $this->controllerUrlMap = array(); $this->requestUri = $_SERVER['REQUEST_URI']; - $this->requestMethod = strtolower(isset($_GET['_method']) ? $_GET['_method'] : $_SERVER['REQUEST_METHOD']); + $this->requestMethod = ($_SERVER['REQUEST_METHOD'] != 'GET') ? 'post' : 'get'; } public function addRoute(RouterEntry $route) { diff --git a/src/Pecee/SimpleRouter/RouterController.php b/src/Pecee/SimpleRouter/RouterController.php index 856cfce..fe1a9f4 100644 --- a/src/Pecee/SimpleRouter/RouterController.php +++ b/src/Pecee/SimpleRouter/RouterController.php @@ -31,11 +31,9 @@ class RouterController extends RouterEntry { if(count($path)) { $method = (!isset($path[0]) || trim($path[0]) === '') ? self::DEFAULT_METHOD : $path[0]; - $this->method = $method; array_shift($path); - $this->parameters = $path; // Set callback @@ -43,11 +41,8 @@ class RouterController extends RouterEntry { return $this; } - } - return null; - } /** diff --git a/src/Pecee/SimpleRouter/RouterEntry.php b/src/Pecee/SimpleRouter/RouterEntry.php index cd1e893..6a8d373 100644 --- a/src/Pecee/SimpleRouter/RouterEntry.php +++ b/src/Pecee/SimpleRouter/RouterEntry.php @@ -181,12 +181,9 @@ abstract class RouterEntry { } else { // When the callback is a method $controller = explode('@', $this->getCallback()); - $className = $this->getNamespace() . '\\' . $controller[0]; - $class = $this->loadClass($className); - - $method = $requestMethod . ucfirst($controller[1]); + $method = strtolower($requestMethod) . ucfirst($controller[1]); if (!method_exists($class, $method)) { throw new RouterException(sprintf('Method %s does not exist in class %s', $method, $className), 404); diff --git a/src/Pecee/SimpleRouter/RouterRessource.php b/src/Pecee/SimpleRouter/RouterRessource.php new file mode 100644 index 0000000..be6797e --- /dev/null +++ b/src/Pecee/SimpleRouter/RouterRessource.php @@ -0,0 +1,91 @@ +url = $url; + $this->controller = $controller; + $this->parameters; + $this->postMethod = strtoupper(isset($_POST['_method']) ? $_POST['_method'] : $_SERVER['REQUEST_METHOD']); + } + + public function renderRoute($requestMethod) { + return parent::renderRoute($requestMethod); + } + + protected function call($method, $parameters) { + $this->setCallback($this->controller . '@' . $method); + $this->parameters = $parameters; + return $this; + } + + public function matchRoute($requestMethod, $url) { + $url = parse_url($url); + $url = $url['path']; + + if(strtolower($url) == strtolower($this->url) || stripos($url, $this->url) !== false) { + $url = rtrim($url, '/'); + + $strippedUrl = trim(substr($url, strlen($this->url)), '/'); + $path = explode('/', $strippedUrl); + + $args = $path; + $action = ''; + + if(count($args)) { + $action = $args[0]; + array_shift($args); + } + + if (count($path)) { + + // Delete + if($this->postMethod == 'DELETE' && $requestMethod == self::REQUEST_TYPE_POST) { + return $this->call('destroy', $args); + } + + // Update + if(in_array($this->postMethod, array('PUT', 'PATCH')) && $requestMethod == self::REQUEST_TYPE_POST) { + return $this->call('update', array_merge(array($action), $args)); + } + + // Edit + if(isset($args[0]) && strtolower($args[0]) == 'edit' && $requestMethod == self::REQUEST_TYPE_GET) { + return $this->call('edit', array_merge(array($action), array_slice($args, 1))); + } + + // Create + if(strtolower($action) == 'create' && $this->postMethod == 'GET') { + return $this->call('create', $args); + } + + // Save + if($requestMethod == 'POST') { + return $this->call('store', $args); + } + + // Show + if($action && $requestMethod == 'GET') { + return $this->call('show', array_merge(array($action), $args)); + } + + // Index + return $this->call('index', $args); + + } + } + + return null; + } + +} \ No newline at end of file diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index f30c046..e99671d 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -99,8 +99,12 @@ class SimpleRouter { return $route; } - public static function ressource($controller, $settings = array()) { - // not yet implemented + public static function ressource($url, $controller) { + $route = new RouterRessource($url, $controller); + $router = RouterBase::getInstance(); + $router->addRoute($route); + + return $route; } public function getRoute($controller = null, $parameters = null, $getParams = null) {