public function testStaticConstructor() { $exception = CubexException::debugException("msg", 123, "debug"); $this->assertEquals("msg", $exception->getMessage()); $this->assertEquals(123, $exception->getCode()); $this->assertEquals("debug", $exception->getDebug()); }
/** * Handles a Request to convert it to a Response. * * When $catch is true, the implementation must catch all exceptions * and do its best to convert them to a Response instance. * * @param Request $request A Request instance * @param integer $type The type of the request * (one of HttpKernelInterface::MASTER_REQUEST * or HttpKernelInterface::SUB_REQUEST) * @param Boolean $catch Whether to catch exceptions or not * * @return Response A Response instance * * @throws \Exception When an Exception occurs during processing * * @api */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { $this->_request = $request; if (!$request instanceof CubexRequest) { throw new \RuntimeException("Invalid cubex request"); } //Initialise the kernel $this->init(); try { //Check to see if the request is allowed to process $authed = $this->canProcess(); //If can process returns a response, use that to send back to the user if ($authed instanceof Response) { return $authed; } $sub = $request->subDomain(); if (!method_exists($this, $sub)) { $sub = 'defaultAction'; } $response = $this->_processResponse($this->_getCallableResult([$this, $sub]), $request, $type, $catch); if (!$response instanceof Response) { throw CubexException::debugException("The subdomain requested is not be supported", 404, $response); } } catch (\Exception $e) { //shutdown the kernel $this->shutdown(); if ($catch && $e->getCode() == 404) { return $this->getCubex()->make('404'); } else { if ($catch) { return $this->getCubex()->exceptionResponse($e); } else { throw $e; } } } //shutdown the kernel $this->shutdown(); return $response; }
public function exceptionProvider() { return [[CubexException::debugException('Broken', 404, 'debug'), ['Broken</h2>', '(404)', 'debug</div>']], [CubexException::debugException('', 0, ['a' => 'b']), print_r(['a' => 'b'], true)], [CubexException::debugException('', 0, false), 'bool(false)'], [CubexException::debugException('', 0, 23), 'int(23)']]; }
/** * Handles a Request to convert it to a Response. * * When $catch is true, the implementation must catch all exceptions * and do its best to convert them to a Response instance. * * @param Request $request A Request instance * @param integer $type The type of the request * (one of HttpKernelInterface::MASTER_REQUEST * or HttpKernelInterface::SUB_REQUEST) * @param Boolean $catch Whether to catch exceptions or not * * @return Response A Response instance * * @throws \Exception When an Exception occurs during processing * * @api */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { $this->_request = $request; $calltime = microtime(true); //Initialise the kernel $this->init(); try { //Check to see if the request is allowed to process $authed = $this->canProcess(); //If can process returns a response, use that to send back to the user if ($authed instanceof Response) { return $this->_sanitizeResponse($authed); } else { if ($authed !== true) { return new Error403Response(); } } $response = null; $route = $this->findRoute($request); if ($route) { //Process the route to get the response $response = $this->executeRoute($route, $request, $type, $catch); } if (!$response instanceof Response) { throw CubexException::debugException("The processed route did not yield a valid response", 404, $response); } } catch (\Exception $e) { //shutdown the kernel $this->shutdown(); if ($catch) { return $this->handleException($e); } else { throw $e; } } //shutdown the kernel $this->shutdown(); if ($response instanceof CubexResponse) { $response->setCallStartTime($calltime); } return $response; }
/** * @param Request $request * @param int $type * @param bool $catch * * @return CubexResponse|BinaryFileResponse|Response * @throws \Exception */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { //If the favicon has not been picked up within the public folder //return the cubex favicon if ($request->getRequestUri() === '/favicon.ico') { $favIconPaths = []; $favIconPaths[] = Path::build($this->getProjectRoot(), 'favicon.ico'); $favIconPaths[] = Path::build($this->getProjectRoot(), 'assets', 'favicon.ico'); $favIconPaths[] = Path::build(dirname(__DIR__), 'favicon.ico'); $favPath = null; foreach ($favIconPaths as $favPath) { if (file_exists($favPath)) { break; } } $favicon = new BinaryFileResponse($favPath); $favicon->prepare($request); return $favicon; } try { //Ensure all constants have been configured if ($this->getDocRoot() === null) { throw new \RuntimeException("Cubex has been constructed without a document root provided" . ", you must call createConstants before calling handle."); } //Ensure we are working with a Cubex Request for added functionality if (!$request instanceof CubexRequest) { throw new \InvalidArgumentException('You must use a \\Cubex\\Http\\Request'); } $this->instance('request', $request); //Boot Cubex $this->boot(); //Retrieve the $kernel = $this->makeWithCubex('\\Cubex\\Kernel\\CubexKernel'); if ($kernel instanceof CubexKernel) { $response = $kernel->handle($request, $type, $catch); if (!$response instanceof Response) { throw CubexException::debugException("A valid response was not generated by the default kernel", 500, $response); } return $response; } throw new \RuntimeException("No Cubex Kernel has been configured"); } catch (\Exception $e) { if ($catch) { return $this->exceptionResponse($e); } else { throw $e; } } }