Skip to content

Slim3 compatibility #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Nov 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
language: php

matrix:
fast_finish: true
include:
- php: 5.5
- php: 5.6
- php: 7
- php: hhvm
allow_failures:
- php: hhvm
php:
- 5.5
- 5.6
- 7.0
- 7.1
- hhvm

install:
- travis_retry composer install --no-interaction --ignore-platform-reqs --prefer-source
- composer info -i
env:
- DEPS=lowest
- DEPS=latest

before_script:
- composer self-update
- if [[ $DEPS == 'lowest' ]]; then travis_retry composer update --prefer-lowest --prefer-stable --no-interaction ; fi
- travis_retry composer install --no-interaction

script:
- ./vendor/bin/phpunit
26 changes: 17 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ This middleware provide framework-agnostic possibility to attach [PHP Debug bar]

## Installation

```json
{
"require": {
"php-middleware/php-debug-bar": "^1.1.0"
}
}
```
composer require php-middleware/php-debug-bar
```

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.
Expand All @@ -32,12 +28,24 @@ $app->run($request, $response);

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

### How to install on Slim 3?

Add existing factory to container:

```php
$container['debugbar_middleware'] = new PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory();
```

and add middleware from container to app:

```php
$app->add($app->getContainer()->get('debugbar_middleware'));
```

## It's just works with any modern php framework!

Middleware tested on:
* [Expressive](https://github.com/zendframework/zend-expressive)

Middleware should works with:
* [Zend Expressive](https://github.com/zendframework/zend-expressive)
* [Slim 3.x](https://github.com/slimphp/Slim)

And any other modern framework [supported middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html).
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
},
"require-dev": {
"phpunit/phpunit": "^4.8.6",
"mikey179/vfsStream": "^1.6"
"mikey179/vfsStream": "^1.6",
"slim/slim": "^3.0",
"zendframework/zend-expressive": "^1.0",
"zendframework/zend-expressive-fastroute": "^1.0",
"zendframework/zend-servicemanager": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
31 changes: 26 additions & 5 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\UriInterface;
use Slim\Http\Uri;
use Zend\Diactoros\Response;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\Serializer;
Expand Down Expand Up @@ -79,23 +80,43 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
*/
private function getStaticFile(UriInterface $uri)
{
if (strpos($uri->getPath(), $this->debugBarRenderer->getBaseUrl()) !== 0) {
$path = $this->extractPath($uri);

if (strpos($path, $this->debugBarRenderer->getBaseUrl()) !== 0) {
return;
}

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

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

if (!file_exists($fullPathToFile)) {
return;
}

$stream = new Stream($fullPathToFile, 'r');
$staticResponse = new Response($stream);
$contentType = $this->getContentTypeByFileName($fullPathToFile);
$stream = new Stream($fullPathToFile, 'r');

return $staticResponse->withHeader('Content-type', $contentType);
return new Response($stream, 200, [
'Content-type' => $contentType,
]);
}

/**
* @param UriInterface $uri
*
* @return string
*/
private function extractPath(UriInterface $uri)
{
// Slim3 compatibility
if ($uri instanceof Uri) {
$basePath = $uri->getBasePath();
if (!empty($basePath)) {
return $basePath;
}
}
return $uri->getPath();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/PhpDebugBarMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author Witold Wasiczko <witold@wasiczko.pl>
*/
class PhpDebugBarMiddlewareFactory
final class PhpDebugBarMiddlewareFactory
{
public function __invoke()
{
Expand Down
60 changes: 60 additions & 0 deletions test/AbstractMiddlewareRunnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace PhpMiddlewareTest\PhpDebugBar;

use PHPUnit_Framework_TestCase;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;

abstract class AbstractMiddlewareRunnerTest extends PHPUnit_Framework_TestCase
{

final public function testAppendJsIntoHtmlContent()
{
$response = $this->dispatchApplication([
'REQUEST_URI' => '/hello',
'REQUEST_METHOD' => 'GET',
'HTTP_ACCEPT' => 'text/html',
], [
'/hello' => function (ServerRequestInterface $request, ResponseInterface $response, $next) {
$response->getBody()->write('Hello!');
return $response;
},
]);

$responseBody = (string) $response->getBody();

$this->assertContains('var phpdebugbar = new PhpDebugBar.DebugBar();', $responseBody);
$this->assertContains('Hello!', $responseBody);
$this->assertContains('"/phpdebugbar/debugbar.js"', $responseBody);
}

final public function testGetStatics()
{
$response = $this->dispatchApplication([
'DOCUMENT_ROOT' => __DIR__,
'REMOTE_ADDR' => '127.0.0.1',
'REMOTE_PORT' => '40226',
'SERVER_SOFTWARE' => 'PHP 7.0.8-3ubuntu3 Development Server',
'SERVER_PROTOCOL' => 'HTTP/1.1',
'SERVER_NAME' => '0.0.0.0',
'SERVER_PORT' => '8080',
'REQUEST_URI' => '/phpdebugbar/debugbar.js',
'REQUEST_METHOD' => 'GET',
'SCRIPT_NAME' => '/phpdebugbar/debugbar.js',
'SCRIPT_FILENAME' => __FILE__,
'PHP_SELF' => '/phpdebugbar/debugbar.js',
'HTTP_HOST' => '0.0.0.0:8080',
'HTTP_ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
]);

$contentType = $response->getHeaderLine('Content-type');

$this->assertContains('text/javascript', $contentType);
}

/**
* @return ResponseInterface
*/
abstract protected function dispatchApplication(array $server, array $pipe = []);
}
29 changes: 29 additions & 0 deletions test/Slim3Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace PhpMiddlewareTest\PhpDebugBar;

use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
use Slim\App;
use Slim\Http\Environment;

final class Slim3Test extends AbstractMiddlewareRunnerTest
{
protected function dispatchApplication(array $server, array $pipe = [])
{
$app = new App();
$app->getContainer()['environment'] = function() use ($server) {
return new Environment($server);
};

$middlewareFactory = new PhpDebugBarMiddlewareFactory();
$middleware = $middlewareFactory();

$app->add($middleware);

foreach ($pipe as $pattern => $middleware) {
$app->get($pattern, $middleware);
}

return $app->run(true);
}
}
28 changes: 28 additions & 0 deletions test/TestEmitter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PhpMiddlewareTest\PhpDebugBar;

use BadMethodCallException;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Response\EmitterInterface;

final class TestEmitter implements EmitterInterface
{
private $response;

public function emit(ResponseInterface $response)
{
$this->response = $response;

return $response;
}

public function getResponse()
{
if ($this->response instanceof ResponseInterface) {
return $this->response;
}

throw new BadMethodCallException('Not emitted yet');
}
}
41 changes: 41 additions & 0 deletions test/ZendExpressiveTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace PhpMiddlewareTest\PhpDebugBar;

use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddlewareFactory;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Expressive\Application;
use Zend\Expressive\Router\FastRouteRouter;
use Zend\ServiceManager\ServiceManager;

final class ZendExpressiveTest extends AbstractMiddlewareRunnerTest
{
protected function dispatchApplication(array $server, array $pipe = [])
{
$container = new ServiceManager([
'factories' => [
PhpDebugBarMiddleware::class => PhpDebugBarMiddlewareFactory::class,
],
]);
$router = new FastRouteRouter();
$emitter = new TestEmitter();

$app = new Application($router, $container, null, $emitter);

$app->pipe(PhpDebugBarMiddleware::class);

foreach ($pipe as $pattern => $middleware) {
$app->get($pattern, $middleware);
}

$app->pipeRoutingMiddleware();
$app->pipeDispatchMiddleware();

$serverRequest = ServerRequestFactory::fromGlobals($server);

$app->run($serverRequest);

return $emitter->getResponse();
}
}