Implement basic router and a controller for simfile API. Need to make some changes...
authorCameron Ball <c.ball1729@gmail.com>
Mon, 15 Sep 2014 11:52:10 +0000 (19:52 +0800)
committerCameron Ball <c.ball1729@gmail.com>
Mon, 15 Sep 2014 11:52:10 +0000 (19:52 +0800)
13 files changed:
Controllers/AbstractBaseController.php [deleted file]
Controllers/IDivineController.php
Controllers/IndexController.php
Controllers/SimfileController.php [new file with mode: 0644]
DataAccess/DataMapper/DataMapper.php
Services/Routing/IRoute.php
Services/Routing/IRouter.php [new file with mode: 0644]
Services/Routing/Route.php
Services/Routing/Router.php
config/DI.php
config/Routes.php [new file with mode: 0644]
public_html/.htaccess
public_html/index.php

diff --git a/Controllers/AbstractBaseController.php b/Controllers/AbstractBaseController.php
deleted file mode 100644 (file)
index b30837a..0000000
+++ /dev/null
@@ -1,23 +0,0 @@
-<?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 535fbea..86065cb 100644 (file)
@@ -4,6 +4,5 @@ namespace Controllers;
 
 interface IDivineController
 {
-    public function setJsonResponse($bool);
-    public function getAction();
+    public function indexAction();
 }
index 15d4672..1f5d4f8 100644 (file)
@@ -26,7 +26,7 @@ class IndexController extends AbstractBaseController implements IDivineControlle
         $this->_simfileRepository = $repository;
     }
         
-    public function getAction() {
+    public function indexAction() {
         /* @var $simfile Domain\Entities\StepMania\ISimfile */
 //        public function getMethod();
 //        public function isGet();
diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php
new file mode 100644 (file)
index 0000000..767445d
--- /dev/null
@@ -0,0 +1,40 @@
+<?php\r
+\r
+namespace Controllers;\r
+\r
+use Controllers\IDivineController;\r
+use Services\Http\IHttpRequest;\r
+use Services\Http\IHttpResponse;\r
+use DataAccess\StepMania\ISimfileRepository;\r
+\r
+class SimfileController implements IDivineController\r
+{\r
+    private $_simfileRepository;\r
+    private $_response;\r
+    private $_request;\r
+    \r
+    public function __construct(\r
+        IHttpRequest $request,\r
+        IHttpResponse $response,\r
+        ISimfileRepository $repository\r
+    ) {\r
+        $this->_request = $request;\r
+        $this->_response = $response;\r
+        $this->_simfileRepository = $repository;\r
+    }\r
+    \r
+    public function indexAction() {\r
+        ;\r
+    }\r
+    \r
+    // list simfiles\r
+    public function listAction()\r
+    {\r
+        /* @var $simfile Domain\Entities\StepMania\ISimfile */\r
+        $simfile = $this->_simfileRepository->find(1);\r
+        \r
+        $this->_response->setHeader('Content-Type', 'application/json')\r
+                        ->setBody(json_encode(array('artist' => $simfile->getArtist()->getName())))\r
+                        ->sendResponse();\r
+    }\r
+}\r
index be1bf0a..544afef 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, null, $options);        \r
+        $this->_db = new PDO($dsn, $username, $password, $options);        \r
         $this->_maps = include $maps;\r
     }\r
     \r
@@ -32,21 +32,24 @@ class DataMapper implements IDataMapper
             $this->_maps[$entity]['table'],\r
             $id));\r
         $statement->execute();\r
-        $row = $statement->fetch();\r
-                \r
-        $className = $this->_maps[$entity]['class']; //the entity to instantiate and return\r
-        $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db);\r
-                \r
-        if(count($constructors) == 0)\r
+        $rows = $statement->fetchAll();\r
+        \r
+        foreach($rows as $row)\r
         {\r
-            $class = new $className;            \r
-        } else {\r
-            $r = new ReflectionClass($className);\r
-            $class = $r->newInstanceArgs($constructors);\r
-        }\r
+            $className = $this->_maps[$entity]['class']; //the entity to instantiate and return\r
+            $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db);\r
+\r
+            if(count($constructors) == 0)\r
+            {\r
+                $class = new $className;            \r
+            } else {\r
+                $r = new ReflectionClass($className);\r
+                $class = $r->newInstanceArgs($constructors);\r
+            }\r
 \r
