use Services\Http\IHttpRequest;
use Controllers\AbstractBaseController;
-class IndexController extends AbstractBaseController implements IDivineController
+class IndexController implements IDivineController
{
private $_content;
$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();
}
}
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
\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
}\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
\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
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
public function supports($method);
public function getControllerName();
public function getActionName();
+ public function getActionArgs();
}
{\r
public function getControllerName();\r
public function getActionName();\r
+ public function getActionArgs();\r
}
\ No newline at end of file
private $_actionName;
private $_pattern;
private $_methods;
+ private $_actionArgs;
+ private $_argNames;
+ private $_argValues;
public function __construct($pattern, array $methods, $controllerName, $actionName = null)
{
}
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
* 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;
}
{
return $this->_actionName;
}
+
+ public function getActionArgs()
+ {
+ $argValues = array();
+ foreach($this->_argNames as $argName)
+ {
+ $argValues[] = $this->_argValues[$argName];
+ }
+
+ return $argValues;
+ }
}
return $matchedRoute ? $matchedRoute->getActionName() : 'index';
}
+ public function getActionArgs()
+ {
+ $matchedRoute = $this->findMatch();
+ return $matchedRoute ? $matchedRoute->getActionArgs() : array() ;
+ }
+
private function findMatch()
{
if($this->_matchedRoute)
'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
\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