From 0b0a4e9d69091aa24f59788764e7e463fdfbda5f Mon Sep 17 00:00:00 2001 From: Matthew Ross Date: Thu, 21 Apr 2016 18:08:59 -0400 Subject: [PATCH] Initial API setup --- src/api/.htaccess | 9 +++++ src/api/app-setup.php | 50 ++++++++++++++++++++++++++ src/api/controllers/ApiJson.php | 26 ++++++++++++++ src/api/controllers/BaseController.php | 15 ++++++++ src/api/controllers/Boards.php | 24 +++++++++++++ src/api/controllers/Invalid.php | 21 +++++++++++ src/api/controllers/todo | 0 src/api/index.php | 16 +++++++++ src/api/models/BaseModel.php | 30 ++++++++++++++++ src/api/models/Board.php | 24 +++++++++++++ src/api/models/todo | 0 11 files changed, 215 insertions(+) create mode 100644 src/api/.htaccess create mode 100644 src/api/app-setup.php create mode 100644 src/api/controllers/ApiJson.php create mode 100644 src/api/controllers/BaseController.php create mode 100644 src/api/controllers/Boards.php create mode 100644 src/api/controllers/Invalid.php delete mode 100644 src/api/controllers/todo create mode 100644 src/api/index.php create mode 100644 src/api/models/BaseModel.php create mode 100644 src/api/models/Board.php delete mode 100644 src/api/models/todo diff --git a/src/api/.htaccess b/src/api/.htaccess new file mode 100644 index 0000000..d1d7235 --- /dev/null +++ b/src/api/.htaccess @@ -0,0 +1,9 @@ +RewriteEngine On + +RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$ +RewriteRule ^(.*)$ - [E=BASE:%1] + +RewriteRule ^recipes.db$ %{ENV_BASE}index.php [QSA,L] + +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L] \ No newline at end of file diff --git a/src/api/app-setup.php b/src/api/app-setup.php new file mode 100644 index 0000000..062a5ca --- /dev/null +++ b/src/api/app-setup.php @@ -0,0 +1,50 @@ +getContainer(); + +// Inject a Monolog logger into the dependency container +$container['logger'] = function($c) { + $logger = new Monolog\Logger('API'); + $fileHandler = new Monolog\Handler\StreamHandler('logs/api.log'); + + $logger->pushHandler($fileHandler); + + return $logger; +}; + +// Replace notFoundHandler to use an API response +$container['notFoundHandler'] = function($c) { + return function($request, $response) use ($c) { + return $c['response'] + ->withHeader('Content-Type', 'application/json') + ->write('{ message: "Matching API call not found." }'); + }; +}; + +// Replace the errorHandler to use an API response +$container['errorHandler'] = function ($c) { + return function ($request, $response, $exception) use ($c) { + $c['logger']->addError('Server error', $exception->getTrace()); + + return $c['response']->withStatus(500) + ->withHeader('Content-Type', 'application/json') + ->write('{ message: "Internal Server Error", error: "' . + $exception->getMessage() . '" }'); + }; +}; + +// Routes ending in '/' use route without '/' +$app->add(function($request, $response, $next) { + $uri = $request->getUri(); + $path = $uri->getPath(); + + if (strlen($path) > 1 && substr($path, -1) === '/') { + $path = substr($path, 0, -1); + } + + if ($uri->getPath() !== $path) { + return $next($request->withUri($uri->withPath($path)), $response); + } + + return $next($request, $response); +}); + diff --git a/src/api/controllers/ApiJson.php b/src/api/controllers/ApiJson.php new file mode 100644 index 0000000..dc0ca05 --- /dev/null +++ b/src/api/controllers/ApiJson.php @@ -0,0 +1,26 @@ +status = 'success'; + } + + function setFailure() { + $this->status = 'failure'; + } + + function addData($obj) { + $this->data[] = $obj; + } + + function addAlert($type, $text) { + $this->alerts[] = [ + 'type' => $type, + 'text' => $text + ]; + } +} + diff --git a/src/api/controllers/BaseController.php b/src/api/controllers/BaseController.php new file mode 100644 index 0000000..4624663 --- /dev/null +++ b/src/api/controllers/BaseController.php @@ -0,0 +1,15 @@ +apiJson = new ApiJson(); + $this->logger = $container->get('logger'); + } + + public function jsonResponse($response) { + return $response->withJson($this->apiJson); + } +} + diff --git a/src/api/controllers/Boards.php b/src/api/controllers/Boards.php new file mode 100644 index 0000000..c01ffa7 --- /dev/null +++ b/src/api/controllers/Boards.php @@ -0,0 +1,24 @@ +apiJson->setSuccess(); + + foreach($boardBeans as $bean) { + $this->apiJson->addData(Board::fromBean($bean)); + } + } else { + $this->logger->addInfo('No boards in database.'); + $this->apiJson->addAlert('info', 'No boards in database.'); + } + + return $this->jsonResponse($response); + } + +} + diff --git a/src/api/controllers/Invalid.php b/src/api/controllers/Invalid.php new file mode 100644 index 0000000..e13a293 --- /dev/null +++ b/src/api/controllers/Invalid.php @@ -0,0 +1,21 @@ +apiJson->addAlert('error', + 'No API functionality at this endpoint.'); + + $apiReturn = new stdClass(); + $apiReturn->status = 'One of "success" or "failure".'; + $apiReturn->data = 'An array of data (JSON objects and/or arrays).'; + $apiReturn->alerts = 'An array of alerts, with "type" of "success", "error", "warn", or "info" and a "text" message.'; + + $this->apiJson->addData($apiReturn); + + $this->logger->addInfo('Invalid Endpoint: ', [$this->apiJson]); + + return $this->jsonResponse($response); + } + +} + diff --git a/src/api/controllers/todo b/src/api/controllers/todo deleted file mode 100644 index e69de29..0000000 diff --git a/src/api/index.php b/src/api/index.php new file mode 100644 index 0000000..3b894b8 --- /dev/null +++ b/src/api/index.php @@ -0,0 +1,16 @@ +get('/', 'Invalid:noApi'); + +$app->get('/boards', 'Boards:getAllBoards'); + +$app->run(); +R::close(); + diff --git a/src/api/models/BaseModel.php b/src/api/models/BaseModel.php new file mode 100644 index 0000000..1a717b1 --- /dev/null +++ b/src/api/models/BaseModel.php @@ -0,0 +1,30 @@ +getContainer(); + + $this->logger = $container->get('logger'); + $this->bean = R::load($type, $id); + } + + public abstract function updateBean(); + public abstract function loadFromBean($bean); + + public function save() { + $this->updateBean(); + R::store($this->bean); + $this->loadFromBean($this->bean); + } + + public function delete() { + $this->updateBean(); + R::trash($this->bean); + } +} + diff --git a/src/api/models/Board.php b/src/api/models/Board.php new file mode 100644 index 0000000..3176579 --- /dev/null +++ b/src/api/models/Board.php @@ -0,0 +1,24 @@ +loadFromBean($this->bean); + } + + public static function fromBean($bean) { + } + + public function updateBean() { + } + + public function loadFromBean() { + } + +} + diff --git a/src/api/models/todo b/src/api/models/todo deleted file mode 100644 index e69de29..0000000