Пример #1
0
 public function testDuplicatePlugin()
 {
     $broker = new Zend_Controller_Plugin_Broker();
     $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
     $broker->registerPlugin($plugin);
     try {
         $broker->registerPlugin($plugin);
         $this->fail('Duplicate registry of plugin object should be disallowed');
     } catch (Exception $expected) {
         $this->assertContains('already', $expected->getMessage());
     }
 }
Пример #2
0
 public function forceExit()
 {
     if (APPLICATION_ENV != "testing") {
         Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer')->setNoRender(true);
     }
     $plugins = Zend_Controller_Front::getInstance()->getPlugins();
     $broker = new Zend_Controller_Plugin_Broker();
     $broker->setRequest(Zend_Controller_Front::getInstance()->getRequest());
     $broker->setResponse(Zend_Controller_Front::getInstance()->getResponse());
     foreach ($plugins as $index => $plugin) {
         $broker->registerPlugin($plugin, $index);
     }
     try {
         $broker->postDispatch($broker->getRequest());
     } catch (Exception $e) {
         \App::log()->crit('Error executing "postDispatch" after stream');
         \App::log()->crit($e);
     }
     try {
         $broker->dispatchLoopShutdown();
     } catch (Exception $e) {
         \App::log()->crit('Error executing "dispatchLoopShutdown" after stream');
         \App::log()->crit($e);
     }
     if (APPLICATION_ENV !== 'testing') {
         exit;
     }
 }
Пример #3
0
 /**
  * Dispatch an HTTP request to a controller/action.
  */
 public function dispatch()
 {
     /* @var $action Zend_Controller_Dispatcher_Token */
     try {
         // notify plugins that the router is startup up
         $this->_plugins->routeStartup();
         /**
          * Route a URI to a controller/action.  If the route cannot be
          * made, an exception is thrown.
          */
         try {
             $action = $this->getRouter()->route($this->getDispatcher());
         } catch (Zend_Controller_Router_Exception $e) {
             // Failed routing (basically a 404, no controller found)
             // change the action
             $action = new Zend_Controller_Dispatcher_Token('index', 'noRoute', array('error' => $e));
         }
         // notify plugins that the router is shutting down
         $action = $this->_plugins->routeShutdown($action);
         // notify plugins that the dispatch loop is starting up
         $action = $this->_plugins->dispatchLoopStartup($action);
         /**
          * Attempt to dispatch to the controller/action.  On return, either
          * false will be given to indicate completion, or Zend_Controller_Dispatcher_Token will be
          * given to indicate a forward to another controller/action must
          * be performed.
          */
         while ($action instanceof Zend_Controller_Dispatcher_Token) {
             // notify plugins that a dispatch is about to occur
             $action = $this->_plugins->preDispatch($action);
             $action = $this->getDispatcher()->dispatch($action);
             // notify plugins that the dispatch has finish
             $action = $this->_plugins->postDispatch($action);
         }
         // notify plugins that the dispatch loop is shutting down
         $this->_plugins->dispatchLoopShutdown();
     } catch (Exception $e) {
         // @todo exception processing
         //echo('EXCEPTION: ' . $e->getMessage());
         throw $e;
     }
 }
