Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Commit

Permalink
Updated zend-view to zend-servicemanager v3
Browse files Browse the repository at this point in the history
Updates zend-view to use the new zend-servicemanager v3 API. Changes
include:

- Removed `ServiceLocatorAwareInterface` implementation from
  `FlashMessenger` view helper; was never used internally, and the
  interface no longer exists anyways.
- Navigation `AbstractHelper` now removes the
  `ServiceLocatorAwareInterface`, but keeps the methods originally
  defined by it, using duck-typing to do injection when necessary.
  Navigation view helpers require pulling navigation containers from the
  application container instance. This is automated by the Navigation
  `PluginManager` implementation now.
- All factories were updated to use the new `FactoryInterface`
  definition from zend-servicemanager:
  - New namespace for the `FactoryInterface`.
  - New method name for the factory (`__invoke`).
  - New arguments for the factory (first argument is a container-interop
    `ContainerInterface`; required second argument `$name` and optional
    third argument `$options`).
  - Factories that pulled the parent locator were updated, as the
    container passed is now the application service manager.
- Plugin managers were updated to the new `AbstractPluginManager`
  implementation:
  - Define `$instanceOf` and remove `validatePlugin` when applicable.
  - Define default configuration, merge it with incoming configuration,
    and pass to the parent constructor.
  - Define copious aliases to cover various naming patterns previously
    covered by normalization.
  - Initializers now have a new signature; all plugin-defined
    initializers were updated to it.
- When lazy loading plugin managers, pass an empty `ServiceManager`
  instance.
- All tests were updated to ensure plugin and service managers are
  properly injected and configured.
  • Loading branch information
weierophinney committed Oct 5, 2015
1 parent adade3d commit 8595427
Show file tree
Hide file tree
Showing 19 changed files with 319 additions and 298 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"zendframework/zend-paginator": "~2.5",
"zendframework/zend-permissions-acl": "~2.5",
"zendframework/zend-serializer": "~2.5",
"zendframework/zend-servicemanager": "~2.5",
"zendframework/zend-servicemanager": "dev-develop as 2.7.0",
"zendframework/zend-session": "dev-master",
"zendframework/zend-uri": "~2.5",
"fabpot/php-cs-fixer": "1.7.*",
Expand Down
33 changes: 1 addition & 32 deletions src/Helper/FlashMessenger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
namespace Zend\View\Helper;

use Zend\Mvc\Controller\Plugin\FlashMessenger as PluginFlashMessenger;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\I18n\View\Helper\AbstractTranslatorHelper;

/**
* Helper to proxy the plugin flash messenger
*/
class FlashMessenger extends AbstractTranslatorHelper implements ServiceLocatorAwareInterface
class FlashMessenger extends AbstractTranslatorHelper
{
/**
* Default attributes for the open format tag
Expand Down Expand Up @@ -62,13 +60,6 @@ class FlashMessenger extends AbstractTranslatorHelper implements ServiceLocatorA
*/
protected $pluginFlashMessenger;

/**
* Service locator
*
* @var ServiceLocatorInterface
*/
protected $serviceLocator;

/**
* Returns the flash messenger plugin controller
*
Expand Down Expand Up @@ -309,28 +300,6 @@ public function getPluginFlashMessenger()
return $this->pluginFlashMessenger;
}

/**
* Set the service locator.
*
* @param ServiceLocatorInterface $serviceLocator
* @return AbstractHelper
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
}

/**
* Get the service locator.
*
* @return ServiceLocatorInterface
*/
public function getServiceLocator()
{
return $this->serviceLocator;
}

/**
* Retrieve the escapeHtml helper
*
Expand Down
3 changes: 1 addition & 2 deletions src/Helper/Navigation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Zend\View\Helper;

use Zend\Navigation\AbstractContainer;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\View\Exception;
use Zend\View\Helper\Navigation\AbstractHelper as AbstractNavigationHelper;
use Zend\View\Helper\Navigation\HelperInterface as NavigationHelper;
Expand Down Expand Up @@ -112,7 +111,7 @@ public function __call($method, array $arguments = [])
// check if call should proxy to another helper
$helper = $this->findHelper($method, false);
if ($helper) {
if ($helper instanceof ServiceLocatorAwareInterface && $this->getServiceLocator()) {
if (method_exists($helper, 'setServiceLocator') && $this->getServiceLocator()) {
$helper->setServiceLocator($this->getServiceLocator());
}
return call_user_func_array($helper, $arguments);
Expand Down
37 changes: 16 additions & 21 deletions src/Helper/Navigation/AbstractHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Zend\View\Helper\Navigation;

use Interop\Container\ContainerInterface;
use RecursiveIteratorIterator;
use Zend\EventManager\EventManager;
use Zend\EventManager\EventManagerAwareInterface;
Expand All @@ -18,8 +19,6 @@
use Zend\Navigation;
use Zend\Navigation\Page\AbstractPage;
use Zend\Permissions\Acl;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\View;
use Zend\View\Exception;

Expand All @@ -29,19 +28,13 @@
abstract class AbstractHelper extends View\Helper\AbstractHtmlElement implements
EventManagerAwareInterface,
HelperInterface,
ServiceLocatorAwareInterface,
TranslatorAwareInterface
{
/**
* @var EventManagerInterface
*/
protected $events;

/**
* @var ServiceLocatorInterface
*/
protected $serviceLocator;

/**
* AbstractContainer to operate on by default
*
Expand Down Expand Up @@ -91,6 +84,11 @@ abstract class AbstractHelper extends View\Helper\AbstractHtmlElement implements
*/
protected $role;

/**
* @var ContainerInterface
*/
protected $serviceLocator;

/**
* Whether ACL should be used for filtering out pages
*
Expand Down Expand Up @@ -260,7 +258,8 @@ protected function parseContainer(&$container = null)
}

if (is_string($container)) {
if (!$this->getServiceLocator()) {
$services = $this->getServiceLocator();
if (! $services) {
throw new Exception\InvalidArgumentException(sprintf(
'Attempted to set container with alias "%s" but no ServiceLocator was set',
$container
Expand All @@ -269,16 +268,8 @@ protected function parseContainer(&$container = null)

/**
* Load the navigation container from the root service locator
*
* The navigation container is probably located in Zend\ServiceManager\ServiceManager
* and not in the View\HelperPluginManager. If the set service locator is a
* HelperPluginManager, access the navigation container via the main service locator.
*/
$sl = $this->getServiceLocator();
if ($sl instanceof View\HelperPluginManager) {
$sl = $sl->getServiceLocator();
}
$container = $sl->get($container);
$container = $services->get($container);
return;
}

