Skip to content

Commit

Permalink
Added $this->getResponse()->setBodyJson() method (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbeaty authored Jan 2, 2025
1 parent 6a7eed2 commit 02d3877
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
5 changes: 2 additions & 3 deletions app/code/core/Mage/Adminhtml/controllers/IndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @package Mage_Adminhtml
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
* @copyright Copyright (c) 2019-2024 The OpenMage Contributors (https://openmage.org)
* @copyright Copyright (c) 2024 Maho (https://mahocommerce.com)
* @copyright Copyright (c) 2024-2025 Maho (https://mahocommerce.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

Expand Down Expand Up @@ -74,8 +74,7 @@ public function preloginAction()
}
}

$this->getResponse()->setHeader('Content-type', 'application/json', true)
->setBody(Mage::helper('core')->jsonEncode($result));
$this->getResponse()->setBodyJson($result);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions app/code/core/Mage/Checkout/controllers/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @package Mage_Checkout
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
* @copyright Copyright (c) 2019-2024 The OpenMage Contributors (https://openmage.org)
* @copyright Copyright (c) 2024 Maho (https://mahocommerce.com)
* @copyright Copyright (c) 2024-2025 Maho (https://mahocommerce.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

Expand Down Expand Up @@ -655,8 +655,7 @@ public function ajaxDeleteAction()
}
}

$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
$this->getResponse()->setBodyJson($result);
}

/**
Expand Down Expand Up @@ -708,8 +707,7 @@ public function ajaxUpdateAction()
}
}

$this->getResponse()->setHeader('Content-type', 'application/json');
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
$this->getResponse()->setBodyJson($result);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions app/code/core/Mage/Checkout/controllers/OnepageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @package Mage_Checkout
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
* @copyright Copyright (c) 2018-2024 The OpenMage Contributors (https://openmage.org)
* @copyright Copyright (c) 2024 Maho (https://mahocommerce.com)
* @copyright Copyright (c) 2024-2025 Maho (https://mahocommerce.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

Expand Down Expand Up @@ -680,7 +680,6 @@ protected function _canShowForUnregisteredUsers()
*/
protected function _prepareDataJSON($response)
{
$this->getResponse()->setHeader('Content-type', 'application/json', true);
return $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
return $this->getResponse()->setBodyJson($response);
}
}
12 changes: 11 additions & 1 deletion app/code/core/Mage/Core/Controller/Response/Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @package Mage_Core
* @copyright Copyright (c) 2006-2020 Magento, Inc. (https://magento.com)
* @copyright Copyright (c) 2019-2024 The OpenMage Contributors (https://openmage.org)
* @copyright Copyright (c) 2024 Maho (https://mahocommerce.com)
* @copyright Copyright (c) 2024-2025 Maho (https://mahocommerce.com)
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

Expand Down Expand Up @@ -99,4 +99,14 @@ public function sendHeadersAndExit(): never
$this->sendHeaders();
exit;
}

/**
* Prepare JSON formatted data for response to client
*/
public function setBodyJson(mixed $response): self
{
$this->setHeader('Content-type', 'application/json', true);
$this->setBody(Mage::helper('core')->jsonEncode($response));
return $this;
}
}

0 comments on commit 02d3877

Please sign in to comment.