Determine if we should cache the current request

@param  MvcEvent $mvcEvent
@return bool
496228
protected function shouldCacheRequest(MvcEvent $mvcEvent)
    {
        $event = $this->createCacheEvent(CacheEvent::EVENT_SHOULDCACHE, $mvcEvent);

        $results = $this->getEventManager()->triggerEventUntil(function ($result) {
            return $result;
        }, $event);

        if ($results->stopped()) {
            return $results->last();
        }

        return false;
    }
528389
public function onPostRenderPage(RendererEvent $event)
    {
        // Checks if page caching is available
        $this->object = $event->getTarget();
        if (false === ($this->object instanceof Page) || false === $this->checkCachePageEvent()) {
            return;
        }

        // Checks if cache_id is available
        if (false === $cache_id = $this->getPageCacheId()) {
            return;
        }

        $column_uids = array();
        foreach ($this->object->getContentSet() as $column) {
            if ($column instanceof AbstractClassContent) {
                $column_uids[] = $column->getUid();
            }
        }

        $lifetime = $this->cache_page->getMinExpireByTag($column_uids);
        $render = $event->getRender();
        $this->cache_page->save($cache_id, $render, $lifetime, $this->object->getUid());
        $this->application->debug(sprintf(
            'Save cache (id: %s, lifetime: %d) for rendering `%s(%s)` with mode `%s`.',
            $cache_id,
            $lifetime,
            get_class($this->object),
            $this->object->getUid(),
            $event->getRenderer()->getMode()
        ));
    }
1156050
public function onRoute(MvcEvent $event)
    {
        /** @var \Zend\Http\Request $request */
        $request = $event->getRequest();
        if (!$this->isAllowed($request)) {
            $event->setError('error-unauthorized-route');
            /* @var $app \Zend\Mvc\Application */
            $app = $event->getTarget();
            $exception = new UnauthorizedAccessException('You are not currently allowed access to this page');
            $event->setParam('exception', $exception);
            $app->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH_ERROR, $event);

            return false;
        }

        return true;
    }
426006
public function onDispatch(MvcEvent $e)
    {
        try {
            parent::onDispatch($e);
        } catch (\Exception $e) {
            return $this->getResponse()->setStatusCode(500)->setContent($e->__toString());
        }
    }
985785
protected function getCachedEvent($event)
    {
        $path = $event->getResource()->getPath();

        return isset($this->eventCache[$path])
            ? $this->eventCache[$path]
            : $this->eventCache[$path] = Carbon::now()->subDay();
    }
1019419
public function onDispatch(MvcEvent $e)
    {
        $routeMatch = $e->getRouteMatch();
        if (!$routeMatch) {
            /**
             * @todo Determine requirements for when route match is missing.
             *       Potentially allow pulling directly from request metadata?
             */
            throw new \Zend\Mvc\Exception\DomainException('Missing route matches; unsure how to retrieve action');
        }

        $action = $routeMatch->getParam('action', null);

        if ($action) {
            $action = strtolower($action);
        }

        switch ($action) {
            case 'process':
                $message = $routeMatch->getParam('message', null);

                if($message instanceof MessageInterface) {
                    $result = $this->process($message);
                } else {
                    throw new \InvalidArgumentException(
                        'Missing or invalid message type: must be an instace of '. MessageInterface::class
                    );
                }
                break;

            case 'send':
            case 'receive':
            case 'await':

                $queue = $routeMatch->getParam('queue', null);

                if (is_string($queue)) {
                    $queue = $this->getServiceLocator()->get($queue);
                }

                if (!$queue instanceof QueueClientInterface) {
                    throw new \InvalidArgumentException(
                        'Invalid queue param type: must be a string or an instace of '. QueueClientInterface::class
                    );
                }

                if ($action === 'send') {

                    $message = $routeMatch->getParam('message', null);
                    $parameters = $routeMatch->getParam('sendParameters', null);

                    if ($parameters === null) {
                        $params = null;
                    } else if ($parameters instanceof SendParametersInterface) {
                        $params = $parameters;
                    } else if (is_string($parameters)) {
                        $params = new SendParameters();
                        $params->fromString($parameters);
                    } else if (is_array($parameters)) {
                        $params = new SendParameters();
                        $params->fromArray($parameters);
                    } else {
                        throw new \InvalidArgumentException(
                            'Invalid sendParameters param type: must be null, an array, a string or an instace of '. SendParametersInterface::class
                        );
                    }

                    $result = $this->send($queue, $message, $params);
                } else {

                    $parameters = $routeMatch->getParam('receiveParameters', null);

                    if ($parameters === null) {
                        $params = null;
                    } else if ($parameters instanceof ReceiveParametersInterface) {
                        $params = $parameters;
                    } else if (is_string($parameters)) {
                        $params = new ReceiveParameters();
                        $params->fromString($parameters);
                    } else if (is_array($parameters)) {
                        $params = new ReceiveParameters();
                        $params->fromArray($parameters);
                    } else {
                        throw new \InvalidArgumentException(
                            'Invalid receiveParameters param type: must be null, an array, a string or an instace of '. QueueClientInterface::class
                        );
                    }

                    if ($action === 'receive') {
                        $maxMessages = (int) $routeMatch->getParam('maxMessages', 1);
                        $result = $this->receive($queue, $maxMessages, $params);
                    } else {
                        $result = $this->await($queue, $params);
                    }
                }
                break;

            default:
                throw new \InvalidArgumentException(
                    sprintf('Invalid action "%s". Only "process", "send", "receive", "await" are allowed', $action)
                );
        }


        $e->setResult($result);

        return $result;
    }
