From 0549f39194b9cda858111cb43d59d604ca406497 Mon Sep 17 00:00:00 2001 From: Cameron Ball Date: Tue, 16 Sep 2014 00:28:48 +0800 Subject: [PATCH] Implement more DataMapper stuff. Allow routing to pass args to controllers. --- Controllers/IndexController.php | 29 +++-------------------------- Controllers/SimfileController.php | 17 +++++++++++++++-- DataAccess/DataMapper/DataMapper.php | 16 +++++++++++++--- DataAccess/DataMapper/IDataMapper.php | 4 ++++ DataAccess/StepMania/SimfileRepository.php | 5 +++++ Services/Routing/IRoute.php | 1 + Services/Routing/IRouter.php | 1 + Services/Routing/Route.php | 25 ++++++++++++++++++------- Services/Routing/Router.php | 6 ++++++ config/Routes.php | 6 ++++++ public_html/index.php | 6 +++++- 11 files changed, 77 insertions(+), 39 deletions(-) diff --git a/Controllers/IndexController.php b/Controllers/IndexController.php index 1f5d4f8..5f86846 100644 --- a/Controllers/IndexController.php +++ b/Controllers/IndexController.php @@ -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(); } } diff --git a/Controllers/SimfileController.php b/Controllers/SimfileController.php index 767445d..5bf7e78 100644 --- a/Controllers/SimfileController.php +++ b/Controllers/SimfileController.php @@ -31,10 +31,23 @@ class SimfileController implements IDivineController public function listAction() { /* @var $simfile Domain\Entities\StepMania\ISimfile */ - $simfile = $this->_simfileRepository->find(1); + $simfiles = $this->_simfileRepository->findRange(1, 10); + $returnArray = array(); + foreach($simfiles as $simfile) + { + $returnArray[$simfile->getTitle()] = array('artist' => $simfile->getArtist()->getName()); + } + + $this->_response->setHeader('Content-Type', 'application/json') + ->setBody(json_encode($returnArray)) + ->sendResponse(); + } + + public function testAction($testArg) + { $this->_response->setHeader('Content-Type', 'application/json') - ->setBody(json_encode(array('artist' => $simfile->getArtist()->getName()))) + ->setBody(json_encode(array('testArg' => $testArg))) ->sendResponse(); } } diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index 544afef..6401f19 100644 --- a/DataAccess/DataMapper/DataMapper.php +++ b/DataAccess/DataMapper/DataMapper.php @@ -28,12 +28,20 @@ class DataMapper implements IDataMapper public function find($id, $entity) { - $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id=%u', + return $this->findRange($id, $entity, 1); + } + + public function findRange($id, $entity, $limit) + { + $statement = $this->_db->prepare(sprintf('SELECT * from %s WHERE id>=%u LIMIT %u', $this->_maps[$entity]['table'], - $id)); + $id, + $limit)); $statement->execute(); $rows = $statement->fetchAll(); + $entities = array(); + foreach($rows as $row) { $className = $this->_maps[$entity]['class']; //the entity to instantiate and return @@ -48,8 +56,10 @@ class DataMapper implements IDataMapper } $class->setId($row['id']); - return $class; + $entities[$row['id']] = $class; } + + return count($entities) > 1 ? $entities : reset($entities); } public function save(IDivineEntity $entity) diff --git a/DataAccess/DataMapper/IDataMapper.php b/DataAccess/DataMapper/IDataMapper.php index 3e61b8f..987bd82 100644 --- a/DataAccess/DataMapper/IDataMapper.php +++ b/DataAccess/DataMapper/IDataMapper.php @@ -6,8 +6,12 @@ use Domain\Entities\IDivineEntity; interface IDataMapper { + //TODO: Table is the wrong name. We actually give the implementation the entity name and it finds the table from the maps. + //find id in table and return it as an entity public function find($id, $table); + //find rows with id >= id and stop at limit + public function findRange($id, $table, $limit); //insert/update entity in table public function save(IDivineEntity $entity); //remove entity from table diff --git a/DataAccess/StepMania/SimfileRepository.php b/DataAccess/StepMania/SimfileRepository.php index 451a912..26c8964 100644 --- a/DataAccess/StepMania/SimfileRepository.php +++ b/DataAccess/StepMania/SimfileRepository.php @@ -19,6 +19,11 @@ class SimfileRepository implements ISimfileRepository return $this->dataMapper->find($id, 'Simfile'); } + public function findRange($id, $limit) + { + return $this->dataMapper->findRange($id, 'Simfile', $limit); + } + public function save(ISimfile $entity) { $this->dataMapper->save($entity); } diff --git a/Services/Routing/IRoute.php b/Services/Routing/IRoute.php index cb37bca..e06d005 100644 --- a/Services/Routing/IRoute.php +++ b/Services/Routing/IRoute.php @@ -8,4 +8,5 @@ interface IRoute public function supports($method); public function getControllerName(); public function getActionName(); + public function getActionArgs(); } diff --git a/Services/Routing/IRouter.php b/Services/Routing/IRouter.php index 7c874ff..e7befd3 100644 --- a/Services/Routing/IRouter.php +++ b/Services/Routing/IRouter.php @@ -6,4 +6,5 @@ interface IRouter { public function getControllerName(); public function getActionName(); + public function getActionArgs(); } \ No newline at end of file diff --git a/Services/Routing/Route.php b/Services/Routing/Route.php index d7e0292..cfc286b 100644 --- a/Services/Routing/Route.php +++ b/Services/Routing/Route.php @@ -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; + } } diff --git a/Services/Routing/Router.php b/Services/Routing/Router.php index 944d353..333f683 100644 --- a/Services/Routing/Router.php +++ b/Services/Routing/Router.php @@ -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) diff --git a/config/Routes.php b/config/Routes.php index 1555cfb..a38b782 100644 --- a/config/Routes.php +++ b/config/Routes.php @@ -5,5 +5,11 @@ return [ 'methods' => ['GET'], 'controller' => 'Simfile', 'action' => 'list' + ], + + '/simfiles/argTest/:testarg' => [ + 'methods' => ['GET'], + 'controller' => 'Simfile', + 'action' => 'test' ] ]; diff --git a/public_html/index.php b/public_html/index.php index def40cd..1f6b8a1 100644 --- a/public_html/index.php +++ b/public_html/index.php @@ -13,6 +13,10 @@ $router = $container->get('Services\Routing\IRouter'); $controllerName= $router->getControllerName(); $controllerAction = $router->getActionName(); +$controllerActionArgs = $router->getActionArgs(); $controller = $container->get('Controllers\\' . $controllerName . 'Controller' ); -$controller->{$controllerAction . 'Action'}(); + +// Last thing to do, call the action on the specified controller. +call_user_func(array($controller, $controllerAction . 'Action'), $controllerActionArgs); + -- 2.11.0