Skip to content

Commit 10b854f

Browse files
committed
Test with Slim
1 parent d04b3dc commit 10b854f

File tree

5 files changed

+117
-28
lines changed

5 files changed

+117
-28
lines changed

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "^4.8.6",
19-
"mikey179/vfsStream": "^1.6"
19+
"mikey179/vfsStream": "^1.6",
20+
"slim/slim": "^3.1"
2021
},
2122
"autoload": {
2223
"psr-4": {

src/PhpDebugBarMiddleware.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,33 +79,43 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
7979
*/
8080
private function getStaticFile(UriInterface $uri)
8181
{
82-
if (strpos($uri->getPath(), $this->debugBarRenderer->getBaseUrl()) !== 0) {
82+
$path = $this->extractPath($uri);
83+
84+
if (strpos($path, $this->debugBarRenderer->getBaseUrl()) !== 0) {
8385
return;
8486
}
8587

86-
if (method_exists($uri, 'getBasePath')) {
87-
if (empty($uri->getBasePath())) {
88-
return;
89-
}
90-
$path = $uri->getBasePath() ?: $uri->getPath();
91-
} else {
92-
$path = $uri->getPath();
93-
}
94-
9588
$pathToFile = substr($path, strlen($this->debugBarRenderer->getBaseUrl()));
96-
89+
9790
$fullPathToFile = $this->debugBarRenderer->getBasePath() . $pathToFile;
98-
91+
9992
if (!file_exists($fullPathToFile)) {
10093
return;
10194
}
102-
$fullPathToFile = str_replace('/', DIRECTORY_SEPARATOR, $fullPathToFile);
10395

104-
$stream = new Stream($fullPathToFile, 'r');
105-
$staticResponse = new Response($stream);
10696
$contentType = $this->getContentTypeByFileName($fullPathToFile);
107-
108-
return $staticResponse->withHeader('Content-type', $contentType);
97+
$stream = new Stream($fullPathToFile, 'r');
98+
99+
return new Response($stream, 200, [
100+
'Content-type' => $contentType,
101+
]);
102+
}
103+
104+
/**
105+
* @param UriInterface $uri
106+
*
107+
* @return string
108+
*/
109+
private function extractPath(UriInterface $uri)
110+
{
111+
// Slim3 compatibility
112+
if (method_exists($uri, 'getBasePath')) {
113+
$basePath = $uri->getBasePath();
114+
if (!empty($basePath)) {
115+
return $basePath;
116+
}
117+
}
118+
return $uri->getPath();
109119
}
110120

111121
/**

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/Slim3Test.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* To change this license header, choose License Headers in Project Properties.
5+
* To change this template file, choose Tools | Templates
6+
* and open the template in the editor.
7+
*/
8+
9+
namespace PhpMiddlewareTest\PhpDebugBar;
10+
11+
use DebugBar\StandardDebugBar;
12+
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
13+
use PHPUnit_Framework_TestCase;
14+
use Slim\App;
15+
16+
/**
17+
* Description of Slim3Test
18+
*
19+
* @author witold
20+
*/
21+
class Slim3Test extends PHPUnit_Framework_TestCase {
22+
23+
public function testSlim3() {
24+
$app = new App();
25+
$app->getContainer()['enviroment'] = function() {
26+
$items = array(
27+
'DOCUMENT_ROOT' => '/home/witold/projects/slim3/public',
28+
'REMOTE_ADDR' => '127.0.0.1',
29+
'REMOTE_PORT' => '59638',
30+
'SERVER_SOFTWARE' => 'PHP 7.0.4-7ubuntu2.1 Development Server',
31+
'SERVER_PROTOCOL' => 'HTTP/1.1',
32+
'SERVER_NAME' => '0.0.0.0',
33+
'SERVER_PORT' => '8080',
34+
'REQUEST_URI' => '/phpdebugbar/debugbar.js',
35+
'REQUEST_METHOD' => 'GET',
36+
'SCRIPT_NAME' => '/phpdebugbar/debugbar.js',
37+
'SCRIPT_FILENAME' => '/home/witold/projects/slim3/public/public/index.php',
38+
'PHP_SELF' => '/phpdebugbar/debugbar.js',
39+
'HTTP_HOST' => '0.0.0.0:8080',
40+
'HTTP_CONNECTION' => 'keep-alive',
41+
'HTTP_CACHE_CONTROL' => 'max-age=0',
42+
'HTTP_UPGRADE_INSECURE_REQUESTS' => '1',
43+
'HTTP_USER_AGENT' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36',
44+
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
45+
'HTTP_ACCEPT_ENCODING' => 'gzip, deflate, sdch',
46+
'HTTP_ACCEPT_LANGUAGE' => 'pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4',
47+
'HTTP_COOKIE' => 'zdt-hidden=0; PHPSESSID=tfun32lfu86islmbfk68s9eqi4',
48+
'REQUEST_TIME_FLOAT' => 1469139685.1076889,
49+
'REQUEST_TIME' => 1469139685,
50+
);
51+
return new \Slim\Http\Environment($items);
52+
};
53+
54+
$debugbar = new StandardDebugBar();
55+
$debugbarRenderer = $debugbar->getJavascriptRenderer('/phpdebugbar');
56+
$middleware = new PhpDebugBarMiddleware($debugbarRenderer);
57+
$app->add($middleware);
58+
59+
$app->get('/', function ($request, $response, $args) {
60+
$response->getBody()->write(' Hello ');
61+
62+
return $response;
63+
});
64+
65+
$response = $app->run(true);
66+
67+
$this->assertContains('phpdebugbar', (string) $response->getBody());
68+
}
69+
70+
}

0 commit comments

Comments
 (0)