-        $class->setId($row['id']);\r
-        return $class;\r
+            $class->setId($row['id']);\r
+            return $class;\r
+        }\r
     }\r
     \r
     public function save(IDivineEntity $entity)\r
index 4882302..cb37bca 100644 (file)
@@ -6,5 +6,6 @@ interface IRoute
 {
     public function matches($uri);
     public function supports($method);
-    public function execute();
+    public function getControllerName();
+    public function getActionName();
 }
diff --git a/Services/Routing/IRouter.php b/Services/Routing/IRouter.php
new file mode 100644 (file)
index 0000000..7c874ff
--- /dev/null
@@ -0,0 +1,9 @@
+<?php\r
+\r
+namespace Services\Routing;\r
+\r
+interface IRouter\r
+{\r
+    public function getControllerName();\r
+    public function getActionName();\r
+}
\ No newline at end of file
index 6ebd02d..d7e0292 100644 (file)
@@ -2,15 +2,19 @@
 
 namespace Services\Routing;
 
-class Route
+use Services\Routing\IRoute;
+
+class Route implements IRoute
 {
     private $_controllerName;
+    private $_actionName;
     private $_pattern;
     private $_methods;
     
-    public function __construct($controllerName, $pattern, array $methods)
+    public function __construct($pattern, array $methods, $controllerName, $actionName = null)
     {
         $this->_controllerName = $controllerName;
+        $this->_actionName = $actionName;
         $this->_pattern = $pattern;
         $this->_methods = $methods;
     }
@@ -56,7 +60,12 @@ class Route
     
     public function getControllerName()
     {
-        $this->_controllerName;
+        return $this->_controllerName;
+    }
+    
+    public function getActionName()
+    {
+        return $this->_actionName;
     }
 }
 
index 3ac00fa..944d353 100644 (file)
@@ -1,8 +1,59 @@
 <?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.
- */
+namespace Services\Routing;
 
+use Services\Routing\Route;
+use Services\Routing\IRouter;
+use Services\Http\IHttpRequest;
+
+class Router implements IRouter
+{
+    private $_maps;
+    private $_routes = array();
+    private $_request;
+    private $_matchedRoute;
+    
+    public function __construct($maps, IHttpRequest $request) {
+        $this->_request = $request;
+        $this->_maps = include $maps;
+        
+        foreach($this->_maps as $pattern => $routeInfo)
+        {
+            $methods = isset($routeInfo['methods']) ? $routeInfo['methods'] : array('GET');
+            $controller = isset($routeInfo['controller']) ? $routeInfo['controller'] : 'index';
+            $action = isset($routeInfo['action']) ? $routeInfo['action'] : 'index';
+            
+            //TODO: really I should be using a builder or a factory with DI for this but yolo.
+            $this->_routes[] = new Route($pattern, $methods, $controller, $action);
+        }
+    }
+    
+    public function getControllerName()
+    {
+        $matchedRoute = $this->findMatch();
+        return $matchedRoute ? $matchedRoute->getControllerName() : 'index';
+    }
+    
+    public function getActionName()
+    {
+        $matchedRoute = $this->findMatch();
+        return $matchedRoute ? $matchedRoute->getActionName() : 'index';
+    }
+    
+    private function findMatch()
+    {
+        if($this->_matchedRoute)
+        {
+            return $this->_matchedRoute;
+        }
+        
+        foreach($this->_routes as $route)
+        {
+            if($route->matches($this->_request->getPath()) && $route->supports($this->_request->getMethod()))
+            {
+                $this->_matchedRoute = $route;
+                return $route;
+            }
+        }
+    }
+}
index 9c7b3bf..3f12b93 100644 (file)
@@ -3,11 +3,20 @@
 return [\r
     //values\r
     'datamapper.maps' => '../config/DataMaps.php',\r
+    'router.maps' => '../config/Routes.php',\r
     \r
+    //entites\r
     'Domain\Entities\StepMania\ISimfile' => DI\object('Domain\Entities\StepMania\Simfile'),\r
+    \r
+    //services\r
     'Services\Http\IHttpResponse' => DI\object('Services\Http\HttpResponse'),\r
     'Services\Http\IHttpRequest' => DI\object('Services\Http\HttpRequest'),\r
+    'Services\Routing\IRouter' => DI\object('Services\Routing\Router')\r
+        ->constructor(DI\link('router.maps')),\r
+    \r
+    //DA\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
+   \r
 ];\r
