Add request class. Not really tested yet.
authorCameron Ball <cameron@getapproved.com.au>
Wed, 10 Sep 2014 08:15:12 +0000 (16:15 +0800)
committerCameron Ball <cameron@getapproved.com.au>
Wed, 10 Sep 2014 08:15:12 +0000 (16:15 +0800)
13 files changed:
Controllers/AbstractBaseController.php [new file with mode: 0644]
Controllers/IDivineController.php
Controllers/IndexController.php
DataAccess/DataMapper/DataMapper.php
Services/Bar.php [deleted file]
Services/BarInterface.php [deleted file]
Services/Foo.php [deleted file]
Services/Http/HttpRequest.php [new file with mode: 0644]
Services/Http/HttpResponse.php [moved from Services/HttpResponse.php with 96% similarity]
Services/Http/IHttpRequest.php [new file with mode: 0644]
Services/Http/IHttpResponse.php [moved from Services/IHttpResponse.php with 94% similarity]
Services/Http/Util.php [new file with mode: 0644]
config/DI.php

diff --git a/Controllers/AbstractBaseController.php b/Controllers/AbstractBaseController.php
new file mode 100644 (file)
index 0000000..b30837a
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace Controllers;
+
+use Controllers\IDivineController;
+use Services\Http\HttpRequest;
+use Exception;
+
+abstract class AbstractBaseController
+{
+    protected $_jsonResponse;
+    
+    //TODO: Not really used as this application probably won't have views.
+    //But hey, the intended usage is when you want a controller to not render
+    //a view. So it's there if I ever use this for anything else.
+    public function setJsonResponse($bool) {
+        if(!is_bool($bool)) {
+            throw new Exception('Not a boolean value.');
+        }
+        
+        $this->_jasonResponse = $bool;
+    }
+}
index c11b7fd..535fbea 100644 (file)
@@ -4,6 +4,6 @@ namespace Controllers;
 
 interface IDivineController
 {
-    public function setJsonResponse();
+    public function setJsonResponse($bool);
     public function getAction();
 }
index a7f0c98..3880ec7 100644 (file)
@@ -3,40 +3,55 @@
 namespace Controllers;
 
 use DataAccess\StepMania\ISimfileRepository;
-use Services\IHttpResponse;
+use Services\Http\IHttpResponse;
+use Services\Http\IHttpRequest;
+use Controllers\AbstractBaseController;
 
-class IndexController implements IDivineController
+class IndexController extends AbstractBaseController implements IDivineController
 {
     
     private $_content;
     private $_simfileRepository;
-    private $_jsonResponse;
     private $_response;
+    private $_request;
     
     //override
     public function __construct(
+        IHttpRequest $request,
         IHttpResponse $response,
         ISimfileRepository $repository
     ) {
+        $this->_request = $request;
         $this->_response = $response;
         $this->_simfileRepository = $repository;
     }
-    
-    public function setJsonResponse() {
-        $this->_jsonResponse = true;
-    }
         
     public function getAction() {
         /* @var $simfile Domain\Entities\StepMania\ISimfile */
-        $simfile = $this->_simfileRepository->find(1);
-        $modes = array();
-        /* @var $steps Domain\VOs\StepMania\IStepChart */
-        foreach ($simfile->getSteps() as $steps) {
-            $modes[] = $steps->getArtist()->getTag();
-        }
+//        public function getMethod();
+//        public function isGet();
+//        public function isPost();
+//        public function isPut();
+//        public function isDelete();
+//        public function isHead();
+//        public function isFormData();
+//        public function get();
+//        public function put();
+//        public function post();
+//        public function delete();
+//        public function cookies();
+//        public function getBody();
+//        public function getContentType();
+//        public function getHost();
+//        public function getIp();
+//        public function getReferrer();
+//        public function getReferer();
+//        public function getUserAgent();
+        $r = $this->_request;
+//        echo $r->getMethod();
         
         $this->_response->setHeader('Content-Type', 'application/json')
-                        ->setBody(json_encode($modes))
+                        ->setBody(json_encode(array('body' => $r->getBody())))
                         ->sendResponse();
     }
 }
index 8ee898d..be1bf0a 100644 (file)
@@ -22,7 +22,7 @@ class DataMapper implements IDataMapper
         $options = array(PDO::ATTR_EMULATE_PREPARES => false,\r
                          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION);\r
         \r
-        $this->_db = new PDO($dsn, $username, $password, $options);        \r
+        $this->_db = new PDO($dsn, $username, null, $options);        \r
         $this->_maps = include $maps;\r
     }\r
     \r
