From 82e1583b730b78b0d14e76956ef3c99f65eaf12e Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Sun, 12 Feb 2012 19:26:59 -0600 Subject: [PATCH] Working view layer integration - Updated ZF submodule to c85c45a4 of weierophinney/feature/view-layer - Updated configuration to utilize new view layer - Updated controllers to return View Models - Updated Application\Module to only configure necessary view helpers - Removed Application\View\Listener; no longer necessary --- module/Application/Module.php | 44 +---- module/Application/config/module.config.php | 46 ++++- .../src/Application/Controller/ErrorController.php | 5 +- .../src/Application/Controller/IndexController.php | 5 +- .../Application/src/Application/View/Listener.php | 187 -------------------- vendor/ZendFramework | 2 +- 6 files changed, 51 insertions(+), 238 deletions(-) delete mode 100644 module/Application/src/Application/View/Listener.php diff --git a/module/Application/Module.php b/module/Application/Module.php index 2b220cd..75282d9 100644 --- a/module/Application/Module.php +++ b/module/Application/Module.php @@ -39,45 +39,11 @@ class Module implements AutoloaderProvider public function initializeView($e) { $app = $e->getParam('application'); + $basePath = $app->getRequest()->getBasePath(); $locator = $app->getLocator(); - $config = $e->getParam('config'); - $view = $this->getView($app); - $viewListener = $this->getViewListener($view, $config); - $app->events()->attachAggregate($viewListener); - $events = StaticEventManager::getInstance(); - $viewListener->registerStaticListeners($events, $locator); - } - - protected function getViewListener($view, $config) - { - if ($this->viewListener instanceof View\Listener) { - return $this->viewListener; - } - - $viewListener = new View\Listener($view, $config->layout); - $viewListener->setDisplayExceptionsFlag($config->display_exceptions); - - $this->viewListener = $viewListener; - return $viewListener; - } - - protected function getView($app) - { - if ($this->view) { - return $this->view; - } - - $locator = $app->getLocator(); - $view = $locator->get('view'); - - // Set up view helpers - $view->plugin('url')->setRouter($app->getRouter()); - $view->doctype()->setDoctype('HTML5'); - - $basePath = $app->getRequest()->getBasePath(); - $view->plugin('basePath')->setBasePath($basePath); - - $this->view = $view; - return $view; + $renderer = $locator->get('Zend\View\Renderer\PhpRenderer'); + $renderer->plugin('url')->setRouter($app->getRouter()); + $renderer->doctype()->setDoctype('HTML5'); + $renderer->plugin('basePath')->setBasePath($basePath); } } diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index cab10d7..6b06ceb 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -1,13 +1,23 @@ 'layout/layout.phtml', - 'display_exceptions' => true, - 'di' => array( + 'di' => array( + 'definition' => array( + 'class' => array( + 'Zend\View\Renderer\PhpRenderer' => array( + 'setResolver' => array( + 'resolver' => array( + 'required' => false, + 'type' => 'Zend\View\Resolver', + ), + ), + ), + ), + ), 'instance' => array( 'alias' => array( 'index' => 'Application\Controller\IndexController', 'error' => 'Application\Controller\ErrorController', - 'view' => 'Zend\View\PhpRenderer', + 'view' => 'Zend\View\Renderer\PhpRenderer', ), // Inject the plugin broker for controller plugins into @@ -24,10 +34,9 @@ return array( ), ), - // Setup the PhpRenderer - 'Zend\View\PhpRenderer' => array( + // Setup the View layer + 'Zend\View\Resolver\TemplatePathStack' => array( 'parameters' => array( - 'resolver' => 'Zend\View\TemplatePathStack', 'options' => array( 'script_paths' => array( 'application' => __DIR__ . '/../view', @@ -35,6 +44,29 @@ return array( ), ), ), + 'Zend\View\Renderer\PhpRenderer' => array( + 'parameters' => array( + 'resolver' => 'Zend\View\Resolver\TemplatePathStack', + ), + ), + 'Zend\Mvc\View\DefaultRenderingStrategy' => array( + 'parameters' => array( + 'baseTemplate' => 'layout/layout.phtml', + ), + ), + 'Zend\Mvc\View\ExceptionStrategy' => array( + 'parameters' => array( + 'displayExceptions' => true, + 'errorTemplate' => 'error/index.phtml', + ), + ), + 'Zend\Mvc\View\RouteNotFoundStrategy' => array( + 'parameters' => array( + 'notFoundTemplate' => 'error/404.phtml', + ), + ), + + // Setup the router and routes 'Zend\Mvc\Router\RouteStack' => array( 'parameters' => array( 'routes' => array( diff --git a/module/Application/src/Application/Controller/ErrorController.php b/module/Application/src/Application/Controller/ErrorController.php index 17f002e..d959f98 100644 --- a/module/Application/src/Application/Controller/ErrorController.php +++ b/module/Application/src/Application/Controller/ErrorController.php @@ -2,7 +2,8 @@ namespace Application\Controller; -use Zend\Mvc\Controller\ActionController; +use Zend\Mvc\Controller\ActionController, + Zend\View\Model\ViewModel; class ErrorController extends ActionController { @@ -28,6 +29,6 @@ class ErrorController extends ActionController break; } - return array('message' => $error['message']); + return new ViewModel(array('message' => $error['message'])); } } diff --git a/module/Application/src/Application/Controller/IndexController.php b/module/Application/src/Application/Controller/IndexController.php index 8c1f5e1..fa1103b 100644 --- a/module/Application/src/Application/Controller/IndexController.php +++ b/module/Application/src/Application/Controller/IndexController.php @@ -2,12 +2,13 @@ namespace Application\Controller; -use Zend\Mvc\Controller\ActionController; +use Zend\Mvc\Controller\ActionController, + Zend\View\Model\ViewModel; class IndexController extends ActionController { public function indexAction() { - return array(); + return new ViewModel(); } } diff --git a/module/Application/src/Application/View/Listener.php b/module/Application/src/Application/View/Listener.php deleted file mode 100644 index 100530e..0000000 --- a/module/Application/src/Application/View/Listener.php +++ /dev/null @@ -1,187 +0,0 @@ -view = $renderer; - $this->layout = $layout; - } - - public function setDisplayExceptionsFlag($flag) - { - $this->displayExceptions = (bool) $flag; - return $this; - } - - public function displayExceptions() - { - return $this->displayExceptions; - } - - public function attach(EventCollection $events) - { - $this->listeners[] = $events->attach('dispatch.error', array($this, 'renderError')); - $this->listeners[] = $events->attach('dispatch', array($this, 'render404'), -1000); - $this->listeners[] = $events->attach('dispatch', array($this, 'renderLayout'), -80); - } - - public function detach(EventCollection $events) - { - foreach ($this->listeners as $key => $listener) { - $events->detach($listener); - unset($this->listeners[$key]); - unset($listener); - } - } - - public function registerStaticListeners(StaticEventCollection $events, $locator) - { - $ident = 'Zend\Mvc\Controller\ActionController'; - $handler = $events->attach($ident, 'dispatch', array($this, 'renderView'), -50); - $this->staticListeners[] = array($ident, $handler); - } - - public function detachStaticListeners(StaticEventCollection $events) - { - foreach ($this->staticListeners as $i => $info) { - list($id, $handler) = $info; - $events->detach($id, $handler); - unset($this->staticListeners[$i]); - } - } - - public function renderView(MvcEvent $e) - { - $response = $e->getResponse(); - if (!$response->isSuccess()) { - return; - } - - $routeMatch = $e->getRouteMatch(); - $controller = $routeMatch->getParam('controller', 'index'); - $action = $routeMatch->getParam('action', 'index'); - $script = $controller . '/' . $action . '.phtml'; - - $vars = $e->getResult(); - if (is_scalar($vars)) { - $vars = array('content' => $vars); - } elseif (is_object($vars) && !$vars instanceof ArrayAccess) { - $vars = (array) $vars; - } - - $content = $this->view->render($script, $vars); - - $e->setParam('content', $content); - return $content; - } - - public function renderLayout(MvcEvent $e) - { - $response = $e->getResponse(); - if (!$response) { - $response = new Response(); - $e->setResponse($response); - } - if ($response->isRedirect()) { - return $response; - } - - $vars = $e->getResult(); - if (is_scalar($vars)) { - $vars = array('content' => $vars); - } elseif (is_object($vars) && !$vars instanceof ArrayAccess) { - $vars = (array) $vars; - } - - if (false !== ($contentParam = $e->getParam('content', false))) { - $vars['content'] = $contentParam; - } - - $layout = $this->view->render($this->layout, $vars); - $response->setContent($layout); - return $response; - } - - public function render404(MvcEvent $e) - { - $vars = $e->getResult(); - if ($vars instanceof Response) { - return; - } - - $response = $e->getResponse(); - if ($response->getStatusCode() != 404) { - // Only handle 404's - return; - } - - $vars = array( - 'message' => 'Page not found.', - 'exception' => $e->getParam('exception'), - 'display_exceptions' => $this->displayExceptions(), - ); - - $content = $this->view->render('error/404.phtml', $vars); - - $e->setResult($content); - - return $this->renderLayout($e); - } - - public function renderError(MvcEvent $e) - { - $error = $e->getError(); - $response = $e->getResponse(); - if (!$response) { - $response = new Response(); - $e->setResponse($response); - } - - switch ($error) { - case Application::ERROR_CONTROLLER_NOT_FOUND: - case Application::ERROR_CONTROLLER_INVALID: - $vars = array( - 'message' => 'Page not found.', - 'exception' => $e->getParam('exception'), - 'display_exceptions' => $this->displayExceptions(), - ); - $response->setStatusCode(404); - break; - - case Application::ERROR_EXCEPTION: - default: - $vars = array( - 'message' => 'An error occurred during execution; please try again later.', - 'exception' => $e->getParam('exception'), - 'display_exceptions' => $this->displayExceptions(), - ); - $response->setStatusCode(500); - break; - } - - $content = $this->view->render('error/index.phtml', $vars); - - $e->setResult($content); - - return $this->renderLayout($e); - } -} diff --git a/vendor/ZendFramework b/vendor/ZendFramework index 94b9f93..c85c45a 160000 --- a/vendor/ZendFramework +++ b/vendor/ZendFramework @@ -1 +1 @@ -Subproject commit 94b9f933582945527b4f59e77090ec55492f6c75 +Subproject commit c85c45a427afd03f5efb6c6fe31220ae97bb1175 -- 1.7.10.4