diff --git a/config/Routes.php b/config/Routes.php
new file mode 100644 (file)
index 0000000..1555cfb
--- /dev/null
@@ -0,0 +1,9 @@
+<?php\r
+\r
+return [\r
+    '/simfiles' => [\r
+        'methods' => ['GET'],\r
+        'controller' => 'Simfile',\r
+        'action' => 'list'\r
+    ]\r
+];\r
index a345710..93c4480 100644 (file)
@@ -3,6 +3,4 @@ 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 606a855..def40cd 100644 (file)
@@ -1,83 +1,18 @@
 <?php\r
 require_once('../vendor/autoload.php');\r
 \r
-use Domain\Entities\StepMania\SimfileFactory;\r
-use Domain\Entities\StepMania\SimfileBuilder;\r
-use Domain\Entities\StepMania\SimfileStepByStepBuilder;\r
-use Domain\VOs\StepMania\BPM;\r
-use Domain\VOs\StepMania\StepChart;\r
-use Domain\VOs\StepMania\DanceMode;\r
-use Domain\VOs\StepMania\Difficulty;\r
-use Domain\VOs\StepMania\StepArtist;\r
-use Domain\VOs\StepMania\Artist;\r
-use Domain\ConstantsAndTypes\SimfileConstants;\r
-\r
+// Set up the DI container\r
 $containerBuilder = new DI\ContainerBuilder();\r
 $containerBuilder->addDefinitions('../config/DI.php');\r
 $containerBuilder->useAutowiring(true);\r
 \r
 $container = $containerBuilder->build();\r
 \r
-$route = new Services\Routing\Route('swege', '/yolo/swage', array('GET'));\r
-$request = new Services\Http\HttpRequest();\r
-\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
-//$foo->returnMe();\r
-//\r
-//$DataMapper = new \DataAccess\DataMapper\DataMapper('../config/DataMaps.php');\r
-//$user = $DataMapper->find(1,'User');\r
-//\r
-//$simfileFactory = new SimfileFactory();\r
-//$simfileBuilder = new SimfileBuilder($simfileFactory);\r
-//$simfileStepByStepBuilder = new SimfileStepByStepBuilder($simfileBuilder);\r
-//\r
-//$danceMode = new DanceMode('dance-single');\r
-//$difficulty = new Difficulty('challenge');\r
-//$stepArtist = new StepArtist('Someone new fuck');\r
-//$artist = new Artist('A brand new awesome artist!');\r
-//$rating = '10';\r
-//\r
-//$bpm = new BPM('256', '128');\r
-//$stepChart = new StepChart($danceMode,\r
-//    $difficulty,\r
-//    $stepArtist,\r
-//    $rating);\r
-//\r
-//$steps = array($stepChart);\r
-//\r
-//\r
-//$simfile = $simfileStepByStepBuilder->With_Title('Brand New Simfile')\r
-//                                    ->With_Artist($artist)\r
-//                                    ->With_Uploader($user)\r
-//                                    ->With_BPM($bpm)\r
-//                                    ->With_BpmChanges(SimfileConstants::NO_BPM_CHANGES)\r
-//                                    ->With_Stops(SimfileConstants::NO_STOPS)\r
-//                                    ->With_FgChanges(SimfileConstants::NO_FG_CHANGES)\r
-//                                    ->With_BgChanges(SimfileConstants::NO_BG_CHANGES)\r
-//                                    ->With_Steps($steps)\r
-//                                    ->build();\r
-//\r
-//\r
-////$user->setId(NULL);\r
-//\r
-//$simfile = $DataMapper->find(1, 'Simfile');\r
-//$simfile->addStepChart($stepChart);\r
-//$DataMapper->save($simfile);\r
-\r
-\r
-\r
+/* @var $router Services\Routing\IRouter */\r
+$router = $container->get('Services\Routing\IRouter');\r
 \r
-//$stepchart = $simfile->getSteps();\r
-//$stepchart = $stepchart[0];\r
-//$maps = include '../config/DataMaps.php';\r
-//\r
-//$DataMapper->save($user);\r
+$controllerName= $router->getControllerName();\r
+$controllerAction = $router->getActionName();\r
 \r
+$controller = $container->get('Controllers\\' . $controllerName . 'Controller' );\r
+$controller->{$controllerAction . 'Action'}();\r