From 2062617659655ee3157095f097e2929ca2010df7 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Mon, 15 Sep 2014 19:52:10 +0800 Subject: [PATCH] Implement basic router and a controller for simfile API. Need to make some changes to DataMapper to allow it to return multiple etities. --- Controllers/AbstractBaseController.php | 23 ---------- Controllers/IDivineController.php | 3 +- Controllers/IndexController.php | 2 +- Controllers/SimfileController.php | 40 +++++++++++++++++ DataAccess/DataMapper/DataMapper.php | 31 +++++++------ Services/Routing/IRoute.php | 3 +- Services/Routing/IRouter.php | 9 ++++ Services/Routing/Route.php | 15 +++++-- Services/Routing/Router.php | 61 +++++++++++++++++++++++--- config/DI.php | 9 ++++ config/Routes.php | 9 ++++ public_html/.htaccess | 2 - public_html/index.php | 79 +++------------------------------- 13 files changed, 163 insertions(+), 123 deletions(-) delete mode 100644 Controllers/AbstractBaseController.php create mode 100644 Controllers/SimfileController.php create mode 100644 Services/Routing/IRouter.php create mode 100644 config/Routes.php diff --git a/Controllers/AbstractBaseController.php b/Controllers/AbstractBaseController.php deleted file mode 100644 index b30837a..0000000 --- a/Controllers/AbstractBaseController.php +++ /dev/null @@ -1,23 +0,0 @@ -_jasonResponse = $bool; - } -} diff --git a/Controllers/IDivineController.php b/Controllers/IDivineController.php index 535fbea..86065cb 100644 --- a/Controllers/IDivineController.php +++ b/Controllers/IDivineController.php @@ -4,6 +4,5 @@ namespace Controllers; interface IDivineController { - public function setJsonResponse($bool); - public function getAction(); + public function indexAction(); } diff --git a/Controllers/IndexController.php b/Controllers/IndexController.php index 15d4672..1f5d4f8 100644 --- a/Controllers/IndexController.php +++ b/Controllers/IndexController.php @@ -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 index 0000000..767445d --- /dev/null +++ b/Controllers/SimfileController.php @@ -0,0 +1,40 @@ +_request = $request; + $this->_response = $response; + $this->_simfileRepository = $repository; + } + + public function indexAction() { + ; + } + + // list simfiles + public function listAction() + { + /* @var $simfile Domain\Entities\StepMania\ISimfile */ + $simfile = $this->_simfileRepository->find(1); + + $this->_response->setHeader('Content-Type', 'application/json') + ->setBody(json_encode(array('artist' => $simfile->getArtist()->getName()))) + ->sendResponse(); + } +} diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index be1bf0a..544afef 100644 --- a/DataAccess/DataMapper/DataMapper.php +++ b/DataAccess/DataMapper/DataMapper.php @@ -22,7 +22,7 @@ class DataMapper implements IDataMapper $options = array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION); - $this->_db = new PDO($dsn, $username, null, $options); + $this->_db = new PDO($dsn, $username, $password, $options); $this->_maps = include $maps; } @@ -32,21 +32,24 @@ class DataMapper implements IDataMapper $this->_maps[$entity]['table'], $id)); $statement->execute(); - $row = $statement->fetch(); - - $className = $this->_maps[$entity]['class']; //the entity to instantiate and return - $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db); - - if(count($constructors) == 0) + $rows = $statement->fetchAll(); + + foreach($rows as $row) { - $class = new $className; - } else { - $r = new ReflectionClass($className); - $class = $r->newInstanceArgs($constructors); - } + $className = $this->_maps[$entity]['class']; //the entity to instantiate and return + $constructors = AbstractPopulationHelper::getConstrutorArray($this->_maps, $entity, $row, $this->_db); + + if(count($constructors) == 0) + { + $class = new $className; + } else { + $r = new ReflectionClass($className); + $class = $r->newInstanceArgs($constructors); + } - $class->setId($row['id']); - return $class; + $class->setId($row['id']); + return $class; + } } public function save(IDivineEntity $entity) diff --git a/Services/Routing/IRoute.php b/Services/Routing/IRoute.php index 4882302..cb37bca 100644 --- a/Services/Routing/IRoute.php +++ b/Services/Routing/IRoute.php @@ -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 index 0000000..7c874ff --- /dev/null +++ b/Services/Routing/IRouter.php @@ -0,0 +1,9 @@ +_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; } } diff --git a/Services/Routing/Router.php b/Services/Routing/Router.php index 3ac00fa..944d353 100644 --- a/Services/Routing/Router.php +++ b/Services/Routing/Router.php @@ -1,8 +1,59 @@ _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; + } + } + } +} diff --git a/config/DI.php b/config/DI.php index 9c7b3bf..3f12b93 100644 --- a/config/DI.php +++ b/config/DI.php @@ -3,11 +3,20 @@ return [ //values 'datamapper.maps' => '../config/DataMaps.php', + 'router.maps' => '../config/Routes.php', + //entites 'Domain\Entities\StepMania\ISimfile' => DI\object('Domain\Entities\StepMania\Simfile'), + + //services 'Services\Http\IHttpResponse' => DI\object('Services\Http\HttpResponse'), 'Services\Http\IHttpRequest' => DI\object('Services\Http\HttpRequest'), + 'Services\Routing\IRouter' => DI\object('Services\Routing\Router') + ->constructor(DI\link('router.maps')), + + //DA 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), 'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper') ->constructor(DI\link('datamapper.maps')), + ]; diff --git a/config/Routes.php b/config/Routes.php new file mode 100644 index 0000000..1555cfb --- /dev/null +++ b/config/Routes.php @@ -0,0 +1,9 @@ + [ + 'methods' => ['GET'], + 'controller' => 'Simfile', + 'action' => 'list' + ] +]; diff --git a/public_html/.htaccess b/public_html/.htaccess index a345710..93c4480 100644 --- a/public_html/.htaccess +++ b/public_html/.htaccess @@ -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 diff --git a/public_html/index.php b/public_html/index.php index 606a855..def40cd 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -1,83 +1,18 @@ addDefinitions('../config/DI.php'); $containerBuilder->useAutowiring(true); $container = $containerBuilder->build(); -$route = new Services\Routing\Route('swege', '/yolo/swage', array('GET')); -$request = new Services\Http\HttpRequest(); - -$match = $route->matches($request->getPath()); - -var_dump($route->supports('POST')); - -//$indexController = $container->get('Controllers\IndexController'); -//$indexController->getAction(); - -/* @var $foo Domain\Entities\Foo */ -//$foo = $container->get('Domain\Entities\Foo'); -//$foo->returnMe(); -// -//$DataMapper = new \DataAccess\DataMapper\DataMapper('../config/DataMaps.php'); -//$user = $DataMapper->find(1,'User'); -// -//$simfileFactory = new SimfileFactory(); -//$simfileBuilder = new SimfileBuilder($simfileFactory); -//$simfileStepByStepBuilder = new SimfileStepByStepBuilder($simfileBuilder); -// -//$danceMode = new DanceMode('dance-single'); -//$difficulty = new Difficulty('challenge'); -//$stepArtist = new StepArtist('Someone new fuck'); -//$artist = new Artist('A brand new awesome artist!'); -//$rating = '10'; -// -//$bpm = new BPM('256', '128'); -//$stepChart = new StepChart($danceMode, -// $difficulty, -// $stepArtist, -// $rating); -// -//$steps = array($stepChart); -// -// -//$simfile = $simfileStepByStepBuilder->With_Title('Brand New Simfile') -// ->With_Artist($artist) -// ->With_Uploader($user) -// ->With_BPM($bpm) -// ->With_BpmChanges(SimfileConstants::NO_BPM_CHANGES) -// ->With_Stops(SimfileConstants::NO_STOPS) -// ->With_FgChanges(SimfileConstants::NO_FG_CHANGES) -// ->With_BgChanges(SimfileConstants::NO_BG_CHANGES) -// ->With_Steps($steps) -// ->build(); -// -// -////$user->setId(NULL); -// -//$simfile = $DataMapper->find(1, 'Simfile'); -//$simfile->addStepChart($stepChart); -//$DataMapper->save($simfile); - - - +/* @var $router Services\Routing\IRouter */ +$router = $container->get('Services\Routing\IRouter'); -//$stepchart = $simfile->getSteps(); -//$stepchart = $stepchart[0]; -//$maps = include '../config/DataMaps.php'; -// -//$DataMapper->save($user); +$controllerName= $router->getControllerName(); +$controllerAction = $router->getActionName(); +$controller = $container->get('Controllers\\' . $controllerName . 'Controller' ); +$controller->{$controllerAction . 'Action'}(); -- 2.11.0