Late-binding asset routes.

@param MvcEvent $event
180250
public function onPreRoute(MvcEvent $event)
    {
    	// Get the router object
    	$serviceManager = $event->getApplication()->getServiceManager();
    	$router         = $event->getRouter();
    	
    	// Load the route configuration
    	$config = $serviceManager->get('Config');
	    $config = isset($config['asset_manager']) && (is_array($config['asset_manager']) || $config['asset_manager'] instanceof ArrayAccess)
	              ? $config['asset_manager']
	              : array();
	    $config = isset($config['routes']) && (is_array($config['routes']) || $config['routes'] instanceof ArrayAccess)
	              ? $config['routes']
	              : array();
	    
	    // There can be special routes defined:
	    //    /css
	    //    /js
	    //    ...
	    // This way the asset manager can be hidden
	    if (!empty($config)) {
	    	// Rename all routes so I get an easy base route 
	    	// name called 'asset_manager'
		    $keys   = array_keys($config);
		    array_walk($keys, function($n) {
		    	$n = 'asset_manager/' . $n;
		    });
		    $config = array_combine($keys, array_values($config));
		    
		    // Add the routes
    		$router->addRoutes($config);
	    } else {
	    	// No routes have been configured!
	    	// So I need to create my own
	    	$router->addRoute(
    			'asset_manager',
	    		array(
	    			'type' => 'Zend\Mvc\Router\Http\Literal',
	                'options' => array(
	                    'route'    => '/assets',
	                ),
	                'may_terminate' => true,
	                'child_routes' => array(
	                	'default' => array(
	                		'type' => 'Wildcard'
	                	)
	                )
	    		)
	    	);
	    }
    }
1313641
public function attach(EventManagerInterface $events)
    {
        $events->attach(
            MvcEvent::EVENT_ROUTE,
            function (MvcEvent $event) {
                /** @var ControllerManager $controllerManager */
                $controllerManager = $event->getTarget()->getServiceManager()->get('ControllerManager');
                $routeMatch = $event->getRouteMatch();
                if (isset($routeMatch)) {
                    $controller = $controllerManager->get($routeMatch->getParam('controller'));
                    if ($controller instanceof AuthenticatedControllerInterface) {
                        $this->authenticate($event);
                    }
                }
            },
            $this->priority
        );
    }
664223
public function onDispatchError(MvcEvent $event)
    {
        // Do nothing if the result is a response object
        $result     = $event->getResult();
        $routeMatch = $event->getRouteMatch();
        $response   = $event->getResponse();
        $router     = $event->getRouter();
        $error      = $event->getError();
        $url        = $this->redirectUri;

        if ($result instanceof Response
            || ! $routeMatch
            || ($response && ! $response instanceof Response)
            || ! (
                Route::ERROR === $error
                || Controller::ERROR === $error
                || (
                    Application::ERROR_EXCEPTION === $error
                    && ($event->getParam('exception') instanceof UnAuthorizedException)
                )
            )
        ) {
            return;
        }

        if (null === $url) {
            $url = $router->assemble(array(), array('name' => $this->redirectRoute));
        }

        $response = $response ?: new Response();

        $response->getHeaders()->addHeaderLine('Location', $url);
        $response->setStatusCode(302);

        $event->setResponse($response);
    }
394213
public function init(MvcEvent $e)
    {
        $request = $e->getRequest();
        if (!$request instanceof Request) {
            return;
        }

        $sem = $e->getApplication()->getEventManager()->getSharedManager();
        $sem->attach('UploadableManager', 'createFile', [$this, 'onCreateFile']);
    }
