From 46dddc5c5365fc9a34f49e164bc9aa0c5b8b7f80 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 7 Jun 2019 10:06:28 +1000 Subject: [PATCH] Update to use PSR HTTP Message Response Interface as per @totten --- CRM/Utils/System.php | 15 ++++----------- CRM/Utils/System/Base.php | 16 +++++++++++----- CRM/Utils/System/WordPress.php | 19 +++++++++++++------ Civi/Core/AssetBuilder.php | 2 +- 4 files changed, 29 insertions(+), 23 deletions(-) diff --git a/CRM/Utils/System.php b/CRM/Utils/System.php index 0c3e316219..96bec632d6 100644 --- a/CRM/Utils/System.php +++ b/CRM/Utils/System.php @@ -53,7 +53,7 @@ * @method static array synchronizeUsers() Create CRM contacts for all existing CMS users. * @method static appendCoreResources(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_coreResourceList. * @method static alterAssetUrl(\Civi\Core\Event\GenericHookEvent $e) Callback for hook_civicrm_getAssetUrl. - * @method static sendResponse(array $responseData) function to set HTTP status response and return specific response to client initially for assetBuilder content. + * @method static sendResponse(\Psr\Http\Message\ResponseInterface $response) function to handle RepsoseInterface for delivering HTTP Responses. */ class CRM_Utils_System { @@ -1864,18 +1864,11 @@ class CRM_Utils_System { /** * Return an HTTP Response with appropriate content and status code set. - * @param array $responseData + * @param \Psr\Http\Message\ResponseInterface $response */ - public static function sendResponse($responseData) { + public static function sendResponse(\Psr\Http\Message\ResponseInterface $response) { $config = CRM_Core_Config::singleton(); - if (!empty($responseData['statusCode'])) { - $config->userSystem->setStatusCode($responseData['statusCode']); - } - if (!empty($responseData['mimeType'])) { - self::setHttpHeader('Content-Type', $responseData['mimeType']); - } - echo $responseData['content']; - self::civiExit(); + $config->userSystem->sendResponse($response); } } diff --git a/CRM/Utils/System/Base.php b/CRM/Utils/System/Base.php index 654463b40a..6bc5290ac9 100644 --- a/CRM/Utils/System/Base.php +++ b/CRM/Utils/System/Base.php @@ -946,17 +946,23 @@ abstract class CRM_Utils_System_Base { } /** - * Set the HTTP Status Code for a request - * @param string $statusCode + * Send an HTTP Response base on PSR HTTP RespnseInterface response. + * + * @param \Psr\Http\Message\ResponseInterface $response */ - public function setStatusCode($statusCode) { + public function sendResponse(\Psr\Http\Message\ResponseInterface $response) { if (function_exists('http_response_code')) { // PHP 5.4+ - http_response_code($statusCode); + http_response_code($response->getStatusCode()); } else { - header('X-PHP-Response-Code: ' . $statusCode, TRUE, $statusCode); + // @todo should we remove this given we are no longer supporting < PHP 5.6 however keeping + // in for the moment for compatability with the original AssetBuilder code. + header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $reponse->getStatusCode()); } + CRM_Utils_System::setHttpHeader('Content-Type', $response->getHeaderLine('Content-Type')); + echo $response->getBody(); + CRM_Utils_System::civiExit(); } } diff --git a/CRM/Utils/System/WordPress.php b/CRM/Utils/System/WordPress.php index 84cba04f88..31ce2eccc9 100644 --- a/CRM/Utils/System/WordPress.php +++ b/CRM/Utils/System/WordPress.php @@ -864,18 +864,25 @@ class CRM_Utils_System_WordPress extends CRM_Utils_System_Base { } /** - * Set the HTTP Status Code for a request - * @param string $statusCode + * Send an HTTP Response base on PSR HTTP RespnseInterface response. + * + * @param \Psr\Http\Message\ResponseInterface $response */ - public function setStatusCode($statusCode) { - status_header($statusCode); + public function sendResponse(\Psr\Http\Message\ResponseInterface $response) { + // use WordPress function status_header to ensure 404 response is sent + status_header($response->getStatusCode()); if (function_exists('http_response_code')) { // PHP 5.4+ - http_response_code($statusCode); + http_response_code($response->getStatusCode()); } else { - header('X-PHP-Response-Code: ' . $statusCode, TRUE, $statusCode); + // @todo should we remove this given we are no longer supporting < PHP 5.6 however keeping + // in for the moment for compatability with the original AssetBuilder code. + header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $response->getStatusCode()); } + CRM_Utils_System::setHttpHeader('Content-Type', $response->getHeaderLine('Content-Type')); + echo $response->getBody(); + CRM_Utils_System::civiExit(); } } diff --git a/Civi/Core/AssetBuilder.php b/Civi/Core/AssetBuilder.php index f52b2653fd..4f0756610e 100644 --- a/Civi/Core/AssetBuilder.php +++ b/Civi/Core/AssetBuilder.php @@ -342,7 +342,7 @@ class AssetBuilder { public static function pageRun() { // Beg your pardon, sir. Please may I have an HTTP response class instead? $asset = self::pageRender($_GET); - \CRM_Utils_System::sendResponse($asset); + \CRM_Utils_System::sendResponse(new \GuzzleHttp\Psr7\Response($asset['statusCode'], ['Content-Type' => $asset['mimeType']], $asset['content'])); } /** -- 2.25.1