/** * Create a console view model representing a "not found" action * * @return ConsoleModel */ public function __invoke() { $viewModel = new ConsoleModel(); $viewModel->setErrorLevel(1); $viewModel->setResult('Page not found'); return $viewModel; }
/** * Inspect the result, and cast it to a ViewModel if a string is detected * * @param MvcEvent $e * @return void */ public function createViewModelFromString(MvcEvent $e) { $result = $e->getResult(); if (!is_string($result)) { return; } // create Console model $model = new ConsoleModel(); // store the result in a model variable $model->setVariable(ConsoleModel::RESULT, $result); $e->setResult($model); }
/** * Create an exception view model, and set the HTTP status code * * @todo dispatch.error does not halt dispatch unless a response is * returned. As such, we likely need to trigger rendering as a low * priority dispatch.error event (or goto a render event) to ensure * rendering occurs, and that munging of view models occurs when * expected. * @param MvcEvent $e * @return void */ public function prepareExceptionViewModel(MvcEvent $e) { // Do nothing if no error in the event $error = $e->getError(); if (empty($error)) { return; } // Do nothing if the result is a response object $result = $e->getResult(); if ($result instanceof Response) { return; } switch ($error) { case Application::ERROR_CONTROLLER_NOT_FOUND: case Application::ERROR_CONTROLLER_INVALID: case Application::ERROR_ROUTER_NO_MATCH: // Specifically not handling these because they are handled by routeNotFound strategy return; case Application::ERROR_EXCEPTION: default: // Prepare error message $exception = $e->getParam('exception'); if (is_callable($this->message)) { $callback = $this->message; $message = (string) $callback($exception, $this->displayExceptions); } elseif ($this->displayExceptions && ($exception instanceof \Exception || $exception instanceof \Throwable)) { $previous = ''; $previousException = $exception->getPrevious(); while ($previousException) { $previous .= str_replace([':className', ':message', ':code', ':file', ':line', ':stack'], [get_class($previousException), $previousException->getMessage(), $previousException->getCode(), $previousException->getFile(), $previousException->getLine(), $exception->getTraceAsString()], $this->previousMessage); $previousException = $previousException->getPrevious(); } $message = str_replace([':className', ':message', ':code', ':file', ':line', ':stack', ':previous'], [get_class($exception), $exception->getMessage(), $exception->getCode(), $exception->getFile(), $exception->getLine(), $exception->getTraceAsString(), $previous], $this->message); } else { $message = str_replace([':className', ':message', ':code', ':file', ':line', ':stack', ':previous'], ['', '', '', '', '', '', ''], $this->message); } // Prepare view model $model = new ConsoleModel(); $model->setResult($message); $model->setErrorLevel(1); // Inject it into MvcEvent $e->setResult($model); break; } }
/** * Detect if an error is a route not found condition * * If a "controller not found" or "invalid controller" error type is * encountered, sets the response status code to 404. * * @param MvcEvent $e * @throws RuntimeException * @throws ServiceNotFoundException * @return void */ public function handleRouteNotFoundError(MvcEvent $e) { $error = $e->getError(); if (empty($error)) { return; } $response = $e->getResponse(); $request = $e->getRequest(); switch ($error) { case Application::ERROR_CONTROLLER_NOT_FOUND: case Application::ERROR_CONTROLLER_INVALID: case Application::ERROR_ROUTER_NO_MATCH: $this->reason = $error; if (!$response) { $response = new ConsoleResponse(); $e->setResponse($response); } $response->setMetadata('error', $error); break; default: return; } $result = $e->getResult(); if ($result instanceof Response) { // Already have a response as the result return; } // Prepare Console View Model $model = new ConsoleModel(); $model->setErrorLevel(1); // Fetch service manager $sm = $e->getApplication()->getServiceManager(); // Try to fetch module manager $mm = null; try { $mm = $sm->get('ModuleManager'); } catch (ServiceNotFoundException $exception) { // The application does not have or use module manager, so we cannot use it } // Try to fetch current console adapter try { $console = $sm->get('console'); if (!$console instanceof ConsoleAdapter) { throw new ServiceNotFoundException(); } } catch (ServiceNotFoundException $exception) { // The application does not have console adapter throw new RuntimeException('Cannot access Console adapter - is it defined in ServiceManager?'); } // Retrieve the script's name (entry point) $scriptName = ''; if ($request instanceof ConsoleRequest) { $scriptName = basename($request->getScriptName()); } // Get application banner $banner = $this->getConsoleBanner($console, $mm); // Get application usage information $usage = $this->getConsoleUsage($console, $scriptName, $mm); // Inject the text into view $result = $banner ? rtrim($banner, "\r\n") : ''; $result .= $usage ? "\n\n" . trim($usage, "\r\n") : ''; $result .= "\n"; // to ensure we output a final newline $result .= $this->reportNotFoundReason($e); $model->setResult($result); // Inject the result into MvcEvent $e->setResult($model); }