Skip to content

Commit

Permalink
Merge pull request zendframework#2025 from weierophinney/hotfix/zf2-430
Browse files Browse the repository at this point in the history
[ZF2-430] Fix missing method
  • Loading branch information
EvanDotPro committed Jul 27, 2012
2 parents 5b3b6cf + be275e4 commit a3bb37d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 190 deletions.
14 changes: 0 additions & 14 deletions library/Zend/Mvc/View/Console/RouteNotFoundStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ class RouteNotFoundStrategy implements ListenerAggregateInterface
*/
protected $listeners = array();

/**
* Whether or not to display exceptions related to the 404 condition
*
* @var bool
*/
protected $displayExceptions = false;

/**
* Whether or not to display the reason for a 404
*
* @var bool
*/
protected $displayNotFoundReason = false;

/**
* The reason for a not-found condition
*
Expand Down
204 changes: 28 additions & 176 deletions library/Zend/Mvc/View/Console/ViewManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,121 +10,34 @@

namespace Zend\Mvc\View\Console;

use ArrayAccess;
use Traversable;
use Zend\EventManager\EventManagerInterface;
use Zend\EventManager\ListenerAggregateInterface;
use Zend\Mvc\ApplicationInterface;
use Zend\Mvc\Exception;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\View\Http\ViewManager as BaseViewManager;
use Zend\Mvc\View\SendResponseListener;
use Zend\ServiceManager\ConfigurationInterface;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use Zend\View\Helper as ViewHelper;
use Zend\View\HelperPluginManager as ViewHelperManager;
use Zend\View\Renderer\PhpRenderer as ViewPhpRenderer;
use Zend\View\Resolver as ViewResolver;
use Zend\View\Strategy\PhpRendererStrategy;
use Zend\View\View;

/**
* Prepares the view layer
*
* Instantiates and configures all classes related to the view layer, including
* the renderer (and its associated resolver(s) and helper manager), the view
* object (and its associated rendering strategies), and the various MVC
* strategies and listeners.
*
* Defines and manages the following services:
*
* - ViewHelperManager (also aliased to Zend\View\HelperPluginManager and ViewHelperBroker)
* - ViewTemplateMapResolver (also aliased to Zend\View\Resolver\TemplateMapResolver)
* - ViewTemplatePathStack (also aliased to Zend\View\Resolver\TemplatePathStack)
* - ViewResolver (also aliased to Zend\View\Resolver\AggregateResolver and ResolverInterface)
* - ViewRenderer (also aliased to Zend\View\Renderer\PhpRenderer and RendererInterface)
* - ViewPhpRendererStrategy (also aliased to Zend\View\Strategy\PhpRendererStrategy)
* - View (also aliased to Zend\View\View)
* - DefaultRenderingStrategy (also aliased to Zend\Mvc\View\DefaultRenderingStrategy)
* - ExceptionStrategy (also aliased to Zend\Mvc\View\ExceptionStrategy)
* - RouteNotFoundStrategy (also aliased to Zend\Mvc\View\RouteNotFoundStrategy and 404Strategy)
* - ViewModel
* Prepares the view layer for console applications
*
* @category Zend
* @package Zend_Mvc
* @subpackage View
*/
class ViewManager implements ListenerAggregateInterface
class ViewManager extends BaseViewManager
{
/**
* @var \Zend\Stdlib\CallbackHandler[]
*/
protected $listeners = array();

/**
* @var object application configuration service
*/
protected $config;

/**
* @var \Zend\ServiceManager\ServiceManager
*/
protected $services;

/**@+
* Various properties representing strategies and objects instantiated and
* configured by the view manager
*/
protected $exceptionStrategy;
protected $helperManager;
protected $mvcRenderingStrategy;
protected $renderer;
protected $rendererStrategy;
protected $resolver;
protected $routeNotFoundStrategy;
protected $event;
protected $view;
protected $viewModel;
/**@-*/

/**
* Attach the aggregate to the specified event manager
*
* @param EventManagerInterface $events
* @return void
*/
public function attach(EventManagerInterface $events)
{
$this->listeners[] = $events->attach(MvcEvent::EVENT_BOOTSTRAP, array($this, 'onBootstrap'), 10000);
}

/**
* Detach aggregate listeners from the specified event manager
*
* @param EventManagerInterface $events
* @return void
*/
public function detach(EventManagerInterface $events)
{
foreach ($this->listeners as $index => $listener) {
if ($events->detach($listener)) {
unset($this->listeners[$index]);
}
}
}

/**
* Prepares the view layer
*
* Overriding, as several operations are omitted in the console view
* algorithms, as well as to ensure we pick up the Console variants
* of several listeners and strategies.
*
* @param $event
* @return void
*/
public function onBootstrap($event)
{
$application = $event->getApplication();
$services = $application->getServiceManager();
$config = $services->get('Configuration');
$config = $services->get('Config');
$events = $application->getEventManager();
$sharedEvents = $events->getSharedManager();

Expand All @@ -134,11 +47,10 @@ public function onBootstrap($event)
$this->services = $services;
$this->event = $event;

$routeNotFoundStrategy = new RouteNotFoundStrategy();
$routeNotFoundStrategy = $this->getRouteNotFoundStrategy();
$exceptionStrategy = $this->getExceptionStrategy();
$mvcRenderingStrategy = $this->getMvcRenderingStrategy();
$createViewModelListener = new CreateViewModelListener();
// $injectTemplateListener = new InjectTemplateListener();
$injectViewModelListener = new InjectViewModelListener();
$sendResponseListener = new SendResponseListener();

Expand All @@ -154,13 +66,15 @@ public function onBootstrap($event)
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, array($createViewModelListener, 'createViewModelFromArray'), -80);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, array($createViewModelListener, 'createViewModelFromString'), -80);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, array($createViewModelListener, 'createViewModelFromNull'), -80);
// $sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, array($routeNotFoundStrategy, 'prepareNotFoundViewModel'), -90);
$sharedEvents->attach('Zend\Stdlib\DispatchableInterface', MvcEvent::EVENT_DISPATCH, array($injectViewModelListener, 'injectViewModel'), -100);
}

