Ejemplo n.º 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;
    }
Ejemplo n.º 2
0
 /**
  * Retrieve layout object
  *
  * @return \Zend\Layout\Layout
  */
 public function getLayout()
 {
     if (null === $this->_layout) {
         $this->_layout = \Zend\Layout\Layout::startMvc($this->getOptions());
     }
     return $this->_layout;
 }
Ejemplo n.º 3
0
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @return void
     */
    public function setUp()
    {
        \Zend\Layout\Layout::resetMvcInstance();

        $this->front = Controller\Front::getInstance();
        $this->front->resetInstance();
        $this->front->addModuleDirectory(__DIR__ . '/../../_files/modules');
        $this->broker = $this->front->getHelperBroker();

        $this->layout = Layout\Layout::startMvc();

        $this->helper = new Helper\ContextSwitch();
        $this->broker->register('contextswitch', $this->helper);

        $this->request = new \Zend\Controller\Request\Http();
        $this->response = new \Zend\Controller\Response\Cli();

        $this->front->setRequest($this->request)
                    ->setResponse($this->response)
                    ->addControllerDirectory(__DIR__);

        $this->view = new \Zend\View\PhpRenderer();
        $this->viewRenderer = $this->broker->load('viewRenderer');
        $this->viewRenderer->setView($this->view);

        $this->controller = new ContextSwitchTestController(
            $this->request,
            $this->response,
            array()
        );
        $this->controller->setHelperBroker($this->broker);
        $this->controller->setupContexts();
        $this->helper->setActionController($this->controller);
    }
Ejemplo n.º 4
0
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     *
     * @return void
     */
    public function setUp()
    {
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
            unset($_SERVER['HTTP_X_REQUESTED_WITH']);
        }

        \Zend\Layout\Layout::resetMvcInstance();

        $this->front = \Zend\Controller\Front::getInstance();
        $this->front->resetInstance();
        $this->front->addModuleDirectory(__DIR__ . '/../../_files/modules');
        $this->broker = $this->front->getHelperBroker();

        $this->layout = Layout\Layout::startMvc();

        $this->helper = new \Zend\Controller\Action\Helper\AjaxContext();

        $this->request = new \Zend\Controller\Request\Http();
        $this->response = new \Zend\Controller\Response\Cli();

        $this->front->setRequest($this->request)->setResponse($this->response);
        $this->view = new \Zend\View\PhpRenderer();
        $this->viewRenderer = $this->broker->load('viewRenderer');
        $this->viewRenderer->setView($this->view);

        $this->controller = new AjaxContextTestController(
            $this->request,
            $this->response,
            array()
        );
        $this->helper->setActionController($this->controller);
    }
Ejemplo n.º 5
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;
 }
Ejemplo n.º 6
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;
 }
Ejemplo n.º 7
0
    /**
     * Sends mail to recipient(s) if log entries are present.  Note that both
     * plaintext and HTML portions of email are handled here.
     *
     * @return void
     */
    public function shutdown()
    {
        // If there are events to mail, use them as message body.  Otherwise,
        // there is no mail to be sent.
        if (empty($this->_eventsToMail)) {
            return;
        }

        if ($this->_subjectPrependText !== null) {
            // Tack on the summary of entries per-priority to the subject
            // line and set it on the Zend_Mail object.
            $numEntries = $this->_getFormattedNumEntriesPerPriority();
            $this->_mail->setSubject(
                "{$this->_subjectPrependText} ({$numEntries})");
        }


        // Always provide events to mail as plaintext.
        $this->_mail->setBodyText(implode('', $this->_eventsToMail));

        // If a Zend_Layout instance is being used, set its "events"
        // value to the lines formatted for use with the layout.
        if ($this->_layout) {
            // Set the required "messages" value for the layout.  Here we
            // are assuming that the layout is for use with HTML.
            $this->_layout->events =
                implode('', $this->_layoutEventsToMail);

            // If an exception occurs during rendering, convert it to a notice
            // so we can avoid an exception thrown without a stack frame.
            try {
                $this->_mail->setBodyHtml($this->_layout->render());
            } catch (\Exception $e) {
                trigger_error(
                    "exception occurred when rendering layout; " .
                        "unable to set html body for message; " .
                        "message = {$e->getMessage()}; " .
                        "code = {$e->getCode()}; " .
                        "exception class = " . get_class($e),
                    E_USER_NOTICE);
            }
        }

        // Finally, send the mail.  If an exception occurs, convert it into a
        // warning-level message so we can avoid an exception thrown without a
        // stack frame.
        try {
            $this->_mail->send();
        } catch (\Exception $e) {
            trigger_error(
                "unable to send log entries via email; " .
                    "message = {$e->getMessage()}; " .
                    "code = {$e->getCode()}; " .
                        "exception class = " . get_class($e),
                E_USER_WARNING);
        }
    }