821197
public function onPostRoute(MvcEvent $e)
    {
        $matches = $e->getRouteMatch();
        if (!$matches instanceof Router\RouteMatch) {
            // Can't do anything without a route match
            return;
        }
        
    	// Check for 'asset_manager' route
    	$routeName = $matches->getMatchedRouteName();
    	$routeName = explode('/', $routeName, 2);
    	$routeName = $routeName[0];
    	
    	if ($routeName !== 'asset_manager') {
    		// Not an asset request
    		return;
    	}
    	
    	// TODO: Take care of grouped assets (Merged assets)
    	//       Right now only direct access
    	
    	$serviceManager = $e->getApplication()->getServiceManager();
    	$router         = $e->getRouter();
    	$assetManager   = $serviceManager->get('AssetManager');
    	
    	$requestPath     = $e->getRequest()->getUri()->getPath();
    	$requestBasePath = $router->assemble(array(), array('name' => 'asset_manager'));
    	$requestBasePath = $requestBasePath . '/';
   
    	if (strlen($requestPath) <= strlen($requestBasePath)) {
    		// Not enough data!
    		return;
    	}
    	
    	$requestPath = substr($requestPath, strlen($requestBasePath) - 1);
    	
    	$assetPathStack = $serviceManager->get('AssetPathStack');
    	$content = $assetPathStack->resolve($requestPath);  	
    	if (!empty($content)) {
    		$response = $e->getResponse();
    		$headers  = $response->getHeaders();
    		$headers->addHeaderLine('Content-Type', MimeResolver::getMimeType($requestPath));
    		$response->setContent($content);
    		return $response;
    	}
    	
    }
890534
public function save(MvcEvent $mvcEvent)
    {
        if (!$this->shouldCacheRequest($mvcEvent)) {
            return;
        }

        $id = $this->getIdGenerator()->generate();

        $item = ($this->getOptions()->getCacheResponse() === true) ? serialize($mvcEvent->getResponse()) : $mvcEvent->getResponse()->getContent();

        $this->getCacheStorage()->setItem($id, $item);

        $cacheEvent = $this->createCacheEvent(CacheEvent::EVENT_SAVE, $mvcEvent, $id);
        $this->getEventManager()->triggerEvent($cacheEvent);

        $cacheStorage = $this->getCacheStorage();
        if ($cacheStorage instanceof TaggableInterface) {
            $tags = array_unique(array_merge($this->getTags($mvcEvent), $cacheEvent->getTags()));
            $cacheStorage->setTags($id, $this->prefixTags($tags));
        }
    }
5278
public function onBeforeRouting(Event $event): void
    {
        /** @var WebApplication $app */
        $app = $event['app'];

        if ($app->get('system.offline', false) && !$app->get('system.debug')) {
            $app->server->setHandler(
                function (RequestInterface $request, ResponseInterface $response, callable $next = null) {
                    $view = new HtmlView();
                    $view->setLayout('windwalker.offline.offline');

                    return new HtmlResponse($view->render());
                }
            );

            $app->server->listen();

            die;
        }
    }
1079783
public function getAdminPanel(MvcEvent $event)
    {
        $matchRoute = $event->getRouteMatch();

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

        /** @var \RcmAdmin\Controller\AdminPanelController $adminPanelController */
        $adminPanelController = $this->serviceLocator->get(
            \RcmAdmin\Controller\AdminPanelController::class
        );

        $adminPanelController->setEvent($event);

        $adminWrapper = $adminPanelController->getAdminWrapperAction();

        if (!$adminWrapper instanceof ViewModel) {
            return;
        }

        /** @var \Zend\View\Model\ViewModel $viewModel */
        $layout = $event->getViewModel();
        $layout->addChild($adminWrapper, 'rcmAdminPanel');
    }
269438
public function onRouteBuildAlter(RouteBuildEvent $event) {
    $collection = $event->getRouteCollection();
    array_walk(static::$disabledRoutes, function ($disabledRouteName) use ($collection) {
      if ($route = $collection->get($disabledRouteName)) {
        $route->setRequirement('_access', 'FALSE');
      }
    });
  }
641089
public function onRoute(MvcEvent $e)
    {
        if (!$e->getRequest() instanceof HttpRequest || !$e->getResponse() instanceof HttpResponse) {
            return null;
        }

        /** @var HttpResponse $response */
        $response = $e->getResponse();

        $data = $this->getCacheService()->load($e);

        if ($data !== null) {
            $this->loadedFromCache = true;

            if ($this->getOptions()->getCacheResponse() === true) {
                $response = unserialize($data);
            } else {
                $response = $e->getResponse();
                $response->setContent($data);
            }

            $this->addDebugResponseHeader($response, 'Hit');

            return $response;
        }

        $this->addDebugResponseHeader($response, 'Miss');
    }
