improve ::registerContainerHandler docblock

pull/548/head
fadrian06 2 weeks ago
parent 8ccbd7ac9e
commit 59c9c0330f

@ -16,6 +16,7 @@ use flight\net\Router;
use flight\template\View;
use Throwable;
use flight\net\Route;
use Psr\Container\ContainerInterface;
/**
* The Engine class contains the core functionality of the framework.
@ -265,9 +266,9 @@ class Engine
/**
* Registers the container handler
*
* @param callable|object $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
* @template T of object
*
* @return void
* @param ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler Callback function or PSR-11 Container object that sets the container and how it will inject classes
*/
public function registerContainerHandler($containerHandler): void
{

@ -9,6 +9,7 @@ use flight\net\Router;
use flight\template\View;
use flight\net\Route;
use flight\core\EventDispatcher;
use Psr\Container\ContainerInterface;
require_once __DIR__ . '/autoload.php';
@ -18,6 +19,8 @@ require_once __DIR__ . '/autoload.php';
* @license MIT, http://flightphp.com/license
* @copyright Copyright (c) 2011, Mike Cao <mike@mikecao.com>
*
* @template T of object
*
* # Core methods
* @method static void start() Starts the framework.
* @method static void path(string $dir) Adds a path for autoloading classes.
@ -28,7 +31,7 @@ require_once __DIR__ . '/autoload.php';
* Registers a class to a framework method.
* @method static void unregister(string $methodName)
* Unregisters a class to a framework method.
* @method static void registerContainerHandler(callable|object $containerHandler) Registers a container handler.
* @method static void registerContainerHandler(ContainerInterface|callable(class-string<T> $id, array<int|string, mixed> $params): ?T $containerHandler) Registers a container handler.
*
* # Class registration
* @method EventDispatcher eventDispatcher() Gets event dispatcher

@ -106,7 +106,7 @@ class RouteCommand extends AbstractBaseCommand
if ($showAll === true) {
$boolval = true;
} else {
$methods = [ 'GET', 'POST', 'PUT', 'DELETE', 'PATCH' ];
$methods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
foreach ($methods as $method) {
$lowercaseMethod = strtolower($method);
if (

@ -52,7 +52,9 @@ class Dispatcher
/**
* Sets the dependency injection container handler.
*
* @param ContainerInterface|(callable(string $classString, array<int, mixed> $params): (null|object)) $containerHandler
* @template T of object
*
* @param ContainerInterface|(callable(class-string<T> $classString, array<int, mixed> $params): ?T) $containerHandler
* Dependency injection container.
*
* @throws InvalidArgumentException If $containerHandler is not a `callable` or instance of `Psr\Container\ContainerInterface`.

@ -211,8 +211,8 @@ class Request
$this->data->setData($data);
}
}
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
} elseif (in_array($this->method, [ 'PUT', 'DELETE', 'PATCH' ], true) === true) {
// Check PUT, PATCH, DELETE for application/x-www-form-urlencoded data
} elseif (in_array($this->method, ['PUT', 'DELETE', 'PATCH'], true) === true) {
$body = $this->getBody();
if ($body !== '') {
$data = [];

@ -5,4 +5,5 @@ declare(strict_types=1);
// This file is only here so that the PHP8 attribute for doesn't throw an error in files
class ReturnTypeWillChange
{
//
}

@ -4,7 +4,6 @@ declare(strict_types=1);
namespace tests;
use ErrorException;
use Exception;
use flight\database\PdoWrapper;
use flight\Engine;
@ -13,7 +12,6 @@ use flight\net\Response;
use flight\util\Collection;
use InvalidArgumentException;
use JsonException;
use PDOException;
use PHPUnit\Framework\TestCase;
use tests\classes\Container;
use tests\classes\ContainerDefault;

@ -1,5 +1,8 @@
<?php
declare(strict_types=1);
class ExampleClass
{
//
}

@ -1,28 +1,27 @@
<?php
declare(strict_types=1);
namespace Tests\PHP8;
use DateTimeImmutable;
use ExampleClass;
use Flight;
use flight\Container;
use flight\Engine;
use flight\net\Route;
use PHPUnit\Framework\TestCase;
use stdClass;
class FlightTest extends TestCase
final class FlightTest extends TestCase
{
private Engine $engine;
protected function setUp(): void
{
$_SERVER = [];
$_REQUEST = [];
$this->engine = new Engine;
Flight::init();
Flight::setEngine(new Engine());
}
protected function tearDown(): void
{
unset($_REQUEST);
unset($_SERVER);
Flight::clear();
Flight::setEngine($this->engine);
}
//////////////////
@ -30,7 +29,7 @@ class FlightTest extends TestCase
//////////////////
public function test_path(): void
{
Flight::path(path: __DIR__);
Flight::path(dir: __DIR__);
$exampleObject = new ExampleClass();
self::assertInstanceOf(ExampleClass::class, $exampleObject);
@ -46,12 +45,66 @@ class FlightTest extends TestCase
public function test_halt(): void
{
Flight::halt(500, actuallyExit: false, message: 'Test');
Flight::halt(actuallyExit: false, code: 500, message: 'Test');
self::expectOutputString('Test');
self::assertSame(500, Flight::response()->status());
}
public function test_register(): void
{
Flight::register(
class: stdClass::class,
name: 'customClass',
callback: static function (stdClass $object): void {
$object->property = 'value';
},
params: []
);
$object = Flight::customClass();
self::assertInstanceOf(stdClass::class, $object);
self::assertObjectHasProperty('property', $object);
self::assertSame('value', $object->property);
Flight::unregister(methodName: 'customClass');
}
public function test_register_container(): void
{
$dateTime = new DateTimeImmutable();
$controller = new class($dateTime) {
public function __construct(private DateTimeImmutable $dateTime)
{
//
}
public function test(): void
{
echo $this->dateTime->format('Y-m-d');
}
};
Flight::registerContainerHandler(
containerHandler: new Container()
);
Flight::request()->url = '/test';
Flight::route(
pass_route: true,
alias: 'testRoute',
callback: [$controller::class, 'test'],
pattern: '/test'
);
self::expectOutputString($dateTime->format('Y-m-d'));
Flight::start();
}
/////////////////////
// ROUTING METHODS //
/////////////////////

Loading…
Cancel
Save