850888
public function onDispatch(MvcEvent $e)
    {
        $application = $e->getApplication();
        $sm = $application->getServiceManager();
        $front = $sm->get('FrontController');
        $front->returnResponse(true); // Response must be always returned
        $default = $front->getDefaultModule();
        if (null === $front->getControllerDirectory($default)) {
            throw new Exception\RuntimeException(
                'No default controller directory registered with front controller'
            );
        }
        $response = $front->dispatch();
        return $this->complete($response, $e);
    }
458715
public function refreshCache(RefreshCacheEvent $event)
    {
        foreach (OauthConfiguration::resourceOwners() as $resourceOwner) {
            $service = str_replace(' ', '_', strtolower($resourceOwner));
            $errors = $this->validateService(
                $service,
                $this->platformConfigHandler->getParameter($service.'_client_id'),
                $this->platformConfigHandler->getParameter($service.'_client_secret')
            );
            $event->addCacheParameter(
                "is_{$service}_available",
                (0 === count($errors) && $this->isActive($service))
            );
        }
    }
897156
public function onDispatch( MvcEvent $event )
    {
        $store   = $this->getStore();
        $request = $event->getRequest();
        $service = $this->getServiceLocator();
        $locale  = $service->get( 'AdminLocale' );
        $parent  = $request->getQuery( 'parentId' );

        if ( empty( $store['adminLocale'] ) )
        {
            $store['adminLocale'] = $locale->getCurrent();
        }

        if ( ! empty( $parent ) )
        {
            $store['parentId'] = $parent;
        }

        return parent::onDispatch( $event );
    }
958919
public function detectAccessDenied(MvcEvent $event)
    {
        $response = $event->getResponse();
        if ($response->getStatusCode() == 403) {
            $event->setError('Access Denied');
            $event->stopPropagation(true);
        }
    }
838955
public function onRequest(GetResponseEvent $event)
    {
        if (Zone::isBackend($event->getRequest())) {
            $this->setVersionChangeNotice();
        }

        // Twig globals
        $this->setGlobals();

        // Only cache if the config passes checks
        if ($this->app['config']->checkConfig() === false) {
            return;
        }

        // Final thing we do, if we're still standing, is to save our
        // configuration to cache
        if (!$this->app['config']->get('general/caching/config')) {
            return;
        }

        $this->app['config']->cacheConfig();
    }
797764
public function injectViewModelLayout(MvcEvent $e)
    {
        $model = $e->getResult();
        if (!Share::isOnAdmin() || !$model instanceof ViewModel){
            // - we are not on admin
            return false;
        }

        /** @var $response \Zend\Http\PhpEnvironment\Response */
        $response = $e->getResponse();
        if ($response->getStatusCode() != 400 && $response->getStatusCode() != 500) {
        	// set template prefix
        	$template = $model->getTemplate();
        	//$template = (! empty($tn)) ? $tn.'/'.$template : $template;
        	$template = 'admin'.'/'.$template;
        	$model->setTemplate($template);
        }
    }
1142912
protected function checkHttpMethod(MvcEvent $event, $routeConfig)
    {
        $request = $event->getRequest();
        if (!$request instanceof HttpRequest) {
            return false;
        }

        if (isset($routeConfig['http_methods'])) {
            $methods = (array) $routeConfig['http_methods'];
            if (!in_array($request->getMethod(), $methods)) {
                return false;
            }
        }

        return true;
    }
1196949
public function checkRedirect(MvcEvent $event)
    {
        if ($this->isConsoleRequest()) {
            return null;
        }

        // User defaults
        $redirectUrl = $this->redirectService->getRedirectUrl();

        if (empty($redirectUrl)) {
            return null;
        }


        $queryParams = $event->getRequest()->getQuery()->toArray();

        header(
            'Location: ' . $redirectUrl . (count($queryParams) ? '?' . http_build_query($queryParams) : null),
            true,
            302
        );
        exit;

        /* Below is the ZF2 way but Response is not short-circuiting the event like it should *
        $response = new Response();
        $response->setStatusCode(302);
        $response->getHeaders()
            ->addHeaderLine(
                'Location',
                $redirect->getRedirectUrl()
            );
        $event->stopPropagation(true);

        return $response;
        */
    }
560901
public function collect(MvcEvent $mvcEvent)
    {
        if (!$application = $mvcEvent->getApplication()) {
            return;
        }

        $serviceManager = $application->getServiceManager();

        /* @var \ZfcRbac\Service\RoleService $roleService */
        $roleService = $serviceManager->get('ZfcRbac\Service\RoleService');

        /* @var \ZfcRbac\Options\ModuleOptions $options */
        $options = $serviceManager->get('ZfcRbac\Options\ModuleOptions');

        // Start collect all the data we need!
        $this->collectOptions($options);
        $this->collectGuards($options->getGuards());
        $this->collectIdentityRolesAndPermissions($roleService);
    }
377180
public function storeRoute(MvcEvent $event)
    {
        if ($this->ignore) {
            return;
        }
        
        $request = $event->getRequest();
        if ($request->isXmlHttpRequest()) {
            return;
        }
        
        $appConfig = $event->getApplication()
            ->getServiceManager()
            ->get('config');
        $config = array_key_exists(self::CONFIG_KEY, $appConfig) ? $appConfig[self::CONFIG_KEY] : array();
        if (array_key_exists('blacklist', $config)) {
            $this->setBlacklist($config['blacklist']);
        }
        
        $routeMatch = $event->getRouteMatch();
        if (null === $routeMatch ||
             in_array($routeMatch->getMatchedRouteName(), $this->blacklist)) {
            return;
        }
        
        $this->getSession()->route = $routeMatch;
    }
1283784
protected function eventWasProcessed($event)
    {
        if ($this->eventExpired($event)) {
            return true;
        }

        $this->resetEventCache($event);

        return false;
    }