Ejemplo n.º 8
0
 public function appBootstrap()
 {
     $this->frontController->setControllerDirectory(__DIR__ . '/_files/functional-test-app/controllers/');
     // create an instance of the ErrorHandler so we can make sure it will point to our specially named ErrorController
     $plugin = new \Zend\Controller\Plugin\ErrorHandler();
     $plugin->setErrorHandlerController('zend-layout-functional-test-error')->setErrorHandlerAction('error');
     $this->frontController->registerPlugin($plugin, 100);
     \Zend\Layout\Layout::startMvc(__DIR__ . '/_files/functional-test-app/layouts/');
 }
Ejemplo n.º 9
0
 /**
  * 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;
 }
Ejemplo n.º 10
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     Layout::resetMvcInstance();
     $this->request = new HTTPRequest();
     $this->response = new CLIResponse();
     $this->front = FrontController::getInstance();
     $this->front->resetInstance();
     $this->front->setRequest($this->request)->setResponse($this->response);
     $this->broker = $this->front->getHelperBroker();
     $this->viewRenderer = $this->broker->load('viewRenderer');
     $this->layout = Layout::startMvc();
 }
Ejemplo n.º 11
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     Layout::resetMvcInstance();
     HelperBroker::resetHelpers();
     HelperBroker::setPluginLoader(null);
     $this->request = new HTTPRequest();
     $this->response = new CLIResponse();
     $this->front = FrontController::getInstance();
     $this->front->resetInstance();
     $this->front->setRequest($this->request)->setResponse($this->response);
     $this->viewRenderer = HelperBroker::getStaticHelper('viewRenderer');
     $this->layout = Layout::startMvc();
 }
Ejemplo n.º 12
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     $this->markTestSkipped('Skipped until DOJO is converted to Zend\\Dojo');
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker\HelperBroker::resetHelpers();
     HelperBroker\HelperBroker::setPluginLoader(null);
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\Cli();
     $this->front = \Zend\Controller\Front::getInstance();
     $this->front->resetInstance();
     $this->front->setRequest($this->request)->setResponse($this->response);
     $this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->layout = Layout\Layout::startMvc();
 }
Ejemplo n.º 13
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 __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 = JsonFormatter::encode($data, null, $options);
     if (!$keepLayouts && $this->layout instanceof Layout) {
         $this->layout->disableLayout();
     }
     if ($this->response instanceof Response) {
         $headers = $this->response->headers();
         $headers->addHeaderLine('Content-Type', 'application/json');
     }
     return $data;
 }
Ejemplo n.º 14
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 __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;
 }
Ejemplo n.º 15
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker\HelperBroker::resetHelpers();
     $this->front = Controller\Front::getInstance();
     $this->front->resetInstance();
     $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
     $this->layout = Layout\Layout::startMvc();
     $this->helper = new Helper\ContextSwitch();
     HelperBroker\HelperBroker::addHelper($this->helper);
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\Cli();
     $this->front->setRequest($this->request)->setResponse($this->response)->addControllerDirectory(dirname(__FILE__));
     $this->view = new \Zend\View\View();
     $this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->viewRenderer->setView($this->view);
     $this->controller = new ContextSwitchTestController($this->request, $this->response, array());
     $this->controller->setupContexts();
     $this->helper->setActionController($this->controller);
 }
Ejemplo n.º 16
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();
 }
Ejemplo n.º 17
0
 /**
  * Sets up the fixture, for example, open a network connection.
  * This method is called before a test is executed.
  *
  * @return void
  */
 public function setUp()
 {
     if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
         unset($_SERVER['HTTP_X_REQUESTED_WITH']);
     }
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker\HelperBroker::resetHelpers();
     HelperBroker\HelperBroker::addPrefix('Zend\\Controller\\Action\\Helper');
     $this->front = \Zend\Controller\Front::getInstance();
     $this->front->resetInstance();
     $this->front->addModuleDirectory(dirname(__FILE__) . '/../../_files/modules');
     $this->layout = Layout\Layout::startMvc();
     $this->helper = new \Zend\Controller\Action\Helper\AjaxContext();
     $this->request = new \Zend\Controller\Request\HTTP();
     $this->response = new \Zend\Controller\Response\Cli();
     $this->front->setRequest($this->request)->setResponse($this->response);
     $this->view = new \Zend\View\View();
     $this->view->addHelperPath(dirname(__FILE__) . '/../../../../../library/Zend/View/Helper/');
     $this->viewRenderer = HelperBroker\HelperBroker::getStaticHelper('viewRenderer');
     $this->viewRenderer->setView($this->view);
     $this->controller = new AjaxContextTestController($this->request, $this->response, array());
     $this->helper->setActionController($this->controller);
 }
Ejemplo n.º 18
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;
 }