Пример #4
0
 /**
  * Dispatch an HTTP request to a controller/action.
  *
  * @param Zend_Controller_Request_Abstract|null $request
  * @param Zend_Controller_Response_Abstract|null $response
  * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
  */
 public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
 {
     if (!$this->getParam('noErrorHandler') && !$this->_plugins->hasPlugin('Zend_Controller_Plugin_ErrorHandler')) {
         // Register with stack index of 100
         #require_once 'Zend/Controller/Plugin/ErrorHandler.php';
         $this->_plugins->registerPlugin(new Zend_Controller_Plugin_ErrorHandler(), 100);
     }
     if (!$this->getParam('noViewRenderer') && !Zend_Controller_Action_HelperBroker::hasHelper('viewRenderer')) {
         #require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
         Zend_Controller_Action_HelperBroker::getStack()->offsetSet(-80, new Zend_Controller_Action_Helper_ViewRenderer());
     }
     /**
      * Instantiate default request object (HTTP version) if none provided
      */
     if (null !== $request) {
         $this->setRequest($request);
     } elseif (null === $request && null === ($request = $this->getRequest())) {
         #require_once 'Zend/Controller/Request/Http.php';
         $request = new Zend_Controller_Request_Http();
         $this->setRequest($request);
     }
     /**
      * Set base URL of request object, if available
      */
     if (is_callable(array($this->_request, 'setBaseUrl'))) {
         if (null !== $this->_baseUrl) {
             $this->_request->setBaseUrl($this->_baseUrl);
         }
     }
     /**
      * Instantiate default response object (HTTP version) if none provided
      */
     if (null !== $response) {
         $this->setResponse($response);
     } elseif (null === $this->_response && null === ($this->_response = $this->getResponse())) {
         #require_once 'Zend/Controller/Response/Http.php';
         $response = new Zend_Controller_Response_Http();
         $this->setResponse($response);
     }
     /**
      * Register request and response objects with plugin broker
      */
     $this->_plugins->setRequest($this->_request)->setResponse($this->_response);
     /**
      * Initialize router
      */
     $router = $this->getRouter();
     $router->setParams($this->getParams());
     /**
      * Initialize dispatcher
      */
     $dispatcher = $this->getDispatcher();
     $dispatcher->setParams($this->getParams())->setResponse($this->_response);
     // Begin dispatch
     try {
         /**
          * Route request to controller/action, if a router is provided
          */
         /**
          * Notify plugins of router startup
          */
         $this->_plugins->routeStartup($this->_request);
         $router->route($this->_request);
         /**
          * Notify plugins of router completion
          */
         $this->_plugins->routeShutdown($this->_request);
         /**
          * Notify plugins of dispatch loop startup
          */
         $this->_plugins->dispatchLoopStartup($this->_request);
         /**
          *  Attempt to dispatch the controller/action. If the $this->_request
          *  indicates that it needs to be dispatched, move to the next
          *  action in the request.
          */
         do {
             $this->_request->setDispatched(true);
             /**
              * Notify plugins of dispatch startup
              */
             $this->_plugins->preDispatch($this->_request);
             /**
              * Skip requested action if preDispatch() has reset it
              */
             if (!$this->_request->isDispatched()) {
                 continue;
             }
             /**
              * Dispatch request
              */
             try {
                 $dispatcher->dispatch($this->_request, $this->_response);
             } catch (Exception $e) {
                 if ($this->throwExceptions()) {
                     throw $e;
                 }
                 $this->_response->setException($e);
             }
             /**
              * Notify plugins of dispatch completion
              */
             $this->_plugins->postDispatch($this->_request);
         } while (!$this->_request->isDispatched());
     } catch (Exception $e) {
         if ($this->throwExceptions()) {
             throw $e;
         }
         $this->_response->setException($e);
     }
     /**
      * Notify plugins of dispatch loop completion
      */
     try {
         $this->_plugins->dispatchLoopShutdown();
     } catch (Exception $e) {
         if ($this->throwExceptions()) {
             throw $e;
         }
         $this->_response->setException($e);
     }
     if ($this->returnResponse()) {
         return $this->_response;
     }
     $this->_response->sendResponse();
 }
Пример #5
0
 /**
  * Test for ZF-2305
  * @return void
  */
 public function testRegisterPluginSetsRequestAndResponse()
 {
     $broker = new Zend_Controller_Plugin_Broker();
     $request = new Zend_Controller_Request_Simple();
     $response = new Zend_Controller_Response_Cli();
     $broker->setRequest($request);
     $broker->setResponse($response);
     $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
     $broker->registerPlugin($plugin);
     $this->assertSame($request, $plugin->getRequest());
     $this->assertSame($response, $plugin->getResponse());
 }
Пример #6
0
 /**
  * Dispatch an HTTP request to a controller/action.
  *
  * @param Zend_Controller_Request_Abstract|null $request
  * @param Zend_Controller_Response_Abstract|null $response
  * @return void|Zend_Controller_Response_Abstract Returns response object if returnResponse() is true
  */
 public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
 {
     /**
      * Instantiate default request object (HTTP version) if none provided
      */
     if (null !== $request) {
         $this->setRequest($request);
     } elseif (null === $request && null === ($request = $this->getRequest())) {
         require_once 'Zend/Controller/Request/Http.php';
         $request = new Zend_Controller_Request_Http();
         $this->setRequest($request);
     }
     /**
      * Set base URL of request object, if available
      */
     if (is_callable(array($request, 'setBaseUrl'))) {
         if (null !== ($baseUrl = $this->getBaseUrl())) {
             $request->setBaseUrl($baseUrl);
         }
     }
     /**
      * Instantiate default response object (HTTP version) if none provided
      */
     if (null !== $response) {
         $this->setResponse($response);
     } elseif (null === $response && null === ($response = $this->getResponse())) {
         require_once 'Zend/Controller/Response/Http.php';
         $response = new Zend_Controller_Response_Http();
         $this->setResponse($response);
     }
     /**
      * Register request and response objects with plugin broker
      */
     $this->_plugins->setRequest($request)->setResponse($response);
     /**
      * Initialize router
      */
     $router = $this->getRouter();
     $router->setParams($this->getParams());
     /**
      * Initialize dispatcher
      */
     $dispatcher = $this->getDispatcher();
     $dispatcher->setParams($this->getParams())->setResponse($response);
     // Begin dispatch
     try {
         /**
          * Route request to controller/action, if a router is provided
          */
         /**
          * Notify plugins of router startup
          */
         $this->_plugins->routeStartup($request);
         $router->route($request);
         /**
          * Notify plugins of router completion
          */
         $this->_plugins->routeShutdown($request);
         /**
          * Notify plugins of dispatch loop startup
          */
         $this->_plugins->dispatchLoopStartup($request);
         /**
          *  Attempt to dispatch the controller/action. If the $request
          *  indicates that it needs to be dispatched, move to the next
          *  action in the request.
          */
         do {
             $request->setDispatched(true);
             /**
              * Notify plugins of dispatch startup
              */
             $this->_plugins->preDispatch($request);
             /**
              * Skip requested action if preDispatch() has reset it
              */
             if (!$request->isDispatched()) {
                 continue;
             }
             /**
              * Dispatch request
              */
             $dispatcher->dispatch($request, $response);
             /**
              * Notify plugins of dispatch completion
              */
             $this->_plugins->postDispatch($request);
         } while (!$request->isDispatched());
     } catch (Exception $e) {
         if ($this->throwExceptions()) {
             throw $e;
         }
         $response->setException($e);
     }
     /**
      * Notify plugins of dispatch loop completion
      */
     try {
         $this->_plugins->dispatchLoopShutdown();
     } catch (Exception $e) {
         if ($this->throwExceptions()) {
             throw $e;
         }
         $response->setException($e);
     }
     if ($this->returnResponse()) {
         return $response;
     }
     $response->sendHeaders();
     $response->outputBody();
 }