Expand Down Expand Up @@ -755,10 +746,12 @@ public function hasRole()
/**
* Set the service locator.
*
* @param ServiceLocatorInterface $serviceLocator
* Used internally to pull named navigation containers to render.
*
* @param ContainerInterface $serviceLocator
* @return AbstractHelper
*/
public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
public function setServiceLocator(ContainerInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
return $this;
Expand All @@ -767,7 +760,9 @@ public function setServiceLocator(ServiceLocatorInterface $serviceLocator)
/**
* Get the service locator.
*
* @return ServiceLocatorInterface
* Used internally to pull named navigation containers to render.
*
* @return ContainerInterface
*/
public function getServiceLocator()
{
Expand Down
51 changes: 27 additions & 24 deletions src/Helper/Navigation/PluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace Zend\View\Helper\Navigation;

use Zend\View\Exception;
use Interop\Container\ContainerInterface;
use Zend\View\HelperPluginManager;

/**
Expand All @@ -22,37 +22,40 @@
class PluginManager extends HelperPluginManager
{
/**
* Default set of helpers
* @var string Valid instance types.
*/
protected $instanceOf = AbstractHelper::class;

/**
* Default configuration.
*
* @var array
*/
protected $invokableClasses = [
'breadcrumbs' => 'Zend\View\Helper\Navigation\Breadcrumbs',
'links' => 'Zend\View\Helper\Navigation\Links',
'menu' => 'Zend\View\Helper\Navigation\Menu',
'sitemap' => 'Zend\View\Helper\Navigation\Sitemap',
protected $config = [
'invokables' => [
'breadcrumbs' => Breadcrumbs::class,
'links' => Links::class,
'menu' => Menu::class,
'sitemap' => Sitemap::class,
],
];

/**
* Validate the plugin
*
* Checks that the helper loaded is an instance of AbstractHelper.
*
* @param mixed $plugin
* @return void
* @throws Exception\InvalidArgumentException if invalid
* @param ContainerInterface $container
* @param array $config
*/
public function validatePlugin($plugin)
public function __construct(ContainerInterface $container, array $config = [])
{
if ($plugin instanceof AbstractHelper) {
// we're okay
return;
}
$this->config['initializers'] = [
function ($container, $instance) {
if (! $instance instanceof AbstractHelper) {
continue;
}

$instance->setServiceLocator($container);
},
];

throw new Exception\InvalidArgumentException(sprintf(
'Plugin of type %s is invalid; must implement %s\AbstractHelper',
(is_object($plugin) ? get_class($plugin) : gettype($plugin)),
__NAMESPACE__
));
parent::__construct($container, $config);
}
}
21 changes: 12 additions & 9 deletions src/Helper/Service/FlashMessengerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,29 @@

namespace Zend\View\Helper\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\View\Helper\FlashMessenger;

class FlashMessengerFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @param ContainerInterface $container
* @param string $name
* @param null|array $options
* @return FlashMessenger
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
$serviceLocator = $serviceLocator->getServiceLocator();
$helper = new FlashMessenger();
$controllerPluginManager = $serviceLocator->get('ControllerPluginManager');
$flashMessenger = $controllerPluginManager->get('flashmessenger');
$helper = new FlashMessenger();
$controllerPluginManager = $container->get('ControllerPluginManager');
$flashMessenger = $controllerPluginManager->get('flashmessenger');

$helper->setPluginFlashMessenger($flashMessenger);
$config = $serviceLocator->get('Config');

$config = $container->get('Config');
if (isset($config['view_helper_config']['flashmessenger'])) {
$configHelper = $config['view_helper_config']['flashmessenger'];
if (isset($configHelper['message_open_format'])) {
Expand Down
14 changes: 8 additions & 6 deletions src/Helper/Service/IdentityFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,25 @@

namespace Zend\View\Helper\Service;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;
use Zend\View\Helper\Identity;

class IdentityFactory implements FactoryInterface
{
/**
* {@inheritDoc}
*
* @param ContainerInterface $container
* @param string $name
* @param null|array $options
* @return \Zend\View\Helper\Identity
*/
public function createService(ServiceLocatorInterface $serviceLocator)
public function __invoke(ContainerInterface $container, $name, array $options = null)
{
$services = $serviceLocator->getServiceLocator();
$helper = new Identity();
if ($services->has('Zend\Authentication\AuthenticationService')) {
$helper->setAuthenticationService($services->get('Zend\Authentication\AuthenticationService'));
if ($container->has('Zend\Authentication\AuthenticationService')) {
$helper->setAuthenticationService($container->get('Zend\Authentication\AuthenticationService'));
}
return $helper;
}
Expand Down
Loading

0 comments on commit 8595427

Please sign in to comment.