Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4', '8.0', '8.1', '8.2', '8.3']
php-versions: ['8.3', '8.4']
steps:
- name: Checkout
uses: actions/checkout@v2
Expand Down
14 changes: 7 additions & 7 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
"description": "Symfony with enhanced DI and a Silex-like interface",
"type": "library",
"require": {
"php": ">=7.2",
"php": ">=8.3",
"ext-json": "*",
"psr/container": "^1.0",
"symfony/http-kernel": "^4.4",
"symfony/http-foundation": "^4.4",
"symfony/routing": "^4.4",
"symfony/framework-bundle": "^4.4",
"symfony/dependency-injection": "^4.4"
"symfony/http-kernel": "^7.3",
"symfony/http-foundation": "^7.3",
"symfony/routing": "^7.3",
"symfony/framework-bundle": "^7.3",
"symfony/dependency-injection": "^7.3"
},
"require-dev": {
"phpunit/phpunit": "^8"
"phpunit/phpunit": "^12"
},
"license": "LGPL-3.0",
"authors": [
Expand Down
6 changes: 2 additions & 4 deletions src/ContainerProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

declare(strict_types=1);

namespace Clearbooks\Dilex;
Expand All @@ -7,10 +8,7 @@

class ContainerProvider
{
/**
* @var ContainerInterface
*/
private $container = null;
private ?ContainerInterface $container = null;

public function getContainer(): ?ContainerInterface
{
Expand Down
19 changes: 11 additions & 8 deletions src/ContainerWithFallback.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
<?php

declare(strict_types=1);

namespace Clearbooks\Dilex;

use Psr\Container\ContainerInterface;
use Symfony\Component\DependencyInjection\Container;

class ContainerWithFallback extends Container
{
/**
* @var ContainerInterface
*/
private $fallbackContainer;
private ?ContainerInterface $fallbackContainer = null;

public function setFallbackContainer( ContainerInterface $fallbackContainer ): void
{
public function setFallbackContainer(
ContainerInterface $fallbackContainer
): void {
$this->fallbackContainer = $fallbackContainer;
}

public function has( $id )
#[\Override]
public function has( string $id ): bool
{
if ( parent::has( $id ) ) {
return true;
Expand All @@ -29,7 +31,8 @@ public function has( $id )
return $this->fallbackContainer->has( $id );
}

public function get( $id, $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE )
#[\Override]
public function get( string $id, int $invalidBehavior = self::EXCEPTION_ON_INVALID_REFERENCE ): ?object
{
if ( !$this->fallbackContainer || parent::has( $id ) ) {
return parent::get( $id );
Expand Down
142 changes: 52 additions & 90 deletions src/Dilex.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

namespace Clearbooks\Dilex;

use Clearbooks\Dilex\EventListener\CallbackWrapper\AfterWrapper;
Expand All @@ -11,7 +14,6 @@
use Clearbooks\Dilex\EventListener\ListenerRunner\AfterControllerListenerRunner;
use Clearbooks\Dilex\EventListener\ListenerRunner\BeforeControllerListenerRunner;
use Clearbooks\Dilex\EventListener\StringToResponseListener;
use Exception;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
Expand All @@ -21,87 +23,36 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

use function trim;
use function rtrim;

class Dilex extends Kernel implements RouteContainer, EventListenerApplier
{
use MicroKernelTrait;

/**
* @var ContainerInterface|null
*/
private $fallbackContainerInterface;

/**
* @var RouteRegistry
*/
private $routeRegistry;

/**
* @var ContainerProvider
*/
private $containerProvider;

/**
* @var EventListenerRegistry
*/
private $eventListenerRegistry;

/**
* @var BeforeWrapper
*/
private $beforeEventListenerWrapper;

/**
* @var AfterWrapper
*/
private $afterEventListenerWrapper;

/**
* @var FinishWrapper
*/
private $finishEventListenerWrapper;

/**
* @var ErrorWrapper
*/
private $errorEventListenerWrapper;

/**
* @var BeforeControllerListenerRunner
*/
private $beforeControllerListenerRunner;

/**
* @var AfterControllerListenerRunner
*/
private $afterControllerListenerRunner;

/**
* @var StringToResponseListener
*/
private $stringToResponseListener;

/**
* @var string|null
*/
private $projectDirectory = null;

/**
* @var string|null
*/
private $cacheDirectory = null;

/**
* @var string|null
*/
private $logDirectory = null;

public function __construct( string $environment, bool $debug,
ContainerInterface $fallbackContainerInterface = null )
{
private RouteRegistry $routeRegistry;
private ContainerProvider $containerProvider;
private EventListenerRegistry $eventListenerRegistry;
private BeforeWrapper $beforeEventListenerWrapper;
private AfterWrapper $afterEventListenerWrapper;
private FinishWrapper $finishEventListenerWrapper;
private ErrorWrapper $errorEventListenerWrapper;
private BeforeControllerListenerRunner $beforeControllerListenerRunner;
private AfterControllerListenerRunner $afterControllerListenerRunner;
private StringToResponseListener $stringToResponseListener;
private ?string $projectDirectory = null;
private ?string $cacheDirectory = null;
private ?string $logDirectory = null;

public function __construct(
string $environment,
bool $debug,
private readonly ?ContainerInterface $fallbackContainerInterface = null
) {
parent::__construct( $environment, $debug );
$this->fallbackContainerInterface = $fallbackContainerInterface;

$this->routeRegistry = new RouteRegistry();
$this->containerProvider = new ContainerProvider();
$this->eventListenerRegistry = new EventListenerRegistry();
Expand Down Expand Up @@ -129,7 +80,8 @@ public function setLogDirectory( ?string $logDirectory ): void
$this->logDirectory = $logDirectory === null ? null : ( '/' . trim( $logDirectory, '/' ) );
}

public function getProjectDir()
#[\Override]
public function getProjectDir(): string
{
if ( $this->projectDirectory === null ) {
return parent::getProjectDir();
Expand All @@ -138,7 +90,8 @@ public function getProjectDir()
return $this->projectDirectory;
}

public function getCacheDir()
#[\Override]
public function getCacheDir(): string
{
if ( $this->cacheDirectory === null ) {
return parent::getCacheDir();
Expand All @@ -147,7 +100,8 @@ public function getCacheDir()
return $this->getProjectDir() . $this->cacheDirectory . $this->getEnvironment();
}

public function getLogDir()
#[\Override]
public function getLogDir(): string
{
if ( $this->logDirectory === null ) {
return parent::getLogDir();
Expand All @@ -156,6 +110,7 @@ public function getLogDir()
return $this->getProjectDir() . $this->logDirectory;
}

#[\Override]
public function registerBundles(): array
{
return [
Expand All @@ -168,19 +123,21 @@ protected function configureContainer( ContainerBuilder $container, LoaderInterf

}

protected function configureRoutes( RouteCollectionBuilder $routes ): void
protected function configureRoutes( RoutingConfigurator $routes ): void
{
foreach ( $this->routeRegistry->getRoutes() as $route ) {
$routes->addRoute( $route );
RouteApplier::applyRouteToSymfony($route, $routes);
}
}

protected function getContainerBaseClass()
#[\Override]
protected function getContainerBaseClass(): string
{
return ContainerWithFallback::class;
}

protected function initializeContainer()
#[\Override]
protected function initializeContainer(): void
{
parent::initializeContainer();

Expand Down Expand Up @@ -228,41 +185,49 @@ private function initializeListeners(): void
$this->eventListenerRegistry->registerEvents( $eventDispatcher );
}

#[\Override]
public function match( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint );
}

#[\Override]
public function get( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_GET );
}

#[\Override]
public function post( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_POST );
}

#[\Override]
public function put( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_PUT );
}

#[\Override]
public function delete( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_DELETE );
}

#[\Override]
public function options( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_OPTIONS );
}

#[\Override]
public function patch( string $pattern, string $endpoint ): Route
{
return $this->routeRegistry->addRoute( $pattern, $endpoint, Request::METHOD_PATCH );
}

#[\Override]
public function before( $callback, int $priority = 0 ): void
{
$this->eventListenerRegistry->addEvent(
Expand All @@ -274,6 +239,7 @@ public function before( $callback, int $priority = 0 ): void
);
}

#[\Override]
public function after( $callback, int $priority = 0 ): void
{
$this->eventListenerRegistry->addEvent(
Expand All @@ -285,6 +251,7 @@ public function after( $callback, int $priority = 0 ): void
);
}

#[\Override]
public function finish( $callback, int $priority = 0 ): void
{
$this->eventListenerRegistry->addEvent(
Expand All @@ -296,6 +263,7 @@ public function finish( $callback, int $priority = 0 ): void
);
}

#[\Override]
public function error( $callback, int $priority = -8 ): void
{
$this->eventListenerRegistry->addEvent(
Expand All @@ -307,13 +275,7 @@ public function error( $callback, int $priority = -8 ): void
);
}

/**
* Handles the request and delivers the response.
*
* @param Request|null $request
* @throws Exception
*/
public function run( Request $request = null ): void
public function run( ?Request $request = null ): void
{
if ( !$request ) {
$request = Request::createFromGlobals();
Expand Down
4 changes: 4 additions & 0 deletions src/Endpoint.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

declare(strict_types=1);

namespace Clearbooks\Dilex;

use Symfony\Component\HttpFoundation\Request;

interface Endpoint
Expand Down
Loading