Skip to content

Commit d3db972

Browse files
authored
Merge pull request #12 from php-middleware/bugfix-slim3_compatibility
Slim3 compatibility
2 parents 4032e27 + 05d7f4d commit d3db972

9 files changed

+221
-28
lines changed

.travis.yml

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
language: php
22

3-
matrix:
4-
fast_finish: true
5-
include:
6-
- php: 5.5
7-
- php: 5.6
8-
- php: 7
9-
- php: hhvm
10-
allow_failures:
11-
- php: hhvm
3+
php:
4+
- 5.5
5+
- 5.6
6+
- 7.0
7+
- 7.1
8+
- hhvm
129

13-
install:
14-
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
15-
- composer info -i
10+
env:
11+
- DEPS=lowest
12+
- DEPS=latest
13+
14+
before_script:
15+
- composer self-update
16+
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable --no-interaction ; fi
17+
- travis_retry composer install --no-interaction
1618

1719
script:
1820
- ./vendor/bin/phpunit

README.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@ This middleware provide framework-agnostic possibility to attach [PHP Debug bar]
55

66
## Installation
77

8-
```json
9-
{
10-
"require": {
11-
"php-middleware/php-debug-bar": "^1.1.0"
12-
}
13-
}
8+
```
9+
composer require php-middleware/php-debug-bar
1410
```
1511

1612
To build this middleware you need to injecting inside `PhpDebugBarMiddleware` instance `DebugBar\JavascriptRenderer` (you can get it from `DebugBar\StandardDebugBar`) and add middleware to your middleware runner. Or use default factory.
@@ -32,12 +28,24 @@ $app->run($request, $response);
3228

3329
You don't need to copy any static assets from phpdebugbar vendor!
3430

31+
### How to install on Slim 3?
32+
33+
Add existing factory to container:
34+
35+
```php
36+
$container['debugbar_middleware'] = new PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory();
37+
```
38+
39+
and add middleware from container to app:
40+
41+
```php
42+
$app->add($app->getContainer()->get('debugbar_middleware'));
43+
```
44+
3545
## It's just works with any modern php framework!
3646

3747
Middleware tested on:
38-
* [Expressive](https://github.com/zendframework/zend-expressive)
39-
40-
Middleware should works with:
48+
* [Zend Expressive](https://github.com/zendframework/zend-expressive)
4149
* [Slim 3.x](https://github.com/slimphp/Slim)
4250

4351
And any other modern framework [supported middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html).

composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "^4.8.6",
19-
"mikey179/vfsStream": "^1.6"
19+
"mikey179/vfsStream": "^1.6",
20+
"slim/slim": "^3.0",
21+
"zendframework/zend-expressive": "^1.0",
22+
"zendframework/zend-expressive-fastroute": "^1.0",
23+
"zendframework/zend-servicemanager": "^3.0"
2024
},
2125
"autoload": {
2226
"psr-4": {

src/PhpDebugBarMiddleware.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Http\Message\ResponseInterface;
88
use Psr\Http\Message\ServerRequestInterface;
99
use Psr\Http\Message\UriInterface;
10+
use Slim\Http\Uri;
1011
use Zend\Diactoros\Response;
1112
use Zend\Diactoros\Response\HtmlResponse;
1213
use Zend\Diactoros\Response\Serializer;
@@ -79,23 +80,43 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
7980
*/
8081
private function getStaticFile(UriInterface $uri)
8182
{
82-
if (strpos($uri->getPath(), $this->debugBarRenderer->getBaseUrl()) !== 0) {
83+
$path = $this->extractPath($uri);
84+
85+
if (strpos($path, $this->debugBarRenderer->getBaseUrl()) !== 0) {
8386
return;
8487
}
8588

86-
$pathToFile = substr($uri->getPath(), strlen($this->debugBarRenderer->getBaseUrl()));
89+
$pathToFile = substr($path, strlen($this->debugBarRenderer->getBaseUrl()));
8790

8891
$fullPathToFile = $this->debugBarRenderer->getBasePath() . $pathToFile;
8992

9093
if (!file_exists($fullPathToFile)) {
9194
return;
9295
}
9396

94-
$stream = new Stream($fullPathToFile, 'r');
95-
$staticResponse = new Response($stream);
9697
$contentType = $this->getContentTypeByFileName($fullPathToFile);
98+
$stream = new Stream($fullPathToFile, 'r');
9799

98-
return $staticResponse->withHeader('Content-type', $contentType);
100+
return new Response($stream, 200, [
101+
'Content-type' => $contentType,
102+
]);
103+
}
104+
105+
/**
106+
* @param UriInterface $uri
107+
*
108+
* @return string
109+
*/
110+
private function extractPath(UriInterface $uri)
111+
{
112+
// Slim3 compatibility
113+
if ($uri instanceof Uri) {
114+
$basePath = $uri->getBasePath();
115+
if (!empty($basePath)) {
116+
return $basePath;
117+
}
118+
}
119+
return $uri->getPath();
99120
}
100121

101122
/**

src/PhpDebugBarMiddlewareFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @author Witold Wasiczko <witold@wasiczko.pl>
1111
*/
12-
class PhpDebugBarMiddlewareFactory
12+
final class PhpDebugBarMiddlewareFactory
1313
{
1414
public function __invoke()
1515
{

test/AbstractMiddlewareRunnerTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\PhpDebugBar;
4+
5+
use PHPUnit_Framework_TestCase;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Psr\Http\Message\ServerRequestInterface;
8+
9+
abstract class AbstractMiddlewareRunnerTest extends PHPUnit_Framework_TestCase
10+
{
11+
12+
final public function testAppendJsIntoHtmlContent()
13+
{
14+
$response = $this->dispatchApplication([
15+
'REQUEST_URI' => '/hello',
16+
'REQUEST_METHOD' => 'GET',
17+
'HTTP_ACCEPT' => 'text/html',
18+
], [
19+
'/hello' => function (ServerRequestInterface $request, ResponseInterface $response, $next) {
20+
$response->getBody()->write('Hello!');
21+
return $response;
22+
},
23+
]);
24+
25+
$responseBody = (string) $response->getBody();
26+
27+
$this->assertContains('var phpdebugbar = new PhpDebugBar.DebugBar();', $responseBody);
28+
$this->assertContains('Hello!', $responseBody);
29+
$this->assertContains('"/phpdebugbar/debugbar.js"', $responseBody);
30+
}
31+
32+
final public function testGetStatics()
33+
{
34+
$response = $this->dispatchApplication([
35+
'DOCUMENT_ROOT' => __DIR__,
36+
'REMOTE_ADDR' => '127.0.0.1',
37+
'REMOTE_PORT' => '40226',
38+
'SERVER_SOFTWARE' => 'PHP 7.0.8-3ubuntu3 Development Server',
39+
'SERVER_PROTOCOL' => 'HTTP/1.1',
40+
'SERVER_NAME' => '0.0.0.0',
41+
'SERVER_PORT' => '8080',
42+
'REQUEST_URI' => '/phpdebugbar/debugbar.js',
43+
'REQUEST_METHOD' => 'GET',
44+
'SCRIPT_NAME' => '/phpdebugbar/debugbar.js',
45+
'SCRIPT_FILENAME' => __FILE__,
46+
'PHP_SELF' => '/phpdebugbar/debugbar.js',
47+
'HTTP_HOST' => '0.0.0.0:8080',
48+
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
49+
]);
50+
51+
$contentType = $response->getHeaderLine('Content-type');
52+
53+
$this->assertContains('text/javascript', $contentType);
54+
}
55+
56+
/**
57+
* @return ResponseInterface
58+
*/
59+
abstract protected function dispatchApplication(array $server, array $pipe = []);
60+
}

test/Slim3Test.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\PhpDebugBar;
4+
5+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
6+
use Slim\App;
7+
use Slim\Http\Environment;
8+
9+
final class Slim3Test extends AbstractMiddlewareRunnerTest
10+
{
11+
protected function dispatchApplication(array $server, array $pipe = [])
12+
{
13+
$app = new App();
14+
$app->getContainer()['environment'] = function() use ($server) {
15+
return new Environment($server);
16+
};
17+
18+
$middlewareFactory = new PhpDebugBarMiddlewareFactory();
19+
$middleware = $middlewareFactory();
20+
21+
$app->add($middleware);
22+
23+
foreach ($pipe as $pattern => $middleware) {
24+
$app->get($pattern, $middleware);
25+
}
26+
27+
return $app->run(true);
28+
}
29+
}

test/TestEmitter.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\PhpDebugBar;
4+
5+
use BadMethodCallException;
6+
use Psr\Http\Message\ResponseInterface;
7+
use Zend\Diactoros\Response\EmitterInterface;
8+
9+
final class TestEmitter implements EmitterInterface
10+
{
11+
private $response;
12+
13+
public function emit(ResponseInterface $response)
14+
{
15+
$this->response = $response;
16+
17+
return $response;
18+
}
19+
20+
public function getResponse()
21+
{
22+
if ($this->response instanceof ResponseInterface) {
23+
return $this->response;
24+
}
25+
26+
throw new BadMethodCallException('Not emitted yet');
27+
}
28+
}

test/ZendExpressiveTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace PhpMiddlewareTest\PhpDebugBar;
4+
5+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
6+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
7+
use Zend\Diactoros\ServerRequestFactory;
8+
use Zend\Expressive\Application;
9+
use Zend\Expressive\Router\FastRouteRouter;
10+
use Zend\ServiceManager\ServiceManager;
11+
12+
final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
13+
{
14+
protected function dispatchApplication(array $server, array $pipe = [])
15+
{
16+
$container = new ServiceManager([
17+
'factories' => [
18+
PhpDebugBarMiddleware::class => PhpDebugBarMiddlewareFactory::class,
19+
],
20+
]);
21+
$router = new FastRouteRouter();
22+
$emitter = new TestEmitter();
23+
24+
$app = new Application($router, $container, null, $emitter);
25+
26+
$app->pipe(PhpDebugBarMiddleware::class);
27+
28+
foreach ($pipe as $pattern => $middleware) {
29+
$app->get($pattern, $middleware);
30+
}
31+
32+
$app->pipeRoutingMiddleware();
33+
$app->pipeDispatchMiddleware();
34+
35+
$serverRequest = ServerRequestFactory::fromGlobals($server);
36+
37+
$app->run($serverRequest);
38+
39+
return $emitter->getResponse();
40+
}
41+
}

0 commit comments

Comments
 (0)