Пример #7
0
 /**
  * Dispatch an HTTP request to a controller/action.
  *
  * @param Zend_Controller_Request_Abstract|null $request
  * @param Zend_Controller_Response_Abstract|null $response
  * @return Zend_Controller_Response_Abstract
  */
 public function dispatch(Zend_Controller_Request_Abstract $request = null, Zend_Controller_Response_Abstract $response = null)
 {
     /**
      * Instantiate default request object (HTTP version) if none provided
      */
     if (null === $request && null === ($request = $this->getRequest())) {
         Zend::loadClass('Zend_Controller_Request_Http');
         $request = new Zend_Controller_Request_Http();
     }
     /**
      * Instantiate default response object (HTTP version) if none provided
      */
     if (null === $response && null === ($response = $this->getResponse())) {
         Zend::loadClass('Zend_Controller_Response_Http');
         $response = new Zend_Controller_Response_Http();
     }
     /**
      * Register request and response objects with plugin broker
      */
     $this->_plugins->setRequest($request)->setResponse($response);
     // Begin dispatch
     try {
         /**
          * Route request to controller/action, if a router is provided
          */
         if (null !== ($router = $this->getRouter())) {
             /**
              * Notify plugins of router startup
              */
             $this->_plugins->routeStartup($request);
             $router->setParams($this->getParams());
             $router->route($request);
             /**
              * Notify plugins of router completion
              */
             $this->_plugins->routeShutdown($request);
         }
         /**
          * Notify plugins of dispatch loop startup
          */
         $this->_plugins->dispatchLoopStartup($request);
         $dispatcher = $this->getDispatcher();
         $dispatcher->setParams($this->getParams());
         /**
          *  Attempt to dispatch the controller/action. If the $request
          *  indicates that it needs to be dispatched, move to the next
          *  action in the request.
          */
         do {
             $request->setDispatched(true);
             /**
              * Notify plugins of dispatch startup
              */
             $this->_plugins->preDispatch($request);
             /**
              * Skip requested action if preDispatch() has reset it
              */
             if (!$request->isDispatched()) {
                 continue;
             }
             /**
              * Dispatch request
              */
             $dispatcher->dispatch($request, $response);
             /**
              * Notify plugins of dispatch completion
              */
             $this->_plugins->postDispatch($request);
         } while (!$request->isDispatched());
         /**
          * Notify plugins of dispatch loop completion
          */
         $this->_plugins->dispatchLoopShutdown();
     } catch (Exception $e) {
         $response->setException($e);
     }
     return $response;
 }
Пример #8
0
    public function testRegisterPluginStackOrderWithAutmaticNumbersIncrementsCorrectly()
    {
        $broker   = new Zend_Controller_Plugin_Broker();
        $plugin1  = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
        $plugin2  = new Zend_Controller_Plugin_BrokerTest_ExceptionTestPlugin();
        $plugin3  = new Zend_Controller_Plugin_BrokerTest_TestPlugin2();
        $broker->registerPlugin($plugin1, 2);
        $broker->registerPlugin($plugin2, 3);
        $broker->registerPlugin($plugin3);

        $plugins = $broker->getPlugins();
        $expected = array(2 => $plugin1, 3 => $plugin2, 4 => $plugin3);
        $this->assertSame($expected, $plugins);
    }
Пример #9
0
    public function testHasPlugin()
    {
        $broker = new Zend_Controller_Plugin_Broker();
        $this->assertFalse($broker->hasPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin'));

        $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
        $broker->registerPlugin($plugin);
        $this->assertTrue($broker->hasPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin'));
    }
Пример #10
0
 public function testGetPluginByNameReturnsArray()
 {
     $broker = new Zend_Controller_Plugin_Broker();
     $plugin = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
     $broker->registerPlugin($plugin);
     $plugin2 = new Zend_Controller_Plugin_BrokerTest_TestPlugin();
     $broker->registerPlugin($plugin2);
     $retrieved = $broker->getPlugin('Zend_Controller_Plugin_BrokerTest_TestPlugin');
     $this->assertTrue(is_array($retrieved));
     $this->assertEquals(2, count($retrieved));
     $this->assertSame($plugin, $retrieved[0]);
     $this->assertSame($plugin2, $retrieved[1]);
 }