/** * {@inheritdoc} * * @param int $offset * @param int $limit * @param array $params * @return array|int|mixed */ public function readSet($offset = 0, $limit = 10, $params = array()) { $select = Db::select('*')->from('test', 't'); if ($limit) { $selectPart = $select->getQueryPart('select'); $selectPart = 'SQL_CALC_FOUND_ROWS ' . current($selectPart); $select->select($selectPart); $select->setLimit($limit); $select->setOffset($offset); } $result = $select->execute('\\Application\\Test\\Row'); if ($limit) { $total = Db::fetchOne('SELECT FOUND_ROWS()'); } else { $total = sizeof($result); } if (sizeof($result) < $total && Request::METHOD_GET == Request::getMethod()) { Response::setStatusCode(206); Response::setHeader('Content-Range', 'items ' . $offset . '-' . ($offset + sizeof($result)) . '/' . $total); } return $result; }
/** * Do process * @return void */ protected function doProcess() { Logger::info("app:process:do"); $module = Request::getModule(); $controller = Request::getController(); $params = Request::getAllParams(); // try to dispatch controller try { // get reflection of requested controller $controllerFile = $this->getControllerFile($module, $controller); $reflection = $this->reflection($controllerFile); // check header "accept" for catch JSON(P) or XML requests, and switch presentation // it's some magic for AJAX and REST requests if ($produces = $reflection->getAccept() and $accept = $this->getRequest()->getAccept()) { // switch statement for $accept switch ($accept) { case Request::ACCEPT_HTML: // with layout // without additional presentation break; case Request::ACCEPT_JSON: $this->useJson(); break; case Request::ACCEPT_JSONP: $this->useJsonp(); break; case Request::ACCEPT_XML: $this->useXml(); break; default: // not acceptable MIME type throw new NotAcceptableException(); break; } } // check call method(s) if ($reflection->getMethod() && !in_array(Request::getMethod(), $reflection->getMethod())) { throw new NotAllowedException(join(',', $reflection->getMethod())); } // check HTML cache if ($reflection->getCacheHtml() && Request::getMethod() == Request::METHOD_GET) { $htmlKey = 'html:' . $module . ':' . $controller . ':' . http_build_query($params); if ($cachedHtml = Cache::get($htmlKey)) { Response::setBody($cachedHtml); return; } } // dispatch controller $dispatchResult = $this->dispatch($module, $controller, $params); } catch (RedirectException $e) { Response::setException($e); if (Request::isXmlHttpRequest()) { // 204 - No Content Response::setStatusCode(204); Response::setHeader('Bluz-Redirect', $e->getMessage()); } else { Response::setStatusCode($e->getCode()); Response::setHeader('Location', $e->getMessage()); } return null; } catch (ReloadException $e) { Response::setException($e); if (Request::isXmlHttpRequest()) { // 204 - No Content Response::setStatusCode(204); Response::setHeader('Bluz-Reload', 'true'); } else { Response::setStatusCode($e->getCode()); Response::setHeader('Refresh', '0; url=' . Request::getRequestUri()); } return null; } catch (\Exception $e) { Response::setException($e); // cast to valid HTTP error code // 500 - Internal Server Error $statusCode = 100 <= $e->getCode() && $e->getCode() <= 505 ? $e->getCode() : 500; Response::setStatusCode($statusCode); $dispatchResult = $this->dispatch(Router::getErrorModule(), Router::getErrorController(), array('code' => $e->getCode(), 'message' => $e->getMessage())); } if ($this->hasLayout()) { Layout::setContent($dispatchResult); $dispatchResult = Layout::getInstance(); } if (isset($htmlKey, $reflection)) { // @TODO: Added ETag header Cache::set($htmlKey, $dispatchResult(), $reflection->getCacheHtml()); Cache::addTag($htmlKey, $module); Cache::addTag($htmlKey, 'html'); Cache::addTag($htmlKey, 'html:' . $module); Cache::addTag($htmlKey, 'html:' . $module . ':' . $controller); } Response::setBody($dispatchResult); }
/** * Method DELETE * * @return false * @throws BadRequestException */ public function methodDelete() { if (!empty($this->primary)) { // delete one // @throws NotFoundException $this->deleteOne($this->primary); } else { // delete collection // @throws NotFoundException if (!sizeof($this->data)) { // data not exist throw new BadRequestException(); } $this->deleteSet($this->data); } Response::setStatusCode(204); return false; // disable view }
* * @param \Bluz\Crud\Table $crud * @param mixed $primary * @param array $data * @return void|array * @throws BadRequestException * @throws NotImplementedException */ return function ($crud, $primary, $data) { if (!sizeof($data)) { // data not found throw new BadRequestException(); } try { if (!empty($primary)) { // update one item $result = $crud->updateOne($primary, $data); } else { // update collection $result = $crud->updateSet($data); } // if $result === 0 it's means a update is not apply // or records not found if (0 === $result) { Response::setStatusCode(304); } } catch (ValidatorException $e) { Response::setStatusCode(400); return ['errors' => $e->getErrors()]; } };
* @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\Application\Helper; use Bluz\Application\Application; use Bluz\Controller\Controller; use Bluz\Proxy\Response; use Bluz\Proxy\Router; /** * Reload helper can be declared inside Bootstrap * @param \Exception $exception * @return Controller */ return function ($exception) { /** * @var Application $this */ Response::removeHeaders(); Response::clearBody(); // cast to valid HTTP error code // 500 - Internal Server Error $statusCode = 100 <= $exception->getCode() && $exception->getCode() <= 505 ? $exception->getCode() : 500; Response::setStatusCode($statusCode); $module = Router::getErrorModule(); $controller = Router::getErrorController(); $params = ['code' => $exception->getCode(), 'message' => $exception->getMessage()]; return $this->dispatch($module, $controller, $params); };
* @accept JSON * @method POST * * @param \Bluz\Crud\Table $crud * @param mixed $primary * @param array $data * @return void|array * @throws BadRequestException * @throws NotImplementedException */ return function ($crud, $primary, $data) { if (!empty($primary)) { // POST + ID is incorrect behaviour throw new NotImplementedException(); } try { $result = $crud->createOne($data); if (!$result) { // system can't create record with this data throw new BadRequestException(); } if (is_array($result)) { $result = join('-', array_values($result)); } } catch (ValidatorException $e) { Response::setStatusCode(400); return ['errors' => $e->getErrors()]; } Response::setStatusCode(201); Response::setHeader('Location', Router::getUrl(Request::getModule(), Request::getController()) . '/' . $result); };
* @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\Application\Helper; use Bluz\Application\Application; use Bluz\Proxy\Request; use Bluz\Proxy\Response; /** * Redirect helper can be declared inside Bootstrap * @param string $url * @return null */ return function ($url) { /** * @var Application $this */ $this->useLayout(false); Response::removeHeaders(); Response::clearBody(); if (Request::isXmlHttpRequest()) { Response::setStatusCode(204); Response::setHeader('Bluz-Redirect', $url); } else { Response::setStatusCode(302); Response::setHeader('Location', $url); } return null; };
* @param string $id * * @route /api/{$resource} * @param string $resource * * @return mixed */ return function ($resource, $id, $relation, $relationId) { /** * @var Controller $this */ $this->useJson(); Auth::clearIdentity(); try { // authentication by api token if ($token = Request::getParam('token')) { Table::getInstance()->authenticateToken($token); } $params = []; foreach ([$id, $relation, $relationId] as $param) { if (!is_null($param)) { $params[] = $param; } } return $this->dispatch('api', 'resources/' . $resource, $params); } catch (\Exception $e) { // process exceptions here Response::setStatusCode($e->getCode()); return ['error' => $e->getMessage()]; } };