diff --git a/Services/Bar.php b/Services/Bar.php
deleted file mode 100644 (file)
index 47b916d..0000000
+++ /dev/null
@@ -1,25 +0,0 @@
-<?php\r
-/**\r
- * @package DIExample\r
- * @copyright 2013 Internet Brands, Inc. All Rights Reserved.\r
- */\r
-namespace Domain\Entities;\r
-\r
-/**\r
- * Bar\r
- *\r
- * @author Michael Funk <mfunk@internetbrands.com>\r
- */\r
-class Bar implements BarInterface\r
-{\r
-\r
-    /**\r
-     * just returns a hardcoded string\r
-     *\r
-     * @return string\r
-     */\r
-    public function returnMe()\r
-    {\r
-        return 'This is from the bar class';\r
-    }\r
-}
\ No newline at end of file
diff --git a/Services/BarInterface.php b/Services/BarInterface.php
deleted file mode 100644 (file)
index 4f526e1..0000000
+++ /dev/null
@@ -1,22 +0,0 @@
-<?php\r
-/**\r
- * @package DIExample\r
- * @copyright 2013 Internet Brands, Inc. All Rights Reserved.\r
- */\r
-namespace Domain\Entities;\r
-\r
-/**\r
- * BarInterface\r
- *\r
- * @author Michael Funk <mfunk@internetbrands.com>\r
- */\r
-interface BarInterface\r
-{\r
-\r
-    /**\r
-     * returnMe\r
-     *\r
-     * @return string\r
-     */\r
-    public function returnMe();\r
-}
\ No newline at end of file
diff --git a/Services/Foo.php b/Services/Foo.php
deleted file mode 100644 (file)
index abf8d25..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-<?php\r
-/**\r
- * @package DIExample\r
- * @copyright 2013 Internet Brands, Inc. All Rights Reserved.\r
- */\r
-namespace Domain\Entities;\r
-\r
-/**\r
- * Foo\r
- *\r
- * @author Michael Funk <mfunk@internetbrands.com>\r
- */\r
-class Foo\r
-{\r
-\r
-    /**\r
-     * This just holds the injected instance of Bar\r
-     * @var MikeFunk\Test\Bar\r
-     */\r
-    protected $bar;\r
-\r
-    /**\r
-     * inject Bar into $bar and then $this->bar with type hinting. The Bar\r
-     * class is bound to the BarInterface in config/di.yml\r
-     *\r
-     * @return void\r
-     */\r
-    public function __construct(BarInterface $bar)\r
-    {\r
-        $this->bar = $bar;\r
-    }\r
-\r
-    /**\r
-     * Just return the string from Bar::returnMe()\r
-     *\r
-     * @return string\r
-     */\r
-    public function returnMe()\r
-    {\r
-        return $this->bar->returnMe();\r
-    }\r
-}
\ No newline at end of file
diff --git a/Services/Http/HttpRequest.php b/Services/Http/HttpRequest.php
new file mode 100644 (file)
index 0000000..2e6663e
--- /dev/null
@@ -0,0 +1,158 @@
+<?php
+
+namespace Services\Http;
+
+use Services\Http\IHttpRequest;
+use Services\Http\Util;
+
+class HttpRequest implements IHttpRequest
+{
+    const METHOD_GET = 'GET';
+    const METHOD_POST = 'POST';
+    const METHOD_PUT = 'PUT';
+    const METHOD_DELETE = 'DELETE';
+    const METHOD_HEAD = 'HEAD';
+    
+    protected $_method;
+    protected $_cookies;
+    protected $_formDataMediaTypes = array('application/x-www-form-urlencoded');
+
+    public function __construct()
+    {
+        $this->_method = $_SERVER['REQUEST_METHOD'];
+        
+        if(isset($_SERVER['HTTP_COOKIE'])) {
+            $this->_cookies = Util::parseCookieHeader($_SERVER['HTTP_COOKIE']);
+        }
+    }
+    
+    public function getMethod()
+    {
+        return $this->_method;
+    }
+    
+    public function isGet()
+    {
+        return $this->_method == self::METHOD_GET;
+    }
+    
+    public function isPost()
+    {
+        return $this->_method == self::METHOD_POST;
+    }
+    
+    public function isPut()
+    {
+        return $this->_method == self::METHOD_PUT;
+    }
+    
+    public function isHead()
+    {
+        return $this->_method == self::METHOD_HEAD;
+    }
+    
+    public function isDelete()
+    {
+        return $this->_method == self::METHOD_DELETE;
+    }
+    
+    public function getContentType()
+    {
+        return isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : null;
+    }
+    
+    public function isFormData()
+    {
+        return in_array($this->getContentType(), $this->_formDataMediaTypes);
+    }
+    
+    public function get()
+    {
+        return !empty($_GET) ? $_GET : null;
+    }
+    
+    public function getGetElement($key)
+    {
+        return isset($_GET[$key]) ? $_GET[$key] : null;
+    }
+    
+    public function post()
+    {
+        return !empty($_POST) ? $_POST : null;
+    }
+    
+    public function getPostElement($key)
+    {
+        return isset($_POST[$key]) ? $_POST[$key] : null;
+    }
+    
+    public function put()
+    {
+        return $this->post();
+    }
+    
+    public function getPutElement($key)
+    {
+        return $this->getPostElement($key);
+    }
+    
+    public function delete()
+    {
+        return $this->post();
+    }
+    
+    public function getDeleteElement($key)
+    {
+        return $this->getPostElement($key);
+    }
+    
+    public function cookies()
+    {
+        return $this->_cookies;
+    }
+    
+    public function getCookie($key)
+    {
+        return isset($this->_cookies[$key]) ? $this->_cookies[$key] : null;
+    }
+    
+    public function getBody()
+    {
+        return @file_get_contents('php://input');
+    }
+    public function getHost()
+    {
+        if(isset($_SERVER['HTTP_HOST'])) {
+            $parts = explode(':', $_SERVER['HTTP_HOST']);
+            return $parts[0];
+        }
+        
+        return $_SERVER['SERVER_NAME'];
+    }
+    
+    public function getIp()
+    {
+        $keys = array('X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP', 'REMOTE_ADDR');
+        foreach ($keys as $key) {
+            if(isset($_SERVER[$key])) {
+                return $_SERVER[$key];
+            }
+        }
+    }
+    
+    public function getReferrer()
+    {
+        return $_SERVER['HTTP_REFERER'];
+    }
+    
+    // some people like to spell it referer due to RFC 1945
+    public function getReferer()
+    {
+        return $this->getReferer();
+    }
+    
+    public function getUserAgent()
+    {
+        return $_SERVER['HTTP_USER_AGENT'];
+    }
+}
\ No newline at end of file
similarity index 96%
rename from Services/HttpResponse.php
rename to Services/Http/HttpResponse.php
index 26c6e43..3041556 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 
-namespace Services;
+namespace Services\Http;
 