277452
public function injectToolbarHtml(MvcEvent $event)
    {
        if (! $inspection = $event->getParam(ApplicationInspectorListener::PARAM_INSPECTION)) {
            return;
        }

        $response = $event->getResponse();

        if (! $response instanceof Response) {
            return;
        }

        $headers     = $response->getHeaders();
        $contentType = $headers->get('Content-Type');

        if ($contentType) {
            if (!$contentType instanceof ContentType) {
                return;
            }

            if (false === strpos(strtolower($contentType->getFieldValue()), 'html')) {
                return;
            }
        }

        $response->setContent(preg_replace(
            '/<\/body>/i',
            $this->renderer->render($this->inspectionRenderer->render($inspection)) . '</body>',
            $response->getContent(),
            1
        ));
    }
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;
        */
    }
10223
public function beforeRender(Event $event): void
    {
        $this->setJson('isAjax', $this->_controller->request->is('ajax'));
        $this->setJson('isMobile', $this->_controller->request->is('mobile'));
        $this->setBoth('isDialog', $this->_controller->request->is('dialog'));
        $this->setBoth('isJson', $this->_controller->request->is('json'));
        $this->setJson('debug', Configure::read('debug'));

        $ssl = false;
        if (env('HTTPS') || $this->_controller->request->is('ssl') || $this->_controller->request->env('HTTP_X_FORWARDED_PROTO') == 'https') {
            $ssl = true;
        }

        $appData = array(
            'jsonData' => $this->_jsonData,
            'webroot' => 'http' . ($ssl ? 's' : '') . '://' . env('HTTP_HOST') . $this->_controller->request->webroot,
            'url' => $this->_controller->request->url,
            // 'controller' => $this->_controller->name,
            // 'action' => $this->_controller->request->action,
            // 'plugin' => $this->_controller->request->plugin,
            'request' => array(
                'query' => $this->_controller->request->query,
                'pass' => $this->_controller->request->params['pass'],
                'plugin' => $this->_controller->request->plugin,
                'controller' => Inflector::underscore($this->_controller->name),
                'action' => $this->_controller->request->action
            )
        );

        // merge in the additional frontend data
        $appData = Hash::merge($appData, $this->_additionalAppData);
        $this->_controller->set('frontendData', $appData);
        $this->_controller->set('closeDialog', $this->_closeDialog);
    }
178989
public function onKernelController(ControllerEvent $event): void
    {
        if (!$this->check($event)) {
            return;
        }

        if ($annotation = $this->getAnnotation($event)) {
            $annotation->setData($this->getAttributes($event));
            /* @var $annotation Cache */
            $response = $this->getCache($annotation->getKey($event->getRequest()->get(self::ROUTE)));
            if (null !== $response) {
                $event->setController(function () use (
                    $response
                ) {
                    return $response;
                });
            }
        }
    }
215453
public function handleXmlHttpRequest(MvcEvent $event)
	{
		$request = $event->getRequest();
		if ($request->isXMLHttpRequest()) {
			$dispatchResult = $event->getResult();
			if ($dispatchResult instanceof ViewModel) {
				$dispatchResult->setTerminal(true);
			}
		}
	}
1200827
public function onRoute(MvcEvent $event)
    {
        $authService = $this->authService;

        $match = $event->getRouteMatch();
        if (!$authService->isAllowed(new PermResource(['route_match' => $match]), null))
            $authService->riseException();
    }
839407
public function finishEvent(MvcEvent $event)
    {
        $matchRoute = $event->getRouteMatch();

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

        /** @var \Rcm\EventListener\EventFinishListener $eventFinishListener */
        $eventFinishListener = $this->serviceLocator->get(
            \Rcm\EventListener\EventFinishListener::class
        );

        $return = $eventFinishListener->processRcmResponses($event);

        if (!empty($return)) {
            return $return;
        }

        return null;
    }