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

Commit e8fdf76

Browse files
committed
Merge branch 'hotfix/400'
Close #402 Close #401 Fixes #400
2 parents 060e02f + ee14fe3 commit e8fdf76

File tree

3 files changed

+93
-6
lines changed

3 files changed

+93
-6
lines changed

CHANGELOG.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

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

5+
## 1.0.4 - 2016-12-07
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Deprecated
12+
13+
- Nothing.
14+
15+
### Removed
16+
17+
- Nothing.
18+
19+
### Fixed
20+
21+
- [#402](https://github.com/zendframework/zend-expressive/pull/402) fixes how
22+
`Application::__invoke()` registers the error handler designed to swallow
23+
deprecation notices, as introduced in 1.0.3. It now checks to see if another
24+
error handler was previously registered, and, if so, creates a composite
25+
handler that will delegate to the previous for all other errors.
26+
527
## 1.0.3 - 2016-11-11
628

729
### Added

src/Application.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,16 @@ public function __construct(
129129
*
130130
* If $out is not provided, uses the result of `getFinalHandler()`.
131131
*
132-
* @todo Remove logic for creating final handler for version 2.0.
133-
* @todo Remove error handler for deprecation notice due to triggering
134-
* error middleware for version 2.0.0.
132+
* @todo Remove logic for creating final handler for version 2.0.0.
133+
* @todo Remove swallowDeprecationNotices() invocation for version 2.0.0.
135134
* @param ServerRequestInterface $request
136135
* @param ResponseInterface $response
137136
* @param callable|null $out
138137
* @return ResponseInterface
139138
*/
140139
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $out = null)
141140
{
142-
set_error_handler(function ($errno, $errstr) {
143-
return false !== strstr($errstr, 'error middleware is deprecated');
144-
}, E_USER_DEPRECATED);
141+
$this->swallowDeprecationNotices();
145142

146143
if (! $out && (null === ($out = $this->getFinalHandler($response)))) {
147144
$response = $response instanceof StratigilityResponse
@@ -671,4 +668,33 @@ private function checkForDuplicateRoute($path, $methods = null)
671668
);
672669
}
673670
}
671+
672+
/**
673+
* Register an error handler to swallow deprecation notices due to error middleware usage.
674+
*
675+
* @todo Remove method for version 2.0.0.
676+
* @return void
677+
*/
678+
private function swallowDeprecationNotices()
679+
{
680+
$handler = function ($errno, $errstr) {
681+
if ($errno !== E_USER_DEPRECATED) {
682+
return false;
683+
}
684+
return false !== strstr($errstr, 'error middleware is deprecated');
685+
};
686+
687+
$previous = set_error_handler($handler);
688+
if (! $previous) {
689+
return;
690+
}
691+
692+
set_error_handler(function ($errno, $errstr, $errfile, $errline, $errcontext) use ($handler, $previous) {
693+
if ($handler($errno, $errstr)) {
694+
return true;
695+
}
696+
697+
return $previous($errno, $errstr, $errfile, $errline, $errcontext);
698+
});
699+
}
674700
}

test/IntegrationTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@
2121

2222
class IntegrationTest extends TestCase
2323
{
24+
public $errorHandler;
2425
public $response;
2526

2627
public function setUp()
2728
{
2829
$this->response = null;
30+
$this->errorHandler = null;
31+
}
32+
33+
public function tearDown()
34+
{
35+
if ($this->errorHandler) {
36+
set_error_handler($this->errorHandler);
37+
$this->errorHandler = null;
38+
}
2939
}
3040

3141
public function getEmitter()
@@ -67,4 +77,33 @@ public function testInjectedFinalHandlerCanEmitA404WhenNoMiddlewareMatches()
6777
$this->assertInstanceOf(ResponseInterface::class, $this->response);
6878
$this->assertEquals(404, $this->response->getStatusCode());
6979
}
80+
81+
/**
82+
* @todo Remove for version 2.0, as that version will remove error middleware support
83+
* @group 400
84+
*/
85+
public function testErrorMiddlewareDeprecationErrorHandlerWillNotOverridePreviouslyRegisteredErrorHandler()
86+
{
87+
$triggered = false;
88+
$this->errorHandler = set_error_handler(function ($errno, $errstr) use (&$triggered) {
89+
$triggered = true;
90+
return true;
91+
});
92+
93+
$expected = new Response();
94+
$middleware = function ($request, $response, $next) use ($expected) {
95+
trigger_error('Triggered', E_USER_NOTICE);
96+
return $expected;
97+
};
98+
99+
$app = new Application(new FastRouteRouter(), null, null, $this->getEmitter());
100+
$app->pipe($middleware);
101+
102+
$request = new ServerRequest([], [], 'https://example.com/foo', 'GET');
103+
$response = clone $expected;
104+
105+
$app->run($request, $response);
106+
107+
$this->assertTrue($triggered);
108+
}
70109
}

0 commit comments

Comments
 (0)