/** * Prepare request for processing */ public function __construct() { // rewrite REST with "_method" param // this is workaround $this->method = strtoupper(Request::getParam('_method', Request::getMethod())); // get all params $query = Request::getQuery(); if (is_array($query) && !empty($query)) { unset($query['_method']); $this->params = $query; } $this->data = Request::getParams(); }
/** * Send body * * @return void */ protected function sendBody() { // Nobody for HEAD and OPTIONS if (Request::METHOD_HEAD == ProxyRequest::getMethod() || Request::METHOD_OPTIONS == ProxyRequest::getMethod()) { return; } // Body can be Closures $content = $this->body; if ($content instanceof \Closure) { $content(); } else { echo $content; } }
/** * Prepare request for processing */ public function __construct() { // HTTP method $method = Request::getMethod(); $this->method = strtoupper($method); // get path // %module% / %controller% / %id% / %relation% / %id% $path = Router::getCleanUri(); $this->params = explode('/', rtrim($path, '/')); // module $this->module = array_shift($this->params); // controller $this->controller = array_shift($this->params); $data = Request::getParams(); unset($data['_method'], $data['_module'], $data['_controller']); $this->data = $data; }
/** * {@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; }
break; case 401: $title = __("Unauthorized"); $description = __("You are not authorized to view this page, please sign in"); break; case 403: $title = __("Forbidden"); $description = __("You don't have permissions to access this page"); break; case 404: $title = __("Not Found"); $description = __("The page you requested was not found"); break; case 405: $title = __("Method Not Allowed"); $description = __("The server is not support method `%s`", Request::getMethod()); Response::setHeader('Allow', $message); break; case 406: $title = __("Not Acceptable"); $description = __("The server is not acceptable generating content type described at `Accept` header"); break; case 500: $title = __("Internal Server Error"); $description = __("The server encountered an unexpected condition"); break; case 501: $title = __("Not Implemented"); $description = __("The server does not understand or does not support the HTTP method"); break; case 503:
/** * 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); }
namespace Application; use Bluz\Application\Exception\BadRequestException; use Bluz\Application\Exception\NotImplementedException; use Bluz\Proxy\Messages; use Bluz\Proxy\Request; use Bluz\Validator\Exception\ValidatorException; /** * @accept HTML * @accept JSON * @method PUT * * @param \Bluz\Crud\Table $crud * @param mixed $primary * @param array $data * @return void|array * @throws BadRequestException * @throws NotImplementedException */ return function ($crud, $primary, $data) { try { // Result is numbers of affected rows $crud->updateOne($primary, $data); Messages::addSuccess("Record was updated"); return ['row' => $crud->readOne($primary), 'method' => Request::getMethod()]; } catch (ValidatorException $e) { $row = $crud->readOne($primary); $row->setFromArray($data); return ['row' => $row, 'errors' => $e->getErrors(), 'method' => Request::getMethod()]; } };
/** * Check `Method` * * @throws NotAllowedException */ public function checkMethod() { if ($this->getReflection()->getMethod() && !in_array(Request::getMethod(), $this->getReflection()->getMethod())) { Response::setHeader('Allow', join(',', $this->getReflection()->getMethod())); throw new NotAllowedException(); } }
<?php /** * Test AJAX * * @author Anton Shevchuk * @created 26.09.11 17:41 * @return closure */ namespace Application; use Bluz\Proxy\Messages; use Bluz\Proxy\Request; return function ($messages = false) use($view) { /** * @var Bootstrap $this * @var \Bluz\View\View $view */ if ($messages) { Messages::addNotice('Notice for AJAX call'); Messages::addSuccess('Success for AJAX call'); Messages::addError('Error for AJAX call'); $view->baz = 'qux'; } Messages::addNotice('Method ' . Request::getMethod()); $view->foo = 'bar'; };