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

Commit 588f18a

Browse files
committed
Merge branch 'hotfix/138' into develop
Forward port #138
2 parents ff9d145 + 51c0f73 commit 588f18a

File tree

7 files changed

+68
-11
lines changed

7 files changed

+68
-11
lines changed

CHANGELOG.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,16 @@ for full details on how to migrate your v2 application.
150150
- [#128](https://github.com/zendframework/zend-mvc/pull/128) bumps the minimum
151151
supported PHP version to 5.6.
152152

153-
## 2.7.8 - TBD
153+
## 2.7.8 - 2016-05-31
154154

155155
### Added
156156

157-
- Nothing.
157+
- [#138](https://github.com/zendframework/zend-mvc/pull/138) adds support for
158+
PHP 7 `Throwable`s within each of:
159+
- `DispatchListener`
160+
- `MiddlewareListener`
161+
- The console `RouteNotFoundStrategy` and `ExceptionStrategy`
162+
- The HTTP `DefaultRenderingStrategy` and `RouteNotFoundStrategy`
158163

159164
### Deprecated
160165

src/DispatchListener.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ public function onDispatch(MvcEvent $e)
9999
} catch (InvalidServiceException $exception) {
100100
$return = $this->marshalControllerNotFoundEvent($application::ERROR_CONTROLLER_INVALID, $controllerName, $e, $application, $exception);
101101
return $this->complete($return, $e);
102-
} catch (\Exception $exception) {
102+
} catch (\Throwable $exception) {
103+
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
104+
return $this->complete($return, $e);
105+
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
103106
$return = $this->marshalBadControllerEvent($controllerName, $e, $application, $exception);
104107
return $this->complete($return, $e);
105108
}
@@ -110,15 +113,22 @@ public function onDispatch(MvcEvent $e)
110113

111114
$request = $e->getRequest();
112115
$response = $application->getResponse();
116+
$caughtException = null;
113117

114118
try {
115119
$return = $controller->dispatch($request, $response);
116-
} catch (\Exception $ex) {
120+
} catch (\Throwable $ex) {
121+
$caughtException = $ex;
122+
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
123+
$caughtException = $ex;
124+
}
125+
126+
if ($caughtException !== null) {
117127
$e->setName(MvcEvent::EVENT_DISPATCH_ERROR);
118128
$e->setError($application::ERROR_EXCEPTION);
119129
$e->setController($controllerName);
120130
$e->setControllerClass(get_class($controller));
121-
$e->setParam('exception', $ex);
131+
$e->setParam('exception', $caughtException);
122132

123133
$return = $application->getEventManager()->triggerEvent($e)->last();
124134
if (! $return) {
@@ -136,7 +146,7 @@ public function reportMonitorEvent(MvcEvent $e)
136146
{
137147
$error = $e->getError();
138148
$exception = $e->getParam('exception');
139-
if ($exception instanceof \Exception) {
149+
if ($exception instanceof \Exception || $exception instanceof \Throwable) { // @TODO clean up once PHP 7 requirement is enforced
140150
zend_monitor_custom_event_ex($error, $exception->getMessage(), 'Zend Framework Exception', ['code' => $exception->getCode(), 'trace' => $exception->getTraceAsString()]);
141151
}
142152
}

src/MiddlewareListener.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,17 @@ public function onDispatch(MvcEvent $event)
5656
$event->setResult($return);
5757
return $return;
5858
}
59+
60+
$caughtException = null;
5961
try {
6062
$return = $middleware(Psr7Request::fromZend($request), Psr7Response::fromZend($response));
61-
} catch (\Exception $exception) {
63+
} catch (\Throwable $exception) {
64+
$caughtException = $exception;
65+
} catch (\Exception $exception) { // @TODO clean up once PHP 7 requirement is enforced
66+
$caughtException = $exception;
67+
}
68+
69+
if ($caughtException !== null) {
6270
$event->setName(MvcEvent::EVENT_DISPATCH_ERROR);
6371
$event->setError($application::ERROR_EXCEPTION);
6472
$event->setController($middlewareName);

src/View/Http/DefaultRenderingStrategy.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,17 @@ public function render(MvcEvent $e)
9999
$view->setRequest($request);
100100
$view->setResponse($response);
101101

102+
$caughtException = null;
103+
102104
try {
103105
$view->render($viewModel);
104-
} catch (\Exception $ex) {
106+
} catch (\Throwable $ex) {
107+
$caughtException = $ex;
108+
} catch (\Exception $ex) { // @TODO clean up once PHP 7 requirement is enforced
109+
$caughtException = $ex;
110+
}
111+
112+
if ($caughtException !== null) {
105113
if ($e->getName() === MvcEvent::EVENT_RENDER_ERROR) {
106114
throw $ex;
107115
}

src/View/Http/RouteNotFoundStrategy.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ protected function injectException($model, $e)
250250
$model->setVariable('display_exceptions', true);
251251

252252
$exception = $e->getParam('exception', false);
253-
if (!$exception instanceof \Exception) {
253+
254+
// @TODO clean up once PHP 7 requirement is enforced
255+
if (!$exception instanceof \Exception && !$exception instanceof \Throwable) {
254256
return;
255257
}
256258

test/ApplicationTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ public function setupActionController()
298298
return $this->application;
299299
}
300300

301-
public function setupBadController($addService = true)
301+
public function setupBadController($addService = true, $action = 'test')
302302
{
303303
$request = $this->serviceManager->get('Request');
304304
$request->setUri('http://example.local/bad');
@@ -308,7 +308,7 @@ public function setupBadController($addService = true)
308308
'route' => '/bad',
309309
'defaults' => [
310310
'controller' => 'bad',
311-
'action' => 'test',
311+
'action' => $action,
312312
],
313313
]);
314314
$router->addRoute('bad', $route);
@@ -381,6 +381,25 @@ public function testLocatorExceptionShouldTriggerDispatchError()
381381
$this->assertSame($response, $result->getResponse(), get_class($result));
382382
}
383383

384+
/**
385+
* @requires PHP 7.0
386+
* @group error-handling
387+
*/
388+
public function testPhp7ErrorRaisedInDispatchableShouldRaiseDispatchErrorEvent()
389+
{
390+
$this->setupBadController(true, 'test-php7-error');
391+
$response = $this->application->getResponse();
392+
$events = $this->application->getEventManager();
393+
$events->attach(MvcEvent::EVENT_DISPATCH_ERROR, function ($e) use ($response) {
394+
$exception = $e->getParam('exception');
395+
$response->setContent($exception->getMessage());
396+
return $response;
397+
});
398+
399+
$this->application->run();
400+
$this->assertContains('Raised an error', $response->getContent());
401+
}
402+
384403
/**
385404
* @group error-handling
386405
*/

test/Controller/TestAsset/BadController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,9 @@ public function testAction()
1717
{
1818
throw new \Exception('Raised an exception');
1919
}
20+
21+
public function testPhp7ErrorAction()
22+
{
23+
throw new \Error('Raised an error');
24+
}
2025
}

0 commit comments

Comments
 (0)