Skip to content

Commit

Permalink
Update to use PSR HTTP Message Response Interface as per @totten
Browse files Browse the repository at this point in the history
  • Loading branch information
seamuslee001 committed Jun 7, 2019
1 parent b29cb7e commit d5ffdf7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
15 changes: 4 additions & 11 deletions CRM/Utils/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -1864,18 +1864,11 @@ public static function createDefaultCrudLink($crudLinkSpec) {

/**
* 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);
}

}
11 changes: 7 additions & 4 deletions CRM/Utils/System/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -947,16 +947,19 @@ public function synchronizeUsers() {

/**
* Set the HTTP Status Code for a request
* @param string $statusCode
* @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);
header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $statusCode->getStatusCode());
}
CRM_Utils_System::setHeader('Content-Type', $response->getHeaderLine('Content-Type'));
echo $response->getBody();
CRM_Utils_System::civiExit();
}

}
14 changes: 9 additions & 5 deletions CRM/Utils/System/WordPress.php
Original file line number Diff line number Diff line change
Expand Up @@ -865,17 +865,21 @@ public function synchronizeUsers() {

/**
* Set the HTTP Status Code for a request
* @param string $statusCode
* @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);
header('X-PHP-Response-Code: ' . $response->getStatusCode(), TRUE, $statusCode->getStatusCode());
}
CRM_Utils_System::setHeader('Content-Type', $response->getHeaderLine('Content-Type'));
echo $response->getBody();
CRM_Utils_System::civiExit();
}

}
2 changes: 1 addition & 1 deletion Civi/Core/AssetBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ public function setCacheEnabled($cacheEnabled) {
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']));
}

/**
Expand Down

0 comments on commit d5ffdf7

Please sign in to comment.