From 1643e10c74472a0fde89cbea06745c8c72eb20e6 Mon Sep 17 00:00:00 2001 From: AD7six Date: Fri, 2 Aug 2013 13:39:54 +0000 Subject: [PATCH] pretty print json and xml responses in debug mode --- lib/Cake/Test/Case/View/JsonViewTest.php | 5 +++++ lib/Cake/Test/Case/View/XmlViewTest.php | 5 +++++ lib/Cake/View/JsonView.php | 5 +++++ lib/Cake/View/XmlView.php | 8 +++++++- 4 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/Cake/Test/Case/View/JsonViewTest.php b/lib/Cake/Test/Case/View/JsonViewTest.php index 5ee975657..9d9a04b59 100644 --- a/lib/Cake/Test/Case/View/JsonViewTest.php +++ b/lib/Cake/Test/Case/View/JsonViewTest.php @@ -30,6 +30,11 @@ App::uses('JsonView', 'View'); */ class JsonViewTest extends CakeTestCase { + public function setUp() { + parent::setUp(); + Configure::write('debug', 0); + } + /** * testRenderWithoutView method * diff --git a/lib/Cake/Test/Case/View/XmlViewTest.php b/lib/Cake/Test/Case/View/XmlViewTest.php index bf6bf3dcb..69b7b3d03 100644 --- a/lib/Cake/Test/Case/View/XmlViewTest.php +++ b/lib/Cake/Test/Case/View/XmlViewTest.php @@ -30,6 +30,11 @@ App::uses('XmlView', 'View'); */ class XmlViewTest extends CakeTestCase { + public function setUp() { + parent::setUp(); + Configure::write('debug', 0); + } + /** * testRenderWithoutView method * diff --git a/lib/Cake/View/JsonView.php b/lib/Cake/View/JsonView.php index cc73e3629..a417960a6 100644 --- a/lib/Cake/View/JsonView.php +++ b/lib/Cake/View/JsonView.php @@ -125,6 +125,11 @@ class JsonView extends View { } else { $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null; } + + if (version_compare(PHP_VERSION, '5.4.0', '>=') && Configure::read('debug')) { + return json_encode($data, JSON_PRETTY_PRINT); + } + return json_encode($data); } diff --git a/lib/Cake/View/XmlView.php b/lib/Cake/View/XmlView.php index e77698eec..083fd8c12 100644 --- a/lib/Cake/View/XmlView.php +++ b/lib/Cake/View/XmlView.php @@ -113,7 +113,13 @@ class XmlView extends View { $data = array($rootNode => array($serialize => $data)); } } - return Xml::fromArray($data)->asXML(); + + $options = array(); + if (Configure::read('debug')) { + $options['pretty'] = true; + } + + return Xml::fromArray($data, $options)->asXML(); } }