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

Commit c6b02a9

Browse files
committed
Merge branch 'feature/event-manager-v3' into develop
Close #31
2 parents 6775182 + bdacaee commit c6b02a9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+645
-447
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/doc export-ignore
12
/test export-ignore
23
/vendor export-ignore
34
.coveralls.yml export-ignore

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.*.sw*
77
.*.un~
88
nbproject
9+
doc/html/
910
tmp/
1011

1112
clover.xml

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.7.0 - TBD
5+
## 3.0.0 - TBD
66

77
### Added
88

9-
- Nothing.
9+
- [#31](https://github.com/zendframework/zend-mvc/pull/31) adds three required
10+
arguments to the `Zend\Mvc\Application` constructor: an EventManager
11+
instance, a Request instance, and a Response instance.
1012

1113
### Deprecated
1214

@@ -18,6 +20,9 @@ All notable changes to this project will be documented in this file, in reverse
1820

1921
### Fixed
2022

23+
- [#31](https://github.com/zendframework/zend-mvc/pull/31) updates the component
24+
to use zend-eventmanager v3.
25+
2126
## 2.6.1 - TBD
2227

2328
### Added

composer.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
},
1515
"require": {
1616
"php": ">=5.5",
17-
"zendframework/zend-eventmanager": "~2.5",
17+
"zendframework/zend-eventmanager": "dev-develop as 2.7.0",
1818
"zendframework/zend-servicemanager": "~2.5",
1919
"zendframework/zend-hydrator": "~1.0",
2020
"zendframework/zend-form": "~2.6",
21-
"zendframework/zend-stdlib": "~2.7"
21+
"zendframework/zend-stdlib": "~2.7",
22+
"container-interop/container-interop": "^1.1"
2223
},
2324
"require-dev": {
2425
"zendframework/zend-authentication": "~2.5",
@@ -31,14 +32,14 @@
3132
"zendframework/zend-inputfilter": "~2.5",
3233
"zendframework/zend-json": "~2.5",
3334
"zendframework/zend-log": "~2.5",
34-
"zendframework/zend-modulemanager": "~2.6",
35+
"zendframework/zend-modulemanager": "dev-develop as 2.7.0",
3536
"zendframework/zend-session": "~2.5",
3637
"zendframework/zend-serializer": "~2.5",
3738
"zendframework/zend-text": "~2.5",
3839
"zendframework/zend-uri": "~2.5",
3940
"zendframework/zend-validator": "~2.5",
4041
"zendframework/zend-version": "~2.5",
41-
"zendframework/zend-view": "~2.5",
42+
"zendframework/zend-view": "dev-develop as 2.6.0",
4243
"fabpot/php-cs-fixer": "1.7.*",
4344
"phpunit/PHPUnit": "~4.0"
4445
},
@@ -67,7 +68,7 @@
6768
"extra": {
6869
"branch-alias": {
6970
"dev-master": "2.6-dev",
70-
"dev-develop": "2.7-dev"
71+
"dev-develop": "3.0-dev"
7172
}
7273
},
7374
"autoload-dev": {

doc/book/migration.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.

doc/bookdown.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"title": "zend-mvc: MVC application provider",
3+
"content": [
4+
{"Intro": "../README.md"},
5+
{"Migration Guide": "book/migration.md"}
6+
],
7+
"target": "./html"
8+
}

src/Application.php

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Zend\EventManager\EventManagerAwareInterface;
1313
use Zend\EventManager\EventManagerInterface;
1414
use Zend\ServiceManager\ServiceManager;
15+
use Zend\Stdlib\RequestInterface;
1516
use Zend\Stdlib\ResponseInterface;
1617

1718
/**
@@ -103,15 +104,18 @@ class Application implements
103104
* @param mixed $configuration
104105
* @param ServiceManager $serviceManager
105106
*/
106-
public function __construct($configuration, ServiceManager $serviceManager)
107-
{
107+
public function __construct(
108+
$configuration,
109+
ServiceManager $serviceManager,
110+
EventManagerInterface $events,
111+
RequestInterface $request,
112+
ResponseInterface $response
113+
) {
108114
$this->configuration = $configuration;
109115
$this->serviceManager = $serviceManager;
110-
111-
$this->setEventManager($serviceManager->get('EventManager'));
112-
113-
$this->request = $serviceManager->get('Request');
114-
$this->response = $serviceManager->get('Response');
116+
$this->setEventManager($events);
117+
$this->request = $request;
118+
$this->response = $response;
115119
}
116120

117121
/**
@@ -142,19 +146,20 @@ public function bootstrap(array $listeners = [])
142146
$listeners = array_unique(array_merge($this->defaultListeners, $listeners));
143147

144148
foreach ($listeners as $listener) {
145-
$events->attach($serviceManager->get($listener));
149+
$serviceManager->get($listener)->attach($events);
146150
}
147151

148152
// Setup MVC Event
149153
$this->event = $event = new MvcEvent();
154+
$event->setName(MvcEvent::EVENT_BOOTSTRAP);
150155
$event->setTarget($this);
151-
$event->setApplication($this)
152-
->setRequest($this->request)
153-
->setResponse($this->response)
154-
->setRouter($serviceManager->get('Router'));
156+
$event->setApplication($this);
157+
$event->setRequest($this->request);
158+
$event->setResponse($this->response);
159+
$event->setRouter($serviceManager->get('Router'));
155160

156161
// Trigger bootstrap events
157-
$events->trigger(MvcEvent::EVENT_BOOTSTRAP, $event);
162+
$events->triggerEvent($event);
158163
return $this;
159164
}
160165

@@ -294,13 +299,15 @@ public function run()
294299
};
295300

296301
// Trigger route event
297-
$result = $events->trigger(MvcEvent::EVENT_ROUTE, $event, $shortCircuit);
302+
$event->setName(MvcEvent::EVENT_ROUTE);
303+
$result = $events->triggerEventUntil($shortCircuit, $event);
298304
if ($result->stopped()) {
299305
$response = $result->last();
300306
if ($response instanceof ResponseInterface) {
307+
$event->setName(MvcEvent::EVENT_FINISH);
301308
$event->setTarget($this);
302309
$event->setResponse($response);
303-
$events->trigger(MvcEvent::EVENT_FINISH, $event);
310+
$events->triggerEvent($event);
304311
$this->response = $response;
305312
return $this;
306313
}
@@ -311,14 +318,16 @@ public function run()
311318
}
312319

313320
// Trigger dispatch event
314-
$result = $events->trigger(MvcEvent::EVENT_DISPATCH, $event, $shortCircuit);
321+
$event->setName(MvcEvent::EVENT_DISPATCH);
322+
$result = $events->triggerEventUntil($shortCircuit, $event);
315323

316324
// Complete response
317325
$response = $result->last();
318326
if ($response instanceof ResponseInterface) {
327+
$event->setName(MvcEvent::EVENT_FINISH);
319328
$event->setTarget($this);
320329
$event->setResponse($response);
321-
$events->trigger(MvcEvent::EVENT_FINISH, $event);
330+
$events->triggerEvent($event);
322331
$this->response = $response;
323332
return $this;
324333
}
@@ -350,8 +359,13 @@ protected function completeRequest(MvcEvent $event)
350359
{
351360
$events = $this->events;
352361
$event->setTarget($this);
353-
$events->trigger(MvcEvent::EVENT_RENDER, $event);
354-
$events->trigger(MvcEvent::EVENT_FINISH, $event);
362+
363+
$event->setName(MvcEvent::EVENT_RENDER);
364+
$events->triggerEvent($event);
365+
366+
$event->setName(MvcEvent::EVENT_FINISH);
367+
$events->triggerEvent($event);
368+
355369
return $this;
356370
}
357371
}

src/Controller/AbstractController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,14 @@ public function dispatch(Request $request, Response $response = null)
109109
$this->response = $response;
110110

111111
$e = $this->getEvent();
112-
$e->setRequest($request)
113-
->setResponse($response)
114-
->setTarget($this);
112+
$e->setName(MvcEvent::EVENT_DISPATCH);
113+
$e->setRequest($request);
114+
$e->setResponse($response);
115+
$e->setTarget($this);
115116

116-
$result = $this->getEventManager()->trigger(MvcEvent::EVENT_DISPATCH, $e, function ($test) {
117+
$result = $this->getEventManager()->triggerEventUntil(function ($test) {
117118
return ($test instanceof Response);
118-
});
119+
}, $e);
119120

120121
if ($result->stopped()) {
121122
return $result->last();

src/Controller/ControllerManager.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
namespace Zend\Mvc\Controller;
1111

1212
use Zend\EventManager\EventManagerAwareInterface;
13-
use Zend\EventManager\EventManagerInterface;
13+
use Zend\EventManager\SharedEventManagerInterface;
1414
use Zend\Mvc\Exception;
1515
use Zend\ServiceManager\AbstractPluginManager;
1616
use Zend\ServiceManager\ConfigInterface;
@@ -73,10 +73,8 @@ public function injectControllerDependencies($controller, ServiceLocatorInterfac
7373
// is why the shared EM injection needs to happen; the conditional
7474
// will always pass.
7575
$events = $controller->getEventManager();
76-
if (!$events instanceof EventManagerInterface) {
76+
if (! $events || ! $events->getSharedManager() instanceof SharedEventManagerInterface) {
7777
$controller->setEventManager($parentLocator->get('EventManager'));
78-
} else {
79-
$events->setSharedManager($parentLocator->get('SharedEventManager'));
8078
}
8179
}
8280

src/Controller/Plugin/Forward.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,9 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
177177
$results[$id] = [];
178178
foreach ($eventArray as $eventName => $classArray) {
179179
$results[$id][$eventName] = [];
180-
$events = $sharedEvents->getListeners($id, $eventName);
180+
$events = $sharedEvents->getListeners([$id], $eventName);
181181
foreach ($events as $currentEvent) {
182-
$currentCallback = $currentEvent->getCallback();
182+
$currentCallback = $currentEvent;
183183

184184
// If we have an array, grab the object
185185
if (is_array($currentCallback)) {
@@ -193,7 +193,7 @@ protected function detachProblemListeners(SharedEvents $sharedEvents)
193193

194194
foreach ($classArray as $class) {
195195
if ($currentCallback instanceof $class) {
196-
$sharedEvents->detach($id, $currentEvent);
196+
$sharedEvents->detach($currentEvent, $id);
197197
$results[$id][$eventName][] = $currentEvent;
198198
}
199199
}

0 commit comments

Comments
 (0)