-use Services\IHttpResponse;
+use Services\Http\IHttpResponse;
 use Exception;
 
 class HttpResponse implements IHttpResponse
diff --git a/Services/Http/IHttpRequest.php b/Services/Http/IHttpRequest.php
new file mode 100644 (file)
index 0000000..c12f06d
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+namespace Services\Http;
+
+interface IHttpRequest
+{
+    public function getMethod();
+    public function isGet();
+    public function isPost();
+    public function isPut();
+    public function isDelete();
+    public function isHead();
+    public function isFormData();
+    public function get();
+    public function put();
+    public function post();
+    public function delete();
+    public function cookies();
+    public function getBody();
+    public function getContentType();
+    public function getHost();
+    public function getIp();
+    public function getReferrer();
+    public function getReferer();
+    public function getUserAgent();
+}
\ No newline at end of file
similarity index 94%
rename from Services/IHttpResponse.php
rename to Services/Http/IHttpResponse.php
index d2c8f7f..659cb7e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Services;
+namespace Services\Http;
 
 interface IHttpResponse
 {
diff --git a/Services/Http/Util.php b/Services/Http/Util.php
new file mode 100644 (file)
index 0000000..77c9634
--- /dev/null
@@ -0,0 +1,25 @@
+<?php
+
+namespace Services\Http;
+
+class Util
+{
+    public static function parseCookieHeader($header)
+    {
+        $cookies = array();
+        $header = rtrim($header, "\r\n");
+        $headerPieces = preg_split('@\s*[;,]\s*@', $header);
+        foreach ($headerPieces as $c) {
+            $cParts = explode('=', $c, 2);
+            if (count($cParts) === 2) {
+                $key = urldecode($cParts[0]);
+                $value = urldecode($cParts[1]);
+                if (!isset($cookies[$key])) {
+                    $cookies[$key] = $value;
+                }
+            }
+        }
+
+        return $cookies;
+    }
+}
index 8c4c90f..9c7b3bf 100644 (file)
@@ -5,7 +5,8 @@ return [
     'datamapper.maps' => '../config/DataMaps.php',\r
     \r
     'Domain\Entities\StepMania\ISimfile' => DI\object('Domain\Entities\StepMania\Simfile'),\r
-    'Services\IHttpResponse' => DI\object('Services\HttpResponse'),\r
+    'Services\Http\IHttpResponse' => DI\object('Services\Http\HttpResponse'),\r
+    'Services\Http\IHttpRequest' => DI\object('Services\Http\HttpRequest'),\r
     'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'),\r
     'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper')\r
         ->constructor(DI\link('datamapper.maps')),   \r