3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mvc\Service
;
12 use Interop\Container\ContainerInterface
;
14 use Zend\EventManager\EventManager
;
15 use Zend\ServiceManager\FactoryInterface
;
16 use Zend\ServiceManager\ServiceLocatorInterface
;
18 class EventManagerFactory
implements FactoryInterface
21 * Create an EventManager instance
23 * Creates a new EventManager instance, seeding it with a shared instance
24 * of SharedEventManager.
26 * @param ContainerInterface $container
28 * @param null|array $options
29 * @return EventManager
31 public function __invoke(ContainerInterface
$container, $name, array $options = null)
33 if ($this->acceptsSharedManagerToConstructor()) {
34 // zend-eventmanager v3
35 return new EventManager(
36 $container->has('SharedEventManager') ?
$container->get('SharedEventManager') : null
40 // zend-eventmanager v2
41 $events = new EventManager();
43 if ($container->has('SharedEventManager')) {
44 $events->setSharedManager($container->get('SharedEventManager'));
51 * Create and return EventManager instance
53 * For use with zend-servicemanager v2; proxies to __invoke().
55 * @param ServiceLocatorInterface $container
56 * @return EventManager
58 public function createService(ServiceLocatorInterface
$container)
60 return $this($container, EventManager
::class);
64 * Does the EventManager accept the shared manager to the constructor?
66 * In zend-eventmanager v3, the EventManager accepts the shared manager
67 * instance to the constructor *only*, while in v2, it must be injected
68 * via the setSharedManager() method.
72 private function acceptsSharedManagerToConstructor()
74 $r = new ReflectionClass(EventManager
::class);
75 return ! $r->hasMethod('setSharedManager');