From: Cameron Ball Date: Wed, 10 Sep 2014 08:15:12 +0000 (+0800) Subject: Add request class. Not really tested yet. X-Git-Url: http://git.cameron1729.xyz/?p=rock.divinelegy.git;a=commitdiff_plain;h=55d5a84a7acc674338e93e05db3059a0c503aae6 Add request class. Not really tested yet. --- diff --git a/Controllers/AbstractBaseController.php b/Controllers/AbstractBaseController.php new file mode 100644 index 0000000..b30837a --- /dev/null +++ b/Controllers/AbstractBaseController.php @@ -0,0 +1,23 @@ +_jasonResponse = $bool; + } +} diff --git a/Controllers/IDivineController.php b/Controllers/IDivineController.php index c11b7fd..535fbea 100644 --- a/Controllers/IDivineController.php +++ b/Controllers/IDivineController.php @@ -4,6 +4,6 @@ namespace Controllers; interface IDivineController { - public function setJsonResponse(); + public function setJsonResponse($bool); public function getAction(); } diff --git a/Controllers/IndexController.php b/Controllers/IndexController.php index a7f0c98..3880ec7 100644 --- a/Controllers/IndexController.php +++ b/Controllers/IndexController.php @@ -3,40 +3,55 @@ namespace Controllers; use DataAccess\StepMania\ISimfileRepository; -use Services\IHttpResponse; +use Services\Http\IHttpResponse; +use Services\Http\IHttpRequest; +use Controllers\AbstractBaseController; -class IndexController implements IDivineController +class IndexController extends AbstractBaseController implements IDivineController { private $_content; private $_simfileRepository; - private $_jsonResponse; private $_response; + private $_request; //override public function __construct( + IHttpRequest $request, IHttpResponse $response, ISimfileRepository $repository ) { + $this->_request = $request; $this->_response = $response; $this->_simfileRepository = $repository; } - - public function setJsonResponse() { - $this->_jsonResponse = true; - } public function getAction() { /* @var $simfile Domain\Entities\StepMania\ISimfile */ - $simfile = $this->_simfileRepository->find(1); - $modes = array(); - /* @var $steps Domain\VOs\StepMania\IStepChart */ - foreach ($simfile->getSteps() as $steps) { - $modes[] = $steps->getArtist()->getTag(); - } +// 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->getMethod(); $this->_response->setHeader('Content-Type', 'application/json') - ->setBody(json_encode($modes)) + ->setBody(json_encode(array('body' => $r->getBody()))) ->sendResponse(); } } diff --git a/DataAccess/DataMapper/DataMapper.php b/DataAccess/DataMapper/DataMapper.php index 8ee898d..be1bf0a 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, $password, $options); + $this->_db = new PDO($dsn, $username, null, $options); $this->_maps = include $maps; } diff --git a/Services/Bar.php b/Services/Bar.php deleted file mode 100644 index 47b916d..0000000 --- a/Services/Bar.php +++ /dev/null @@ -1,25 +0,0 @@ - - */ -class Bar implements BarInterface -{ - - /** - * just returns a hardcoded string - * - * @return string - */ - public function returnMe() - { - return 'This is from the bar class'; - } -} \ No newline at end of file diff --git a/Services/BarInterface.php b/Services/BarInterface.php deleted file mode 100644 index 4f526e1..0000000 --- a/Services/BarInterface.php +++ /dev/null @@ -1,22 +0,0 @@ - - */ -interface BarInterface -{ - - /** - * returnMe - * - * @return string - */ - public function returnMe(); -} \ No newline at end of file diff --git a/Services/Foo.php b/Services/Foo.php deleted file mode 100644 index abf8d25..0000000 --- a/Services/Foo.php +++ /dev/null @@ -1,42 +0,0 @@ - - */ -class Foo -{ - - /** - * This just holds the injected instance of Bar - * @var MikeFunk\Test\Bar - */ - protected $bar; - - /** - * inject Bar into $bar and then $this->bar with type hinting. The Bar - * class is bound to the BarInterface in config/di.yml - * - * @return void - */ - public function __construct(BarInterface $bar) - { - $this->bar = $bar; - } - - /** - * Just return the string from Bar::returnMe() - * - * @return string - */ - public function returnMe() - { - return $this->bar->returnMe(); - } -} \ No newline at end of file diff --git a/Services/Http/HttpRequest.php b/Services/Http/HttpRequest.php new file mode 100644 index 0000000..2e6663e --- /dev/null +++ b/Services/Http/HttpRequest.php @@ -0,0 +1,158 @@ +_method = $_SERVER['REQUEST_METHOD']; + + if(isset($_SERVER['HTTP_COOKIE'])) { + $this->_cookies = Util::parseCookieHeader($_SERVER['HTTP_COOKIE']); + } + } + + public function getMethod() + { + return $this->_method; + } + + public function isGet() + { + return $this->_method == self::METHOD_GET; + } + + public function isPost() + { + return $this->_method == self::METHOD_POST; + } + + public function isPut() + { + return $this->_method == self::METHOD_PUT; + } + + public function isHead() + { + return $this->_method == self::METHOD_HEAD; + } + + public function isDelete() + { + return $this->_method == self::METHOD_DELETE; + } + + public function getContentType() + { + return isset($_SERVER['CONTENT_TYPE']) ? $_SERVER['CONTENT_TYPE'] : null; + } + + public function isFormData() + { + return in_array($this->getContentType(), $this->_formDataMediaTypes); + } + + public function get() + { + return !empty($_GET) ? $_GET : null; + } + + public function getGetElement($key) + { + return isset($_GET[$key]) ? $_GET[$key] : null; + } + + public function post() + { + return !empty($_POST) ? $_POST : null; + } + + public function getPostElement($key) + { + return isset($_POST[$key]) ? $_POST[$key] : null; + } + + public function put() + { + return $this->post(); + } + + public function getPutElement($key) + { + return $this->getPostElement($key); + } + + public function delete() + { + return $this->post(); + } + + public function getDeleteElement($key) + { + return $this->getPostElement($key); + } + + public function cookies() + { + return $this->_cookies; + } + + public function getCookie($key) + { + return isset($this->_cookies[$key]) ? $this->_cookies[$key] : null; + } + + public function getBody() + { + return @file_get_contents('php://input'); + } + public function getHost() + { + if(isset($_SERVER['HTTP_HOST'])) { + $parts = explode(':', $_SERVER['HTTP_HOST']); + return $parts[0]; + } + + return $_SERVER['SERVER_NAME']; + } + + public function getIp() + { + $keys = array('X_FORWARDED_FOR', 'HTTP_X_FORWARDED_FOR', 'CLIENT_IP', 'REMOTE_ADDR'); + foreach ($keys as $key) { + if(isset($_SERVER[$key])) { + return $_SERVER[$key]; + } + } + } + + public function getReferrer() + { + return $_SERVER['HTTP_REFERER']; + } + + // some people like to spell it referer due to RFC 1945 + public function getReferer() + { + return $this->getReferer(); + } + + public function getUserAgent() + { + return $_SERVER['HTTP_USER_AGENT']; + } +} \ No newline at end of file diff --git a/Services/HttpResponse.php b/Services/Http/HttpResponse.php similarity index 96% rename from Services/HttpResponse.php rename to Services/Http/HttpResponse.php index 26c6e43..3041556 100644 --- a/Services/HttpResponse.php +++ b/Services/Http/HttpResponse.php @@ -1,8 +1,8 @@ '../config/DataMaps.php', 'Domain\Entities\StepMania\ISimfile' => DI\object('Domain\Entities\StepMania\Simfile'), - 'Services\IHttpResponse' => DI\object('Services\HttpResponse'), + 'Services\Http\IHttpResponse' => DI\object('Services\Http\HttpResponse'), + 'Services\Http\IHttpRequest' => DI\object('Services\Http\HttpRequest'), 'DataAccess\StepMania\ISimfileRepository' => DI\object('DataAccess\StepMania\SimfileRepository'), 'DataAccess\DataMapper\IDataMapper' => DI\object('DataAccess\DataMapper\DataMapper') ->constructor(DI\link('datamapper.maps')),