|
| 1 | +# Migration Guide |
| 2 | + |
| 3 | +This is a guide for migration from version 2 to version 3 of zend-mvc. |
| 4 | + |
| 5 | +## Application |
| 6 | + |
| 7 | +The constructor signature of `Zend\Mvc\Application` has changed. Previously, it |
| 8 | +was: |
| 9 | + |
| 10 | +```php |
| 11 | +__construct($configuration, ServiceManager $serviceManager) |
| 12 | +``` |
| 13 | + |
| 14 | +and internally, it pulled the services `EventManager`, `Request`, and `Response` |
| 15 | +from the provided `$serviceManager` during initialization. |
| 16 | + |
| 17 | +The new constructor signature is: |
| 18 | + |
| 19 | +```php |
| 20 | +__construct( |
| 21 | + $configuration, |
| 22 | + ServiceManager $serviceManager, |
| 23 | + EventManager $events, |
| 24 | + RequestInterface $request, |
| 25 | + ResponseInterface $response |
| 26 | +) |
| 27 | +``` |
| 28 | + |
| 29 | +making all dependencies explicit. The factory |
| 30 | +`Zend\Mvc\Service\ApplicationFactory` was updated to follow the new signature. |
| 31 | + |
| 32 | +This change should only affect users who are manually instantiating the |
| 33 | +`Application` instance. |
| 34 | + |
| 35 | +## EventManager initializer and ControllerManager event manager injection |
| 36 | + |
| 37 | +zend-mvc provides two mechanisms for injecting event managers into |
| 38 | +`EventManagerAware` objects. One is the "EventManagerAwareInitializer" |
| 39 | +registered in `Zend\Mvc\Service\ServiceManagerConfig`, and the other is internal |
| 40 | +logic in `Zend\Mvc\Controller\ControllerManager`. In both cases, the logic was |
| 41 | +updated due to changes in the v3 version of zend-eventmanager. |
| 42 | + |
| 43 | +Previously each would check if the instance's `getEventManager()` method |
| 44 | +returned an event manager instance, and, if so, inject the shared event manager: |
| 45 | + |
| 46 | +```php |
| 47 | +$events = $instance->getEventManager(); |
| 48 | +if ($events instanceof EventManagerInterface) { |
| 49 | + $events->setSharedManager($container->get('SharedEventManager')); |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +In zend-eventmanager v3, event manager's are now injected with the shared |
| 54 | +manager at instantiation, and no setter exists for providing the shared manager. |
| 55 | +As such, the above logic changed to: |
| 56 | + |
| 57 | +```php |
| 58 | +$events = $instance->getEventManager(); |
| 59 | +if (! $events || ! $events->getSharedManager()) { |
| 60 | + $instance->setEventManager($container->get('EventManager')); |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +In other words, it re-injects with a new event manager instance if the instance |
| 65 | +pulled does not have a shared manager composed. |
| 66 | + |
| 67 | +This likely will not cause regressions in existing code, but may be something to |
| 68 | +be aware of if you were previously depending on lazy-loaded event manager |
| 69 | +state. |
0 commit comments