+++ /dev/null
-<?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;
- }
-}
interface IDivineController
{
- public function setJsonResponse($bool);
- public function getAction();
+ public function indexAction();
}
$this->_simfileRepository = $repository;
}
- public function getAction() {
+ public function indexAction() {
/* @var $simfile Domain\Entities\StepMania\ISimfile */
// public function getMethod();
// public function isGet();
--- /dev/null
+<?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
$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
$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
{
public function matches($uri);
public function supports($method);
- public function execute();
+ public function getControllerName();
+ public function getActionName();
}
--- /dev/null
+<?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
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;
}
public function getControllerName()
{
- $this->_controllerName;
+ return $this->_controllerName;
+ }
+
+ public function getActionName()
+ {
+ return $this->_actionName;
}
}
<?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;
+ }
+ }
+ }
+}
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
--- /dev/null
+<?php\r
+\r
+return [\r
+ '/simfiles' => [\r
+ 'methods' => ['GET'],\r
+ 'controller' => 'Simfile',\r
+ 'action' => 'list'\r
+ ]\r
+];\r
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
<?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