mirror of
https://github.com/DirectoryLister/DirectoryLister.git
synced 2025-08-29 08:40:42 +02:00
Added middleware to modify the path when running from a sub-directory
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Bootstrap;
|
||||
|
||||
use App\Middleware;
|
||||
use App\Providers;
|
||||
use DI\Bridge\Slim\Bridge;
|
||||
use DI\Container;
|
||||
@@ -19,6 +20,11 @@ class AppManager
|
||||
Providers\TwigProvider::class,
|
||||
];
|
||||
|
||||
/** @const Constant description */
|
||||
protected const MIDDLEWARES = [
|
||||
Middleware\StripBasePathMiddleware::class,
|
||||
];
|
||||
|
||||
/** @var Container The applicaiton container */
|
||||
protected $container;
|
||||
|
||||
@@ -48,8 +54,10 @@ class AppManager
|
||||
public function __invoke(): App
|
||||
{
|
||||
$this->registerProviders();
|
||||
$app = Bridge::create($this->container);
|
||||
$this->registerMiddlewares($app);
|
||||
|
||||
return Bridge::create($this->container);
|
||||
return $app;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,4 +75,20 @@ class AppManager
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register application middleware.
|
||||
*
|
||||
* @param \Slim\App $app
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerMiddlewares(App $app): void
|
||||
{
|
||||
Collection::make(self::MIDDLEWARES)->merge(
|
||||
$this->config->get('app.middlewares', [])
|
||||
)->each(function (string $middleware) use ($app) {
|
||||
$app->add($middleware);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
57
app/Middleware/StripBasePathMiddleware.php
Normal file
57
app/Middleware/StripBasePathMiddleware.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Middleware;
|
||||
|
||||
use DI\Container;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Slim\Psr7\Response;
|
||||
|
||||
class StripBasePathMiddleware
|
||||
{
|
||||
/**
|
||||
* Create a new CanonicalizePathMiddleware object.
|
||||
*
|
||||
* @param \DI\Container $container
|
||||
*/
|
||||
public function __construct(Container $container)
|
||||
{
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Canonicalize the received path variable.
|
||||
*
|
||||
* @param \Slim\Psr7\Request $request
|
||||
* @param \Slim\Psr7\Response $response
|
||||
* @param callable $next
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __invoke(
|
||||
ServerRequestInterface $request,
|
||||
RequestHandlerInterface $handler
|
||||
): Response {
|
||||
$path = $request->getUri()->getPath();
|
||||
|
||||
$request = $request->withUri(
|
||||
$request->getUri()->withPath($this->stripBasePath($path))
|
||||
);
|
||||
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strip the base URL path from a path string.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function stripBasePath(string $path): string
|
||||
{
|
||||
$pattern = sprintf('/^%s/', preg_quote(dirname($_SERVER['SCRIPT_NAME']), '/'));
|
||||
|
||||
return preg_replace($pattern, '', $path);
|
||||
}
|
||||
}
|
@@ -65,4 +65,13 @@ return [
|
||||
'providers' => [
|
||||
// ...
|
||||
],
|
||||
|
||||
/**
|
||||
* Additional middlewares to be registered.
|
||||
*
|
||||
* Default value: []
|
||||
*/
|
||||
'middlewares' => [
|
||||
// ...
|
||||
],
|
||||
];
|
||||
|
@@ -4,6 +4,7 @@ namespace Tests\Support;
|
||||
|
||||
use App\Support\Helpers;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
class HelpersTest extends TestCase
|
||||
{
|
||||
@@ -47,4 +48,25 @@ class HelpersTest extends TestCase
|
||||
|
||||
$this->assertEquals('Test charlie; please ignore', $env);
|
||||
}
|
||||
|
||||
public function test_it_can_get_a_relative_path_from_one_path_to_another(): void
|
||||
{
|
||||
$relativePath = Helpers::realativePath('foo/bar', 'foo/bar/baz/qux');
|
||||
|
||||
$this->assertEquals('baz/qux', $relativePath);
|
||||
}
|
||||
|
||||
public function test_it_cat_get_a_relative_path_to_itself(): void
|
||||
{
|
||||
$relativePath = Helpers::realativePath('foo/bar/baz', 'foo/bar/baz');
|
||||
|
||||
$this->assertEquals('', $relativePath);
|
||||
}
|
||||
|
||||
public function test_it_can_get_a_relative_path_between_two_unrelated_paths(): void
|
||||
{
|
||||
$path = $this->expectException(RuntimeException::class);
|
||||
|
||||
Helpers::realativePath('foo/bar', 'baz/qux');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user