Implement more DataMapper stuff. Allow routing to pass args to controllers.
authorCameron Ball <c.ball1729@gmail.com>
Mon, 15 Sep 2014 16:28:48 +0000 (00:28 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Mon, 15 Sep 2014 16:28:48 +0000 (00:28 +0800)
Controllers/IndexController.php
Controllers/SimfileController.php
DataAccess/DataMapper/DataMapper.php
DataAccess/DataMapper/IDataMapper.php
DataAccess/StepMania/SimfileRepository.php
Services/Routing/IRoute.php
Services/Routing/IRouter.php
Services/Routing/Route.php
Services/Routing/Router.php
config/Routes.php
public_html/index.php

index 1f5d4f8..5f86846 100644 (file)
@@ -7,7 +7,7 @@ use Services\Http\IHttpResponse;
 use Services\Http\IHttpRequest;
 use Controllers\AbstractBaseController;
 
-class IndexController extends AbstractBaseController implements IDivineController
+class IndexController implements IDivineController
 {
     
     private $_content;
@@ -26,32 +26,9 @@ class IndexController extends AbstractBaseController implements IDivineControlle
         $this->_simfileRepository = $repository;
     }
         
-    public function indexAction() {
-        /* @var $simfile Domain\Entities\StepMania\ISimfile */
-//        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->getPath();
-        
+    public function indexAction() {      
         $this->_response->setHeader('Content-Type', 'application/json')
-                        ->setBody(json_encode(array('body' => $r->getBody())))
+                        ->setBody(json_encode(array('message' => 'nothing to see here')))
                         ->sendResponse();
     }
 }
index 767445d..5bf7e78 100644 (file)
@@ -31,10 +31,23 @@ class SimfileController implements IDivineController
     public function listAction()\r
     {\r
         /* @var $simfile Domain\Entities\StepMania\ISimfile */\r
-        $simfile = $this->_simfileRepository->find(1);\r
+        $simfiles = $this->_simfileRepository->findRange(1, 10);\r
+        $returnArray = array();\r
         \r
+        foreach($simfiles as $simfile)\r
+        {\r
+            $returnArray[$simfile->getTitle()] = array('artist' => $simfile->getArtist()->getName());\r
+        }\r
+        \r
+        $this->_response->setHeader('Content-Type', 'application/json')\r
+                        ->setBody(json_encode($returnArray))\r
+                        ->sendResponse();\r
+    }\r
+    \r
+    public function testAction($testArg)\r
+    {\r
         $this->_response->setHeader('Content-Type', 'application/json')\r
-                        ->setBody(json_encode(array('artist' => $simfile->getArtist()->getName())))\r
+                        ->setBody(json_encode(array('testArg' => $testArg)))\r
                         ->sendResponse();\r
     }\r
 }\r
index 544afef..6401f19 100644 (file)
@@ -28,12 +28,20 @@ class DataMapper implements IDataMapper
     \r
     public function find($id, $entity)\r
     {\r
-        $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id=%u',\r
+        return $this->findRange($id, $entity, 1);\r
+    }\r
+    \r
+    public function findRange($id, $entity, $limit)\r
+    {\r
+        $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id>=%u LIMIT %u',\r
             $this->_maps[$entity]['table'],\r
-            $id));\r
+            $id,\r
+            $limit));\r
         $statement->execute();\r
         $rows = $statement->fetchAll();\r
         \r
+        $entities = array();\r
+        \r
         foreach($rows as $row)\r
         {\r
             $className = $this->_maps[$entity]['class']; //the entity to instantiate and return\r
@@ -48,8 +56,10 @@ class DataMapper implements IDataMapper
             }\r
 \r
             $class->setId($row['id']);\r
-            return $class;\r
+            $entities[$row['id']] = $class;\r
         }\r
+        \r
+        return count($entities) > 1 ? $entities : reset($entities);\r
     }\r
     \r
     public function save(IDivineEntity $entity)\r
