示例#1
0
    /**
     * Encode data as JSON, disable layouts, and set response header
     *
     * If $keepLayouts is true, does not disable layouts.
     *
     * @param  mixed $data
     * @param  bool $keepLayouts
     * NOTE:   if boolean, establish $keepLayouts to true|false
     *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
     *         this array can contains a 'keepLayout'=>true|false
     *         that will not be passed to Zend_Json::encode method but will be used here
     * @return string|void
     */
    public function direct($data = null, $keepLayouts = false)
    {
        if ($data == null) {
            throw new \InvalidArgumentException('JSON: missing argument. $data is required in json($data, $keepLayouts = false)');
        }
        
        $options = array();
        if (is_array($keepLayouts))
        {
            $options     = $keepLayouts;
            $keepLayouts = (array_key_exists('keepLayouts', $keepLayouts))
                            ? $keepLayouts['keepLayouts']
                            : false;
            unset($options['keepLayouts']);
        }

        $data = \Zend\Json\Json::encode($data, null, $options);
        if (!$keepLayouts) {
            $layout = LayoutManager::getMvcInstance();
            if ($layout instanceof LayoutManager) {
                $layout->disableLayout();
            }
        }

        $response = \Zend\Controller\Front::getInstance()->getResponse();
        $response->setHeader('Content-Type', 'application/json');
        return $data;
    }
示例#2
0
 /**
  * Disable layouts and view renderer
  *
  * @return \Zend\Controller\Action\Helper\AutoComplete\AbstractAutoComplete Provides a fluent interface
  */
 public function disableLayouts()
 {
     if (null !== ($layout = Layout::getMvcInstance())) {
         $layout->disableLayout();
     }
     $this->getBroker()->load('viewRenderer')->setNoRender(true);
     return $this;
 }
示例#3
0
 /**
  * Disable layouts and view renderer
  *
  * @return \Zend\Controller\Action\Helper\AutoComplete\AbstractAutoComplete Provides a fluent interface
  */
 public function disableLayouts()
 {
     if (null !== ($layout = Layout::getMvcInstance())) {
         $layout->disableLayout();
     }
     HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
     return $this;
 }
示例#4
0
文件: Layout.php 项目: rexmac/zf2
 /**
  * Get layout object
  *
  * @return \Zend\Layout\Layout
  */
 public function getLayout()
 {
     if (null === $this->_layout) {
         $this->_layout = \Zend\Layout\Layout::getMvcInstance();
         if (null === $this->_layout) {
             // Implicitly creates layout object
             $this->_layout = new \Zend\Layout\Layout();
         }
     }
     return $this->_layout;
 }
示例#5
0
文件: Json.php 项目: bradley-holt/zf2
 /**
  * Encode data as JSON, disable layouts, and set response header
  *
  * If $keepLayouts is true, does not disable layouts.
  *
  * @param  mixed $data
  * @param  bool $keepLayouts
  * NOTE:   if boolean, establish $keepLayouts to true|false
  *         if array, admit params for Zend_Json::encode as enableJsonExprFinder=>true|false
  *         this array can contains a 'keepLayout'=>true|false
  *         that will not be passed to Zend_Json::encode method but will be used here
  * @return string|void
  */
 public function __invoke($data, $keepLayouts = false)
 {
     $options = array();
     if (is_array($keepLayouts)) {
         $options = $keepLayouts;
         $keepLayouts = array_key_exists('keepLayouts', $keepLayouts) ? $keepLayouts['keepLayouts'] : false;
         unset($options['keepLayouts']);
     }
     $data = \Zend\Json\Json::encode($data, null, $options);
     if (!$keepLayouts) {
         $layout = LayoutManager::getMvcInstance();
         if ($layout instanceof LayoutManager) {
             $layout->disableLayout();
         }
     }
     $response = \Zend\Controller\Front::getInstance()->getResponse();
     $response->setHeader('Content-Type', 'application/json');
     return $data;
 }