Ejemplo n.º 19
0
 public function testJsonHelperDoesNotDisableLayoutsWhenKeepLayoutFlagTrue()
 {
     $layout = Layout\Layout::startMvc();
     $this->assertTrue($layout->isEnabled());
     $data = $this->helper->direct(array('foobar'), true);
     $this->assertTrue($layout->isEnabled());
 }
Ejemplo n.º 20
0
Archivo: Layout.php Proyecto: hjr3/zf2
 /**
  * Static method for initialization with MVC support
  *
  * @param  string|array|\Zend\Config\Config $options
  * @return \Zend\Layout\Layout
  */
 public static function startMvc($options = null)
 {
     if (null === self::$_mvcInstance) {
         self::$_mvcInstance = new self($options, true);
     }
     if (is_string($options)) {
         self::$_mvcInstance->setLayoutPath($options);
     } elseif (is_array($options) || $options instanceof Config\Config) {
         self::$_mvcInstance->setOptions($options);
     }
     return self::$_mvcInstance;
 }
Ejemplo n.º 21
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;
 }
Ejemplo n.º 22
0
 public function testHelperMethodFetchesLayoutObject()
 {
     $layout = Layout\Layout::startMvc();
     $helper = new Helper\Layout();
     $received = $helper->direct();
     $this->assertSame($layout, $received);
 }
Ejemplo n.º 23
0
 /**
  * @group ZF-5891
  */
 public function testSetLayoutWithDisabledFlag()
 {
     $layout = new Layout\Layout();
     $layout->disableLayout();
     $layout->setLayout('foo', false);
     $this->assertEquals('foo', $layout->getLayout());
     $this->assertFalse($layout->isEnabled());
 }
Ejemplo n.º 24
0
 public function testCanKeepLayoutsWhenUsingDirect()
 {
     $layout = Layout\Layout::startMvc();
     $data = $this->helper->direct(array('foobar'), false, true);
     $this->assertTrue($layout->isEnabled());
     $this->assertFalse($this->viewRenderer->getNoRender());
 }
Ejemplo n.º 25
0
 public function testHelperProxiesToLayoutObjectMethods()
 {
     $layout = Layout\Layout::startMvc();
     $helper = new Helper\Layout();
     $helper->setOptions(array('layout' => 'foo.phtml', 'layoutPath' => __DIR__ . '/_files/layouts', 'contentKey' => 'foo'));
     $this->assertEquals('foo.phtml', $helper->getLayout());
     $this->assertEquals(__DIR__ . '/_files/layouts', $helper->getLayoutPath());
     $this->assertEquals('foo', $helper->getContentKey());
 }
Ejemplo n.º 26
0
 /**
  * Reset MVC state
  *
  * Creates new request/response objects, resets the front controller
  * instance, and resets the action helper broker.
  *
  * @todo   Need to update Zend_Layout to add a resetInstance() method
  * @return void
  */
 public function reset()
 {
     $_SESSION = array();
     $_GET = array();
     $_POST = array();
     $_COOKIE = array();
     $this->resetRequest();
     $this->resetResponse();
     \Zend\Layout\Layout::resetMvcInstance();
     HelperBroker::resetHelpers();
     $this->frontController->resetInstance();
     //\Zend\Session\Manager::$_unitTestEnabled = true;
 }
Ejemplo n.º 27
0
    /**
     * @group ZF-8041
     */
    public function testPostDispatchDoesNotRenderLayoutWhenResponseRedirected()
    {
        $front    = Controller\Front::getInstance();
        $request  = new Request\Simple();
        $response = new Response\Cli();

        $request->setDispatched(true);
        $response->setHttpResponseCode(302);
        $response->setBody('Application content');
        $front->setRequest($request)
              ->setResponse($response);

        $layout = Layout\Layout::startMvc();
        $layout->setLayoutPath(__DIR__ . '/_files/layouts')
               ->setLayout('plugin.phtml')
               ->setMvcSuccessfulActionOnly(false)
               ->disableInflector();

        $plugin = $front->getPlugin('Zend\Layout\Controller\Plugin\Layout');
        $plugin->setResponse($response);
        $plugin->postDispatch($request);

        $body = $response->getBody();
        $this->assertContains('Application content', $body);
        $this->assertNotContains('Site Layout', $body);
    }
Ejemplo n.º 28
0
 /**
  * Reset MVC state
  *
  * Creates new request/response objects, resets the front controller
  * instance, and resets the action helper broker.
  *
  * @todo   Need to update Zend_Layout to add a resetInstance() method
  * @return void
  */
 public function reset()
 {
     $_SESSION = array();
     $_GET = array();
     $_POST = array();
     $_COOKIE = array();
     $this->resetRequest();
     $this->resetResponse();
     \Zend\Layout\Layout::resetMvcInstance();
     $this->frontController->resetInstance();
 }