/** * Returns a responder that supports HTTP requests. * * @param HttpRequest $request the request to satisfy. */ private static function getHttpResponder(HttpRequest $request) { $responder = null; $responderClassName = $request->getResponderClassName(); if (class_exists($responderClassName)) { $responder = new $responderClassName($request); Logger::get()->debug("Responder '{$responderClassName}' is ready."); } else { throw new \Eix\Services\Net\Http\NotFoundException("'{$responderClassName}' responder not found."); } return $responder; }
/** * Builds a request that describes an error condition, to be used when a * request could not be created. */ public function __construct() { parent::__construct(); // Method is always GET. $this->method = self::HTTP_METHOD_GET; // Cancel the URI parsing. $this->uri = false; }
/** * Returns a function name that represents the method and response's * expected content type. * * e.g. httpGetForHtml represents a GET request expecting HTML back. * e.g. httpPostForJson represents a POST request expecting JSON back. * e.g. httpPutForAll represents a PUT request expecting whatever back. */ private function getFunctionName($httpMethod) { if ($this->getRequest()) { $functionName = null; // Prefix starts with 'http'. $functionPrefix = 'http'; // The HTTP method is then appended. $functionPrefix .= ucfirst(strtolower($httpMethod)); // If an action has been captured in the URI, append it too. $action = $this->getRequest()->getParameter('action'); if ($action) { $functionPrefix .= ucfirst($action); // If there is an action, check whether the function is likely // to exist. if (!$this->isBaseFunctionAvailable($functionPrefix)) { throw new \Eix\Services\Net\Http\NotFoundException("This responder does not have any methods starting with {$functionPrefix}."); } } $found = false; $acceptedContentTypes = $this->getRequest()->getAcceptedContentTypes(); // Append a token to the function name, based on the requested content foreach ($acceptedContentTypes as $contentType => $quality) { try { $contentTypeToken = \Eix\Core\Requests\Http::getContentTypeToken($contentType); // Append the token for the response's expected content type. $functionName = $functionPrefix . 'For' . $contentTypeToken; Logger::get()->debug("Trying {$functionName} for {$contentType}..."); // Check whether a method with that name exists. if (method_exists($this, $functionName)) { return $functionName; } } catch (\InvalidArgumentException $exception) { // This content type is invalid, keep on checking. } } // Although a base method exists, it does not for this content type, // so that's a 406. throw new \Eix\Services\Net\Http\NotAcceptableException("This responder cannot serve content type {$contentType}."); } else { // If there is no request, return the generic responder method. return 'httpGetForAll'; } }