/**
* Instantiates and configures the default MVC rendering strategy
*
* Overriding to ensure we pick up the MVC rendering strategy for console,
* as well as to ensure that the appropriate aliases are set.
*
* @return DefaultRenderingStrategy
*/
public function getMvcRenderingStrategy()
Expand All @@ -181,6 +95,9 @@ public function getMvcRenderingStrategy()
/**
* Instantiates and configures the exception strategy
*
* Overriding to ensure we pick up the exception strategy for console, as
* well as to ensure that the appropriate aliases are set.
*
* @return ExceptionStrategy
*/
public function getExceptionStrategy()
Expand All @@ -206,91 +123,26 @@ public function getExceptionStrategy()
}

/**
* Configures the MvcEvent view model to ensure it has the template injected
*
* @return \Zend\View\Model\ModelInterface
*/
public function getViewModel()
{
if ($this->viewModel) {
return $this->viewModel;
}

$this->viewModel = $model = $this->event->getViewModel();
$model->setTemplate($this->getLayoutTemplate());

return $this->viewModel;
}

/**
* Register additional mvc rendering strategies
*
* If there is a "mvc_strategies" key of the view manager configuration, loop
* through it. Pull each as a service fromt the service manager, and, if it
* is a ListenerAggregate, attach it to the view, at priority 100. This
* latter allows each to trigger before the default mvc rendering strategy,
* and for them to trigger in the order they are registered.
*/
protected function registerMvcRenderingStrategies(EventManagerInterface $events)
{
if (!isset($this->config['mvc_strategies'])) {
return;
}
$mvcStrategies = $this->config['mvc_strategies'];
if (is_string($mvcStrategies)) {
$mvcStrategies = array($mvcStrategies);
}
if (!is_array($mvcStrategies) && !$mvcStrategies instanceof Traversable) {
return;
}

foreach ($mvcStrategies as $mvcStrategy) {
if (!is_string($mvcStrategy)) {
continue;
}

$listener = $this->services->get($mvcStrategy);
if ($listener instanceof ListenerAggregateInterface) {
$events->attach($listener, 100);
}
}
}

/**
* Register additional view strategies
* Instantiates and configures the "route not found", or 404, strategy
*
* If there is a "strategies" key of the view manager configuration, loop
* through it. Pull each as a service from the service manager, and, if it
* is a ListenerAggregate, attach it to the view, at priority 100. This
* latter allows each to trigger before the default strategy, and for them
* to trigger in the order they are registered.
* Overriding to ensure we pick up the route not found strategy for console,
* as well as to ensure that the appropriate aliases are set.
*
* @return void
* @return RouteNotFoundStrategy
*/
protected function registerViewStrategies()
public function getRouteNotFoundStrategy()
{
if (!isset($this->config['strategies'])) {
return;
}
$strategies = $this->config['strategies'];
if (is_string($strategies)) {
$strategies = array($strategies);
}
if (!is_array($strategies) && !$strategies instanceof Traversable) {
return;
if ($this->routeNotFoundStrategy) {
return $this->routeNotFoundStrategy;
}

$view = $this->getView();
$this->routeNotFoundStrategy = new RouteNotFoundStrategy();

foreach ($strategies as $strategy) {
if (!is_string($strategy)) {
continue;
}
$this->services->setService('RouteNotFoundStrategy', $this->routeNotFoundStrategy);
$this->services->setAlias('Zend\Mvc\View\RouteNotFoundStrategy', 'RouteNotFoundStrategy');
$this->services->setAlias('Zend\Mvc\View\Console\RouteNotFoundStrategy', 'RouteNotFoundStrategy');
$this->services->setAlias('404Strategy', 'RouteNotFoundStrategy');

$listener = $this->services->get($strategy);
if ($listener instanceof ListenerAggregateInterface) {
$view->getEventManager()->attach($listener, 100);
}
}
return $this->routeNotFoundStrategy;
}
}

0 comments on commit a3bb37d

Please sign in to comment.