2
0

Json.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2015 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-2015 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. * @param boolean $encodeData Provided data is already JSON
  54. * NOTE: if boolean, establish $keepLayouts to true|false
  55. * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  56. * if $keepLayouts and parmas for Zend_Json::encode are required
  57. * then, the array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
  58. * that will not be passed to Zend_Json::encode method but will be passed
  59. * to Zend_View_Helper_Json
  60. * @throws Zend_Controller_Action_Helper_Json
  61. * @return string
  62. */
  63. public function encodeJson($data, $keepLayouts = false, $encodeData = true)
  64. {
  65. /**
  66. * @see Zend_View_Helper_Json
  67. */
  68. require_once 'Zend/View/Helper/Json.php';
  69. $jsonHelper = new Zend_View_Helper_Json();
  70. $data = $jsonHelper->json($data, $keepLayouts, $encodeData);
  71. if (!$keepLayouts) {
  72. /**
  73. * @see Zend_Controller_Action_HelperBroker
  74. */
  75. require_once 'Zend/Controller/Action/HelperBroker.php';
  76. Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
  77. }
  78. return $data;
  79. }
  80. /**
  81. * Encode JSON response and immediately send
  82. *
  83. * @param mixed $data
  84. * @param boolean|array $keepLayouts
  85. * @param $encodeData Encode $data as JSON?
  86. * NOTE: if boolean, establish $keepLayouts to true|false
  87. * if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  88. * if $keepLayouts and parmas for Zend_Json::encode are required
  89. * then, the array can contains a 'keepLayout'=>true|false and/or 'encodeData'=>true|false
  90. * that will not be passed to Zend_Json::encode method but will be passed
  91. * to Zend_View_Helper_Json
  92. * @return string|void
  93. */
  94. public function sendJson($data, $keepLayouts = false, $encodeData = true)
  95. {
  96. $data = $this->encodeJson($data, $keepLayouts, $encodeData);
  97. $response = $this->getResponse();
  98. $response->setBody($data);
  99. if (!$this->suppressExit) {
  100. $response->sendResponse();
  101. exit;
  102. }
  103. return $data;
  104. }
  105. /**
  106. * Strategy pattern: call helper as helper broker method
  107. *
  108. * Allows encoding JSON. If $sendNow is true, immediately sends JSON
  109. * response.
  110. *
  111. * @param mixed $data
  112. * @param boolean $sendNow
  113. * @param boolean $keepLayouts
  114. * @param boolean $encodeData Encode $data as JSON?
  115. * @return string|void
  116. */
  117. public function direct($data, $sendNow = true, $keepLayouts = false, $encodeData = true)
  118. {
  119. if ($sendNow) {
  120. return $this->sendJson($data, $keepLayouts, $encodeData);
  121. }
  122. return $this->encodeJson($data, $keepLayouts, $encodeData);
  123. }
  124. }