Gc\Mvc\Listener\CacheListener::onRoute PHP Method

onRoute() public method

Load the page contents from the cache and set the response.
public onRoute ( MvcEvent $e ) : Zend\Stdlib\ResponseInterface | void
$e Zend\Mvc\MvcEvent Mvc Event
return Zend\Stdlib\ResponseInterface | void
    public function onRoute(MvcEvent $e)
    {
        if ($this->cacheIsActive($e) === false) {
            return;
        }
        if (!$e->getRequest() instanceof HttpRequest) {
            return;
        }
        $serviceManager = $e->getApplication()->getServiceManager();
        $data = $serviceManager->get('CacheService')->load($e);
        if ($data !== null) {
            $this->loadedFromCache = true;
            $response = unserialize($data);
            return $response;
        }
    }

Usage Example

Example #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testOnRouteWithCacheActivatedWithoutIdentityAndWithoutHttpRequest()
 {
     $config = Mockery::mock('Gc\\Core\\Config');
     $config->shouldReceive('getValue')->with('cache_is_active')->once()->andReturn(true);
     $identity = Mockery::mock('Zend\\Authentication\\AuthenticationService');
     $identity->shouldReceive('hasIdentity')->andReturn(false);
     $serviceLocator = Mockery::mock('Zend\\Mvc\\MvcEvent');
     $serviceLocator->shouldReceive('get')->once()->with('CoreConfig')->andReturn($config);
     $serviceLocator->shouldReceive('get')->once()->with('Auth')->andReturn($identity);
     $application = Mockery::Mock('Zend\\Mvc\\Application');
     $application->shouldReceive('getServiceManager')->once()->andReturn($serviceLocator);
     $event = Mockery::mock('Zend\\Mvc\\MvcEvent');
     $event->shouldReceive('getApplication')->once()->andReturn($application);
     $event->shouldReceive('getRequest')->once()->andReturn(null);
     $this->object->onRoute($event);
 }