Merge pull request #1471 from cakephp/feature/2.4-pretty-print

pretty print json and xml responses in debug mode
This commit is contained in:
Andy Dawson 2013-08-03 01:35:56 -07:00
commit f34388c53a
4 changed files with 22 additions and 1 deletions

View file

@ -30,6 +30,11 @@ App::uses('JsonView', 'View');
*/ */
class JsonViewTest extends CakeTestCase { class JsonViewTest extends CakeTestCase {
public function setUp() {
parent::setUp();
Configure::write('debug', 0);
}
/** /**
* testRenderWithoutView method * testRenderWithoutView method
* *

View file

@ -30,6 +30,11 @@ App::uses('XmlView', 'View');
*/ */
class XmlViewTest extends CakeTestCase { class XmlViewTest extends CakeTestCase {
public function setUp() {
parent::setUp();
Configure::write('debug', 0);
}
/** /**
* testRenderWithoutView method * testRenderWithoutView method
* *

View file

@ -128,6 +128,11 @@ class JsonView extends View {
} else { } else {
$data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null; $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); return json_encode($data);
} }

View file

@ -116,7 +116,13 @@ class XmlView extends View {
$data = array($rootNode => array($serialize => $data)); $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();
} }
} }