Skip to content
Open
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
13 changes: 2 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,8 @@ jobs:
- '8.1'
- '8.2'
- '8.3'
exclude:
# Lowest bounds of the dev dependencies (Slim 3.0, Pimple 3.0,
# vfsStream 1.6.8) were released before PHP 8.1 and fatal on load
# there. That is a limitation of those old releases, not of this
# library: every tested PHP version is covered by the highest jobs.
- dependencies: lowest
php-versions: '8.1'
- dependencies: lowest
php-versions: '8.2'
- dependencies: lowest
php-versions: '8.3'
- '8.4'
- '8.5'
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand Down
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ $app->pipe(\PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class);

For more - follow Mezzio [documentation](https://docs.mezzio.dev/mezzio/v3/features/modular-applications/).

### How to install on Slim 4?

Register factories in a PSR-11 container of your choice, then add the middleware
resolved from the container:

```php
$app = \Slim\Factory\AppFactory::create();
$app->add($container->get(\PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware::class));
```

### How to install on Slim 3?

Register factories in container:
Expand Down Expand Up @@ -115,6 +125,6 @@ return array_merge(PhpMiddleware\PhpDebugBar\ConfigProvider::getConfig(), $myOve

Middleware tested on:
* [Mezzio](https://github.com/mezzio/mezzio)
* [Slim 3.x](https://github.com/slimphp/Slim)
* [Slim 4.x](https://github.com/slimphp/Slim)

And any other modern framework [supported PSR-17 middlewares and PSR-7](https://mwop.net/blog/2015-01-08-on-http-middleware-and-psr-7.html).
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"psr/http-factory-implementation": "^1.0"
},
"require-dev": {
"phpunit/phpunit": "^9.1.4",
"mikey179/vfsstream": "^1.6.8",
"symfony/var-dumper": "^4.4.30",
"slim/slim": "^3.0",
"phpunit/phpunit": "^9.6.19",
"mikey179/vfsstream": "^1.6.12",
"symfony/var-dumper": "^5.4.48",
"slim/slim": "^4.15.2",
"mezzio/mezzio": "^3.0",
"mezzio/mezzio-fastroute": "^3.0.1",
"laminas/laminas-servicemanager": "^3.3.2",
"laminas/laminas-diactoros": "^2.0",
"laminas/laminas-diactoros": "^2.18.1 || ^3.0",
"phpstan/phpstan": "^1.4"
},
"autoload": {
Expand Down
9 changes: 5 additions & 4 deletions src/PhpDebugBarMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface as RequestHandler;
use Slim\Http\Uri as SlimUri;

/**
* @author Witold Wasiczko <witold@wasiczko.pl>
Expand Down Expand Up @@ -168,9 +167,11 @@ private function getStaticFile(UriInterface $uri): ?Response

private function extractPath(UriInterface $uri): string
{
// Slim3 compatibility
if ($uri instanceof SlimUri) {
$basePath = $uri->getBasePath();
// Slim3 compatibility: Slim\Http\Uri is duck-typed so slim/slim is not
// needed at analysis time (dev dependencies ship Slim 4, which has no
// such class).
if (method_exists($uri, 'getBasePath')) {
$basePath = (string) $uri->getBasePath();
if (!empty($basePath)) {
return $basePath;
}
Expand Down
23 changes: 23 additions & 0 deletions test/PhpDebugBarMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,29 @@ public function testHandleStaticFile(string $extension, string $contentType): vo
$this->assertSame('filecontent', (string) $result->getBody());
}

public function testHandleStaticFileUsingSlim3UriBasePath(): void
{
$root = vfsStream::setup('boo');

$this->debugbarRenderer->expects($this->any())->method('getBasePath')->willReturn(vfsStream::url('boo'));

// Slim 3 serves the app through a front controller: the script itself is
// the base path and the URI path is only "/".
$uri = new SlimUriStub('/phpdebugbar/debugbar.js', '/');
$request = new ServerRequest([], [], $uri, null, 'php://memory');
$response = new Response\HtmlResponse('<html></html>');

vfsStream::newFile('debugbar.js')->withContent('filecontent')->at($root);

$requestHandler = new RequestHandlerStub($response);

$result = $this->middleware->process($request, $requestHandler);

$this->assertFalse($requestHandler->isCalled(), 'Request handler is called');
$this->assertSame('text/javascript', $result->getHeaderLine('Content-type'));
$this->assertSame('filecontent', (string) $result->getBody());
}

public function getContentTypes(): array
{
return [
Expand Down
44 changes: 0 additions & 44 deletions test/Slim3Test.php

This file was deleted.

44 changes: 44 additions & 0 deletions test/Slim4Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare (strict_types=1);

namespace PhpMiddlewareTest\PhpDebugBar;

use Laminas\Diactoros\ResponseFactory;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Laminas\ServiceManager\ServiceManager;
use PhpMiddleware\PhpDebugBar\ConfigProvider;
use PhpMiddleware\PhpDebugBar\PhpDebugBarMiddleware;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Slim\Factory\AppFactory;

final class Slim4Test extends AbstractMiddlewareRunnerTest
{
protected function dispatchApplication(array $server, array $pipe = []): ResponseInterface
{
$container = new ServiceManager();
$container->setService(ResponseFactoryInterface::class, new ResponseFactory());
$container->setService(StreamFactoryInterface::class, new StreamFactory());

$config = ConfigProvider::getConfig();

foreach ($config['dependencies']['factories'] as $name => $factory) {
$container->setFactory($name, $factory);
}

$app = AppFactory::create(new ResponseFactory());

$app->add($container->get(PhpDebugBarMiddleware::class));

foreach ($pipe as $pattern => $handler) {
$app->get($pattern, function (ServerRequestInterface $request) use ($handler): ResponseInterface {
return $handler($request);
});
}

return $app->handle(ServerRequestFactory::fromGlobals($server));
}
}
112 changes: 112 additions & 0 deletions test/SlimUriStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php
declare (strict_types=1);

namespace PhpMiddlewareTest\PhpDebugBar;

use Psr\Http\Message\UriInterface;

/**
* Mimics Slim 3 Slim\Http\Uri: a PSR-7 URI with an extra getBasePath() method,
* used to cover the duck-typed Slim3 branch of PhpDebugBarMiddleware::extractPath()
* without depending on slim/slim 3. Parameters are untyped on purpose so the
* stub satisfies both psr/http-message ^1.0 and ^2.0.
*/
final class SlimUriStub implements UriInterface
{
/** @var string */
private $basePath;

/** @var string */
private $path;

public function __construct(string $basePath, string $path)
{
$this->basePath = $basePath;
$this->path = $path;
}

public function getBasePath(): string
{
return $this->basePath;
}

public function getScheme(): string
{
return 'http';
}

public function getAuthority(): string
{
return 'example.com';
}

public function getUserInfo(): string
{
return '';
}

public function getHost(): string
{
return 'example.com';
}

public function getPort(): ?int
{
return null;
}

public function getPath(): string
{
return $this->path;
}

public function getQuery(): string
{
return '';
}

public function getFragment(): string
{
return '';
}

public function withScheme($scheme): UriInterface
{
return $this;
}

public function withUserInfo($user, $password = null): UriInterface
{
return $this;
}

public function withHost($host): UriInterface
{
return $this;
}

public function withPort($port): UriInterface
{
return $this;
}

public function withPath($path): UriInterface
{
return $this;
}

public function withQuery($query): UriInterface
{
return $this;
}

public function withFragment($fragment): UriInterface
{
return $this;
}

public function __toString(): string
{
return $this->basePath . $this->path;
}
}
Loading