public function handle(Request $request, callable $next)
    {
        /** @var DispatchResult $response */
        $response = $next($request);

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

        $templateName = $response->getDispatchedClassName();
        $subPath      = $response->getDispatchedMethod();
        $subPath      = Word::camelCaseToDash($subPath);
        $response     = $response->getResult();

        $data = [];
        if ( $response instanceof ViewModel ) {
            $data = $response->getContent();
        }

        if ( $data instanceof Response ) {
            return $data;
        }

        /** @var View $viewRenderer */
        $viewRenderer = $this->serviceManager->load('Arhitect\Contract\View');

        $templateName = explode('Controller\\', $templateName);
        array_shift($templateName);
        $templateName = implode('/', $templateName);
        $templateName = preg_replace('/Controller$/', '', $templateName);
        $templateName .= DIRECTORY_SEPARATOR;

        if ( $response instanceof HtmlView ) {
            if ( $response->getTemplateName() ) {
                $templateName = $response->getTemplateName();
                $subPath      = NULL;
            }
        }

        $content = $viewRenderer->render(
            strtolower($templateName) . strtolower($subPath) . '.twig',
            $data
        );

        return new Response($content);
    }
public function handle(Request $request, callable $next)
    {
        /** @var DispatchResult $response */
        $response = $next($request);

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

        $templateName = $response->getDispatchedClassName();
        $subPath      = $response->getDispatchedMethod();
        $subPath      = Word::camelCaseToDash($subPath);
        $response     = $response->getResult();

        $data = [];
        if ( $response instanceof ViewModel ) {
            $data = $response->getContent();
        }

        if ( $data instanceof Response ) {
            return $data;
        }

        /** @var View $viewRenderer */
        $viewRenderer = $this->serviceManager->load('Arhitect\Contract\View');

        $templateName = explode('Controller\\', $templateName);
        array_shift($templateName);
        $templateName = implode('/', $templateName);
        $templateName = preg_replace('/Controller$/', '', $templateName);
        $templateName .= DIRECTORY_SEPARATOR;

        if ( $response instanceof HtmlView ) {
            if ( $response->getTemplateName() ) {
                $templateName = $response->getTemplateName();
                $subPath      = NULL;
            }
        }

        $content = $viewRenderer->render(
            strtolower($templateName) . strtolower($subPath) . '.twig',
            $data
        );

        return new Response($content);
    }
@param Request  $request
@param callable $next

@return mixed