Changeset View
Changeset View
Standalone View
Standalone View
src/aphront/configuration/AphrontApplicationConfiguration.php
<?php | <?php | ||||
/** | /** | ||||
* @task routing URI Routing | * @task routing URI Routing | ||||
* @task response Response Handling | * @task response Response Handling | ||||
* @task exception Exception Handling | |||||
*/ | */ | ||||
abstract class AphrontApplicationConfiguration extends Phobject { | abstract class AphrontApplicationConfiguration extends Phobject { | ||||
private $request; | private $request; | ||||
private $host; | private $host; | ||||
private $path; | private $path; | ||||
private $console; | private $console; | ||||
abstract public function getApplicationName(); | |||||
abstract public function buildRequest(); | abstract public function buildRequest(); | ||||
abstract public function build404Controller(); | abstract public function build404Controller(); | ||||
abstract public function buildRedirectController($uri, $external); | abstract public function buildRedirectController($uri, $external); | ||||
final public function setRequest(AphrontRequest $request) { | final public function setRequest(AphrontRequest $request) { | ||||
$this->request = $request; | $this->request = $request; | ||||
return $this; | return $this; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 454 Lines • ▼ Show 20 Lines | throw new Exception( | ||||
get_class($controller), | get_class($controller), | ||||
'handleRequest()', | 'handleRequest()', | ||||
'AphrontResponse', | 'AphrontResponse', | ||||
'AphrontResponseProducerInterface')); | 'AphrontResponseProducerInterface')); | ||||
} | } | ||||
/** | /** | ||||
* Verifies that the erturn value from an | * Verifies that the return value from an | ||||
* @{class:AphrontResponseProducerInterface} is of an allowed type. | * @{class:AphrontResponseProducerInterface} is of an allowed type. | ||||
* | * | ||||
* @param AphrontResponseProducerInterface Object which produced | * @param AphrontResponseProducerInterface Object which produced | ||||
* this response. | * this response. | ||||
* @param wild Supposedly valid response. | * @param wild Supposedly valid response. | ||||
* @return void | * @return void | ||||
* @task response | * @task response | ||||
*/ | */ | ||||
Show All 13 Lines | throw new Exception( | ||||
get_class($producer), | get_class($producer), | ||||
'produceAphrontResponse()', | 'produceAphrontResponse()', | ||||
'AphrontResponse', | 'AphrontResponse', | ||||
'AphrontResponseProducerInterface')); | 'AphrontResponseProducerInterface')); | ||||
} | } | ||||
/** | /** | ||||
* Verifies that the return value from an | |||||
* @{class:AphrontRequestExceptionHandler} is of an allowed type. | |||||
* | |||||
* @param AphrontRequestExceptionHandler Object which produced this | |||||
* response. | |||||
* @param wild Supposedly valid response. | |||||
* @return void | |||||
* @task response | |||||
*/ | |||||
private function validateErrorHandlerResponse( | |||||
AphrontRequestExceptionHandler $handler, | |||||
$response) { | |||||
if ($this->isValidResponseObject($response)) { | |||||
return; | |||||
} | |||||
throw new Exception( | |||||
pht( | |||||
'Exception handler "%s" returned an invalid response from call to '. | |||||
'"%s". This method must return an object of class "%s", or an object '. | |||||
'which implements the "%s" interface.', | |||||
get_class($handler), | |||||
'handleRequestException()', | |||||
'AphrontResponse', | |||||
'AphrontResponseProducerInterface')); | |||||
} | |||||
/** | |||||
* Resolves a response object into an @{class:AphrontResponse}. | * Resolves a response object into an @{class:AphrontResponse}. | ||||
* | * | ||||
* Controllers are permitted to return actual responses of class | * Controllers are permitted to return actual responses of class | ||||
* @{class:AphrontResponse}, or other objects which implement | * @{class:AphrontResponse}, or other objects which implement | ||||
* @{interface:AphrontResponseProducerInterface} and can produce a response. | * @{interface:AphrontResponseProducerInterface} and can produce a response. | ||||
* | * | ||||
* If a controller returns a response producer, invoke it now and produce | * If a controller returns a response producer, invoke it now and produce | ||||
* the real response. | * the real response. | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | while (true) { | ||||
$this->validateProducerResponse($response, $new_response); | $this->validateProducerResponse($response, $new_response); | ||||
$response = $new_response; | $response = $new_response; | ||||
} | } | ||||
return $response; | return $response; | ||||
} | } | ||||
/* -( Error Handling )----------------------------------------------------- */ | |||||
/** | |||||
* Convert an exception which has escaped the controller into a response. | |||||
* | |||||
* This method delegates exception handling to available subclasses of | |||||
* @{class:AphrontRequestExceptionHandler}. | |||||
* | |||||
* @param Exception Exception which needs to be handled. | |||||
* @return wild Response or response producer, or null if no available | |||||
* handler can produce a response. | |||||
* @task exception | |||||
*/ | |||||
private function handleException(Exception $ex) { | |||||
$handlers = AphrontRequestExceptionHandler::getAllHandlers(); | |||||
$request = $this->getRequest(); | |||||
foreach ($handlers as $handler) { | |||||
if ($handler->canHandleRequestException($request, $ex)) { | |||||
$response = $handler->handleRequestException($request, $ex); | |||||
$this->validateErrorHandlerResponse($handler, $response); | |||||
return $response; | |||||
} | |||||
} | |||||
throw $ex; | |||||
} | |||||
} | } |