Json.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Controller
  17. * @subpackage Zend_Controller_Action_Helper
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Controller_Action_Helper_Abstract
  24. */
  25. require_once 'Zend/Controller/Action/Helper/Abstract.php';
  26. /**
  27. * Simplify AJAX context switching based on requested format
  28. *
  29. * @uses Zend_Controller_Action_Helper_Abstract
  30. * @category Zend
  31. * @package Zend_Controller
  32. * @subpackage Zend_Controller_Action_Helper
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
  37. {
  38. /**
  39. * Suppress exit when sendJson() called
  40. * @var boolean
  41. */
  42. public $suppressExit = false;
  43. /**
  44. * Create JSON response
  45. *
  46. * Encodes and returns data to JSON. Content-Type header set to
  47. * 'application/json', and disables layouts and viewRenderer (if being
  48. * used).
  49. *
  50. * @param mixed $data
  51. * @param boolean $keepLayouts
  52. * @param boolean|array $keepLayouts
  53. * NOTE: if boolean, establish $keepLayouts to true|false
  54. * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  55. * if $keepLayouts and parmas for Zend_Json::encode are required
  56. * then, the array can contains a 'keepLayout'=>true|false
  57. * that will not be passed to Zend_Json::encode method but will be passed
  58. * to Zend_View_Helper_Json
  59. * @throws Zend_Controller_Action_Helper_Json
  60. * @return string
  61. */
  62. public function encodeJson($data, $keepLayouts = false)
  63. {
  64. /**
  65. * @see Zend_View_Helper_Json
  66. */
  67. require_once 'Zend/View/Helper/Json.php';
  68. $jsonHelper = new Zend_View_Helper_Json();
  69. $data = $jsonHelper->json($data, $keepLayouts);
  70. if (!$keepLayouts) {
  71. /**
  72. * @see Zend_Controller_Action_HelperBroker
  73. */
  74. require_once 'Zend/Controller/Action/HelperBroker.php';
  75. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
  76. }
  77. return $data;
  78. }
  79. /**
  80. * Encode JSON response and immediately send
  81. *
  82. * @param mixed $data
  83. * @param boolean|array $keepLayouts
  84. * NOTE: if boolean, establish $keepLayouts to true|false
  85. * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  86. * if $keepLayouts and parmas for Zend_Json::encode are required
  87. * then, the array can contains a 'keepLayout'=>true|false
  88. * that will not be passed to Zend_Json::encode method but will be passed
  89. * to Zend_View_Helper_Json
  90. * @return string|void
  91. */
  92. public function sendJson($data, $keepLayouts = false)
  93. {
  94. $data = $this->encodeJson($data, $keepLayouts);
  95. $response = $this->getResponse();
  96. $response->setBody($data);
  97. if (!$this->suppressExit) {
  98. $response->sendResponse();
  99. exit;
  100. }
  101. return $data;
  102. }
  103. /**
  104. * Strategy pattern: call helper as helper broker method
  105. *
  106. * Allows encoding JSON. If $sendNow is true, immediately sends JSON
  107. * response.
  108. *
  109. * @param mixed $data
  110. * @param boolean $sendNow
  111. * @param boolean $keepLayouts
  112. * @return string|void
  113. */
  114. public function direct($data, $sendNow = true, $keepLayouts = false)
  115. {
  116. if ($sendNow) {
  117. return $this->sendJson($data, $keepLayouts);
  118. }
  119. return $this->encodeJson($data, $keepLayouts);
  120. }
  121. }