Implement WIP routing.
authorCameron Ball <cameron@getapproved.com.au>
Fri, 12 Sep 2014 08:39:29 +0000 (16:39 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Fri, 12 Sep 2014 08:39:29 +0000 (16:39 +0800)
Controllers/IndexController.php
Services/Http/HttpRequest.php
Services/Http/IHttpRequest.php
Services/Routing/IRoute.php [new file with mode: 0644]
Services/Routing/Route.php [new file with mode: 0644]
Services/Routing/Router.php [new file with mode: 0644]
public_html/.htaccess [new file with mode: 0644]
public_html/index.php

index 3880ec7..15d4672 100644 (file)
@@ -48,7 +48,7 @@ class IndexController extends AbstractBaseController implements IDivineControlle
 //        public function getReferer();
 //        public function getUserAgent();
         $r = $this->_request;
-//        echo $r->getMethod();
+        echo $r->getPath();
         
         $this->_response->setHeader('Content-Type', 'application/json')
                         ->setBody(json_encode(array('body' => $r->getBody())))
index 2e6663e..ef68f33 100644 (file)
@@ -155,4 +155,33 @@ class HttpRequest implements IHttpRequest
     {
         return $_SERVER['HTTP_USER_AGENT'];
     }
+    
+    private function getPhysicalPath()
+    {
+        $scriptName = $_SERVER['SCRIPT_NAME']; // <-- "/foo/index.php"
+        $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"   
+        
+        // Physical path
+        if (strpos($requestUri, $scriptName) !== false) {
+            $physicalPath = $scriptName; // <-- Without rewriting
+        } else {
+            $physicalPath = str_replace('\\', '', dirname($scriptName)); // <-- With rewriting
+        }
+        
+        return $physicalPath;
+    }
+    
+    public function getPath()
+    {
+            $physicalPath = $this->getPhysicalPath();
+            $requestUri = $_SERVER['REQUEST_URI']; // <-- "/foo/bar?test=abc" or "/foo/index.php/bar?test=abc"   
+            $queryString = isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : ''; // <-- "test=abc" or ""
+
+            // Virtual path
+            $path = substr_replace($requestUri, '', 0, strlen($physicalPath)); // <-- Remove physical path
+            $path = str_replace('?' . $queryString, '', $path); // <-- Remove query string
+            $path = '/' . ltrim($path, '/'); // <-- Ensure leading slash
+            
+            return $path;
+    }
 }
\ No newline at end of file
index c12f06d..93d17d3 100644 (file)
@@ -23,4 +23,5 @@ interface IHttpRequest
     public function getReferrer();
     public function getReferer();
     public function getUserAgent();
+    public function getPath();
 }
\ No newline at end of file
diff --git a/Services/Routing/IRoute.php b/Services/Routing/IRoute.php
new file mode 100644 (file)
index 0000000..4882302
--- /dev/null
@@ -0,0 +1,10 @@
+<?php
+
+namespace Services\Routing;
+
+interface IRoute
+{
+    public function matches($uri);
+    public function supports($method);
+    public function execute();
+}
diff --git a/Services/Routing/Route.php b/Services/Routing/Route.php
new file mode 100644 (file)
index 0000000..6ebd02d
--- /dev/null
@@ -0,0 +1,62 @@
+<?php
+
+namespace Services\Routing;
+
+class Route
+{
+    private $_controllerName;
+    private $_pattern;
+    private $_methods;
+    
+    public function __construct($controllerName, $pattern, array $methods)
+    {
+        $this->_controllerName = $controllerName;
+        $this->_pattern = $pattern;
+        $this->_methods = $methods;
+    }
+    
+    public function matches($path) {
+        $argNames = array();
+
+        /*
+         * Set up a callback for preg_replace_callback. What this does is 
+         * replace the :argName style arguments with named groups to match
+         * against the resource URI. So for example:
+         * 
+         * my/:pattern/
+         * 
+         * Becomes:
+         * 
+         * my/(?P<pattern>[^/]+
+         * 
+         * Then we can feed the new regex and the URI in to preg_match to
+         * extract the variables.
+         */
+        $callback = function($m) use ($argNames) {
+            /*
+             * We save away the names of the arguments in a variable so we can
+             * loop through later and put them in $this->arguments.
+             */
+            $argNames[] = $m[1];            
+            return '(?P<' . $m[1] . '>[^/]+)';
+        };
+        
+        $patternAsRegex = preg_replace_callback('#:([\w]+)\+?#', $callback, $this->_pattern);
+        
+        if (!preg_match('#^' . $patternAsRegex . '$#', $path, $argValues)) 
+            return false;
+
+        return true;
+    }
+    
+    public function supports($method)
+    {
+        return in_array($method, $this->_methods);
+    }
+    
+    public function getControllerName()
+    {
+        $this->_controllerName;
+    }
+}
+
diff --git a/Services/Routing/Router.php b/Services/Routing/Router.php
new file mode 100644 (file)
index 0000000..3ac00fa
--- /dev/null
@@ -0,0 +1,8 @@
+<?php
+
+/* 
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
diff --git a/public_html/.htaccess b/public_html/.htaccess
new file mode 100644 (file)
index 0000000..a345710
--- /dev/null
@@ -0,0 +1,8 @@
+RewriteEngine On
+RewriteBase /
+RewriteCond %{REQUEST_FILENAME} !-f
+RewriteRule ^ index.php [QSA,L]
+
+php_value auto_prepend_file /home/cameron/vhosts/xhprof/public_html/header.php
+php_value auto_append_file /home/cameron/vhosts/xhprof/public_html/footer.php
+
index 6a786c1..606a855 100644 (file)
@@ -18,9 +18,15 @@ $containerBuilder->useAutowiring(true);
 \r
 $container = $containerBuilder->build();\r
 \r
+$route = new Services\Routing\Route('swege', '/yolo/swage', array('GET'));\r
+$request = new Services\Http\HttpRequest();\r
 \r
-$indexController = $container->get('Controllers\IndexController');\r
-$indexController->getAction();\r
+$match = $route->matches($request->getPath());\r
+\r
+var_dump($route->supports('POST'));\r
+\r
+//$indexController = $container->get('Controllers\IndexController');\r
+//$indexController->getAction();\r
 \r
 /* @var $foo Domain\Entities\Foo */\r
 //$foo = $container->get('Domain\Entities\Foo');\r