/** * @param \Throwable $exception * * @return bool */ public function exceptionHandler(\Throwable $exception) { if ($exception instanceof ResponseCode) { try { $out = $this->render($exception->getCode(), [], $exception->display()); } catch (\Throwable $exception) { return $this->exceptionHandler($exception); } // debug on dev / display trace if (!(AbstractApp::env() != AbstractApp::PRODUCTION && ob_get_length() > 0)) { self::response()->addHeader('Content-Type', $this->getErrorContentType()); } self::response()->setStatus($exception->getCode()); self::response()->setBody($out); HttpApp::instance()->end(); } else { Handler::log($exception); if (AbstractApp::env() != AbstractApp::PRODUCTION) { Handler::exceptionHandler($exception); } else { $throw = new ResponseCode($exception->getMessage(), 500, $exception); $this->exceptionHandler($throw); } } return true; }
/** * @return void */ public function init() { if (self::request()->getMethod() == 'OPTIONS') { self::response()->addHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); self::response()->addHeader('Access-Control-Max-Age', '604800'); self::response()->addHeader('Access-Control-Request-Headers', 'Origin, Content-Type, Accept, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control'); HttpApp::instance()->end(); } // Enable CORS if (self::request()->getHeader('Origin')) { self::response()->addHeader('Access-Control-Allow-Origin', self::request()->getHeader('Origin')); self::response()->addHeader('Access-Control-Allow-Credentials', 'true'); self::response()->addHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, X-Apikey'); } }
/** * @param string $rendererName * @param string $namespace * @param int $version * @param string $serviceName * @param string $method * * @throws ResponseCode * * @return AbstractService */ protected function initService(string $rendererName, string $namespace, int $version, string $serviceName, string $method) { // renderer $rendererClass = '\\Cawa\\SwaggerServer\\Renderer\\' . $rendererName; if (!class_exists($rendererClass)) { throw new ResponseCode(sprintf("Unknown renderer '%s'", $rendererClass), 404); } /* @var $renderer AbstractRenderer */ $renderer = new $rendererClass(); $renderer->registerExceptionHandler(); $this->controlNamespace($namespace, $version); $service = $this->controlService($namespace, $version, $serviceName, $method); $reflectionMethod = $service->getReflectionMethod($method); $serviceClass = get_class($service); // auth validation /* @var $authName Auth */ if (!($authName = $reflectionMethod->getParam(Definition::AUTH))) { throw new \LogicException(sprintf("Undefined Auth on '%s::%s'", $serviceClass, $method)); } /* @var $auth \Cawa\SwaggerServer\Auth\AbstractAuth */ $authClass = '\\Cawa\\SwaggerServer\\Auth\\' . $authName->getAuth(); $auth = new $authClass($this->module()); if (!$auth->isAllowed($serviceClass)) { if (!$auth->promptAuth()) { throw new ResponseCode(sprintf("Unauthorized service '%s::%s'", $serviceClass, $method), 403); } else { HttpApp::instance()->end(); } } $service->setAuth($auth); $service->setRenderer($renderer); return $service; }
/** * Load route & request */ public function init() { $explode = explode(':', $_SERVER['HTTP_HOST']); $_SERVER['SERVER_NAME'] = array_shift($explode); parent::init(); }
/** * @param int $statusCode */ public function redirectSelf(int $statusCode = 302) { $this->setStatus($statusCode); $this->addHeader('Location', (string) self::request()->getUri()); HttpApp::instance()->end(); }