index 3e61b8f..987bd82 100644 (file)
@@ -6,8 +6,12 @@ use Domain\Entities\IDivineEntity;
 \r
 interface IDataMapper\r
 {\r
+    //TODO: Table is the wrong name. We actually give the implementation the entity name and it finds the table from the maps.\r
+    \r
     //find id in table and return it as an entity\r
     public function find($id, $table);\r
+    //find rows with id >= id and stop at limit\r
+    public function findRange($id, $table, $limit);\r
     //insert/update entity in table\r
     public function save(IDivineEntity $entity);\r
     //remove entity from table\r
index 451a912..26c8964 100644 (file)
@@ -19,6 +19,11 @@ class SimfileRepository implements ISimfileRepository
         return $this->dataMapper->find($id, 'Simfile');\r
     }\r
     \r
+    public function findRange($id, $limit)\r
+    {\r
+        return $this->dataMapper->findRange($id, 'Simfile', $limit);\r
+    }\r
+    \r
     public function save(ISimfile $entity) {\r
         $this->dataMapper->save($entity);\r
     }\r
index cb37bca..e06d005 100644 (file)
@@ -8,4 +8,5 @@ interface IRoute
     public function supports($method);
     public function getControllerName();
     public function getActionName();
+    public function getActionArgs();
 }
index 7c874ff..e7befd3 100644 (file)
@@ -6,4 +6,5 @@ interface IRouter
 {\r
     public function getControllerName();\r
     public function getActionName();\r
+    public function getActionArgs();\r
 }
\ No newline at end of file
index d7e0292..cfc286b 100644 (file)
@@ -10,6 +10,9 @@ class Route implements IRoute
     private $_actionName;
     private $_pattern;
     private $_methods;
+    private $_actionArgs;
+    private $_argNames;
+    private $_argValues;
     
     public function __construct($pattern, array $methods, $controllerName, $actionName = null)
     {
@@ -20,8 +23,6 @@ class Route implements IRoute
     }
     
     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
@@ -36,20 +37,19 @@ class Route implements IRoute
          * Then we can feed the new regex and the URI in to preg_match to
          * extract the variables.
          */
-        $callback = function($m) use ($argNames) {
+        $callback = function($m) {
             /*
              * 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];            
+            $this->_argNames[] = $m[1]; 
             return '(?P<' . $m[1] . '>[^/]+)';
         };
         
         $patternAsRegex = preg_replace_callback('#:([\w]+)\+?#', $callback, $this->_pattern);
-        
-        if (!preg_match('#^' . $patternAsRegex . '$#', $path, $argValues)) 
+        if (!preg_match('#^' . $patternAsRegex . '$#', $path, $this->_argValues))
             return false;
-
+        
         return true;
     }
     
@@ -67,5 +67,16 @@ class Route implements IRoute
     {
         return $this->_actionName;
     }
+    
+    public function getActionArgs()
+    {
+        $argValues = array();
+        foreach($this->_argNames as $argName)
+        {
+            $argValues[] = $this->_argValues[$argName];
+        }
+        
+        return $argValues;
+    }
 }
 
index 944d353..333f683 100644 (file)
@@ -40,6 +40,12 @@ class Router implements IRouter
         return $matchedRoute ? $matchedRoute->getActionName() : 'index';
     }
     
+    public function getActionArgs()
+    {
+        $matchedRoute = $this->findMatch();
+        return $matchedRoute ? $matchedRoute->getActionArgs() : array() ;
+    }
+    
     private function findMatch()
     {
         if($this->_matchedRoute)
index 1555cfb..a38b782 100644 (file)
@@ -5,5 +5,11 @@ return [
         'methods' => ['GET'],\r
         'controller' => 'Simfile',\r
         'action' => 'list'\r
+    ],\r
+    \r
+    '/simfiles/argTest/:testarg' => [\r
+        'methods' => ['GET'],\r
+        'controller' => 'Simfile',\r
+        'action' => 'test'\r
     ]\r
 ];\r
index def40cd..1f6b8a1 100644 (file)
@@ -13,6 +13,10 @@ $router = $container->get('Services\Routing\IRouter');
 \r
 $controllerName= $router->getControllerName();\r
 $controllerAction = $router->getActionName();\r
+$controllerActionArgs = $router->getActionArgs();\r
 \r
 $controller = $container->get('Controllers\\' . $controllerName . 'Controller' );\r
-$controller->{$controllerAction . 'Action'}();\r
+\r
+// Last thing to do, call the action on the specified controller.\r
+call_user_func(array($controller, $controllerAction . 'Action'), $controllerActionArgs);\r
+\r