web/lib/Zend/Controller/Action/Helper/Json.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: Json.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Controller_Action_Helper_Abstract
       
    25  */
       
    26 require_once 'Zend/Controller/Action/Helper/Abstract.php';
       
    27 
       
    28 /**
       
    29  * Simplify AJAX context switching based on requested format
       
    30  *
       
    31  * @uses       Zend_Controller_Action_Helper_Abstract
       
    32  * @category   Zend
       
    33  * @package    Zend_Controller
       
    34  * @subpackage Zend_Controller_Action_Helper
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */
       
    38 class Zend_Controller_Action_Helper_Json extends Zend_Controller_Action_Helper_Abstract
       
    39 {
       
    40     /**
       
    41      * Suppress exit when sendJson() called
       
    42      * @var boolean
       
    43      */
       
    44     public $suppressExit = false;
       
    45 
       
    46     /**
       
    47      * Create JSON response
       
    48      *
       
    49      * Encodes and returns data to JSON. Content-Type header set to
       
    50      * 'application/json', and disables layouts and viewRenderer (if being
       
    51      * used).
       
    52      *
       
    53      * @param  mixed   $data
       
    54      * @param  boolean $keepLayouts
       
    55      * @param  boolean|array $keepLayouts
       
    56      * NOTE:   if boolean, establish $keepLayouts to true|false
       
    57      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
       
    58      *         if $keepLayouts and parmas for Zend_Json::encode are required
       
    59      *         then, the array can contains a 'keepLayout'=>true|false
       
    60      *         that will not be passed to Zend_Json::encode method but will be passed
       
    61      *         to Zend_View_Helper_Json
       
    62      * @throws Zend_Controller_Action_Helper_Json
       
    63      * @return string
       
    64      */
       
    65     public function encodeJson($data, $keepLayouts = false)
       
    66     {
       
    67         /**
       
    68          * @see Zend_View_Helper_Json
       
    69          */
       
    70         require_once 'Zend/View/Helper/Json.php';
       
    71         $jsonHelper = new Zend_View_Helper_Json();
       
    72         $data = $jsonHelper->json($data, $keepLayouts);
       
    73 
       
    74         if (!$keepLayouts) {
       
    75             /**
       
    76              * @see Zend_Controller_Action_HelperBroker
       
    77              */
       
    78             require_once 'Zend/Controller/Action/HelperBroker.php';
       
    79             Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
       
    80         }
       
    81 
       
    82         return $data;
       
    83     }
       
    84 
       
    85     /**
       
    86      * Encode JSON response and immediately send
       
    87      *
       
    88      * @param  mixed   $data
       
    89      * @param  boolean|array $keepLayouts
       
    90      * NOTE:   if boolean, establish $keepLayouts to true|false
       
    91      *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
       
    92      *         if $keepLayouts and parmas for Zend_Json::encode are required
       
    93      *         then, the array can contains a 'keepLayout'=>true|false
       
    94      *         that will not be passed to Zend_Json::encode method but will be passed
       
    95      *         to Zend_View_Helper_Json
       
    96      * @return string|void
       
    97      */
       
    98     public function sendJson($data, $keepLayouts = false)
       
    99     {
       
   100         $data = $this->encodeJson($data, $keepLayouts);
       
   101         $response = $this->getResponse();
       
   102         $response->setBody($data);
       
   103 
       
   104         if (!$this->suppressExit) {
       
   105             $response->sendResponse();
       
   106             exit;
       
   107         }
       
   108 
       
   109         return $data;
       
   110     }
       
   111 
       
   112     /**
       
   113      * Strategy pattern: call helper as helper broker method
       
   114      *
       
   115      * Allows encoding JSON. If $sendNow is true, immediately sends JSON
       
   116      * response.
       
   117      *
       
   118      * @param  mixed   $data
       
   119      * @param  boolean $sendNow
       
   120      * @param  boolean $keepLayouts
       
   121      * @return string|void
       
   122      */
       
   123     public function direct($data, $sendNow = true, $keepLayouts = false)
       
   124     {
       
   125         if ($sendNow) {
       
   126             return $this->sendJson($data, $keepLayouts);
       
   127         }
       
   128         return $this->encodeJson($data, $keepLayouts);
       
   129     }
       
   130 }