示例#6
0
 /**
  * Prepare data for autocompletion
  *
  * @param  mixed   $data
  * @param  boolean $keepLayouts
  * @return string
  */
 public function prepareAutoCompletion($data, $keepLayouts = false)
 {
     if (!$data instanceof DojoData) {
         $items = array();
         foreach ($data as $key => $value) {
             $items[] = array('label' => $value, 'name' => $value);
         }
         $data = new DojoData('name', $items);
     }
     if (!$keepLayouts) {
         $this->getBroker()->load('viewRenderer')->setNoRender(true);
         $layout = Layout::getMvcInstance();
         if ($layout instanceof Layout) {
             $layout->disableLayout();
         }
     }
     $response = FrontController::getInstance()->getResponse();
     $response->setHeader('Content-Type', 'application/json');
     return $data->toJson();
 }
示例#7
0
文件: LayoutTest.php 项目: rexmac/zf2
 /**
  * @group ZF-5152
  */
 public function testCallingStartMvcTwiceDoesntGenerateAnyUnexpectedBehavior()
 {
     Layout\Layout::startMvc('/some/path');
     $this->assertEquals(Layout\Layout::getMvcInstance()->getLayoutPath(), '/some/path');
     Layout\Layout::startMvc('/some/other/path');
     $this->assertEquals(Layout\Layout::getMvcInstance()->getLayoutPath(), '/some/other/path');
     $this->assertTrue(Layout\Layout::getMvcInstance()->isEnabled());
 }
示例#8
0
 /**
  * Initialize context detection and switching
  *
  * @param  mixed $format
  * @throws \Zend\Controller\Action\Exception
  * @return void
  */
 public function initContext($format = null)
 {
     $this->_currentContext = null;
     $controller = $this->getActionController();
     $request = $this->getRequest();
     $action = $request->getActionName();
     // Return if no context switching enabled, or no context switching
     // enabled for this action
     $contexts = $this->getActionContexts($action);
     if (empty($contexts)) {
         return;
     }
     // Return if no context parameter provided
     if (!($context = $request->getParam($this->getContextParam()))) {
         if ($format === null) {
             return;
         }
         $context = $format;
         $format = null;
     }
     // Check if context allowed by action controller
     if (!$this->hasActionContext($action, $context)) {
         return;
     }
     // Return if invalid context parameter provided and no format or invalid
     // format provided
     if (!$this->hasContext($context)) {
         if (empty($format) || !$this->hasContext($format)) {
             return;
         }
     }
     // Use provided format if passed
     if (!empty($format) && $this->hasContext($format)) {
         $context = $format;
     }
     $suffix = $this->getSuffix($context);
     $this->_getViewRenderer()->setViewSuffix($suffix);
     $headers = $this->getHeaders($context);
     if (!empty($headers)) {
         $response = $this->getResponse();
         foreach ($headers as $header => $content) {
             $response->setHeader($header, $content);
         }
     }
     if ($this->getAutoDisableLayout()) {
         $layout = \Zend\Layout\Layout::getMvcInstance();
         if (null !== $layout) {
             $layout->disableLayout();
         }
     }
     if (null !== ($callback = $this->getCallback($context, self::TRIGGER_INIT))) {
         if (is_string($callback) && method_exists($this, $callback)) {
             $this->{$callback}();
         } elseif (is_string($callback) && function_exists($callback)) {
             $callback();
         } elseif (is_array($callback)) {
             call_user_func($callback);
         } else {
             throw new Action\Exception(sprintf('Invalid context callback registered for context "%s"', $context));
         }
     }
     $this->_currentContext = $context;
 }
示例#9
0
 /**
  * Get layout object
  *
  * @return \Zend\Layout\Layout
  */
 public function getLayoutInstance()
 {
     if (null === $this->_layout) {
         if (null === ($this->_layout = \Zend\Layout\Layout::getMvcInstance())) {
             $this->_layout = new \Zend\Layout\Layout();
         }
     }
     return $this->_layout;
 }