Skip to content

Commit

Permalink
more refined templates for any internal that has ContainerInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
limarkxx committed May 11, 2024
1 parent ed5a022 commit 542c05f
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 4 deletions.
7 changes: 7 additions & 0 deletions Slim/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@
use function preg_match;
use function sprintf;

/**
* @template TContainerInterface of (ContainerInterface|null)
*/
final class CallableResolver implements AdvancedCallableResolverInterface
{
public static string $callablePattern = '!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';

/** @var TContainerInterface $container */
private ?ContainerInterface $container;

/**
* @param TContainerInterface $container
*/
public function __construct(?ContainerInterface $container = null)
{
$this->container = $container;
Expand Down
2 changes: 1 addition & 1 deletion Slim/Factory/AppFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class AppFactory
/**
* @template TContainerInterface of (ContainerInterface|null)
* @param TContainerInterface $container
* @return (App<TContainerInterface>|App<ContainerInterface|null>)
* @return (TContainerInterface is ContainerInterface ? App<TContainerInterface> : App<ContainerInterface|null>)
*/
public static function create(
?ResponseFactoryInterface $responseFactory = null,
Expand Down
1 change: 1 addition & 0 deletions Slim/Interfaces/RouteGroupInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function addMiddleware(MiddlewareInterface $middleware): RouteGroupInterf

/**
* Append the group's middleware to the MiddlewareDispatcher
* @param MiddlewareDispatcher<\Psr\Container\ContainerInterface|null> $dispatcher
*/
public function appendMiddlewareToDispatcher(MiddlewareDispatcher $dispatcher): RouteGroupInterface;

Expand Down
9 changes: 9 additions & 0 deletions Slim/MiddlewareDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
use function preg_match;
use function sprintf;

/**
* @template TContainerInterface of (ContainerInterface|null)
*/
class MiddlewareDispatcher implements MiddlewareDispatcherInterface
{
/**
Expand All @@ -37,8 +40,12 @@ class MiddlewareDispatcher implements MiddlewareDispatcherInterface

protected ?CallableResolverInterface $callableResolver;

/** @var TContainerInterface $container */
protected ?ContainerInterface $container;

/**
* @param TContainerInterface $container
*/
public function __construct(
RequestHandlerInterface $kernel,
?CallableResolverInterface $callableResolver = null,
Expand Down Expand Up @@ -131,6 +138,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
* Middleware are organized as a stack. That means middleware
* that have been added before will be executed after the newly
* added one (last in, first out).
* @return MiddlewareDispatcher<TContainerInterface>
*/
public function addDeferred(string $middleware): self
{
Expand Down Expand Up @@ -238,6 +246,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
* Middleware are organized as a stack. That means middleware
* that have been added before will be executed after the newly
* added one (last in, first out).
* @return MiddlewareDispatcher<TContainerInterface>
*/
public function addCallable(callable $middleware): self
{
Expand Down
7 changes: 6 additions & 1 deletion Slim/Routing/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
use function in_array;
use function is_array;

/**
* @template TContainerInterface of (ContainerInterface|null)
*/
class Route implements RouteInterface, RequestHandlerInterface
{
/**
Expand Down Expand Up @@ -77,9 +80,11 @@ class Route implements RouteInterface, RequestHandlerInterface

/**
* Container
* @var TContainerInterface $container
*/
protected ?ContainerInterface $container = null;

/** @var MiddlewareDispatcher<TContainerInterface> $middlewareDispatcher */
protected MiddlewareDispatcher $middlewareDispatcher;

/**
Expand All @@ -106,7 +111,7 @@ class Route implements RouteInterface, RequestHandlerInterface
* @param callable|string $callable The route callable
* @param ResponseFactoryInterface $responseFactory
* @param CallableResolverInterface $callableResolver
* @param ContainerInterface|null $container
* @param TContainerInterface $container
* @param InvocationStrategyInterface|null $invocationStrategy
* @param RouteGroupInterface[] $groups The parent route groups
* @param int $identifier The route identifier
Expand Down
7 changes: 6 additions & 1 deletion Slim/Routing/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
/**
* RouteCollector is used to collect routes and route groups
* as well as generate paths and URLs relative to its environment
* @template TContainerInterface of (ContainerInterface|null)
*/
class RouteCollector implements RouteCollectorInterface
{
Expand Down Expand Up @@ -81,6 +82,9 @@ class RouteCollector implements RouteCollectorInterface

protected ResponseFactoryInterface $responseFactory;

/**
* @param TContainerInterface $container
*/
public function __construct(
ResponseFactoryInterface $responseFactory,
CallableResolverInterface $callableResolver,
Expand Down Expand Up @@ -245,10 +249,11 @@ protected function createGroup(string $pattern, $callable): RouteGroupInterface
}

/**
* @return RouteCollectorProxyInterface<ContainerInterface|null>
* @return RouteCollectorProxyInterface<TContainerInterface>
*/
protected function createProxy(string $pattern): RouteCollectorProxyInterface
{
/** @var RouteCollectorProxyInterface<TContainerInterface> */
return new RouteCollectorProxy(
$this->responseFactory,
$this->callableResolver,
Expand Down
1 change: 1 addition & 0 deletions Slim/Routing/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public function addMiddleware(MiddlewareInterface $middleware): RouteGroupInterf

/**
* {@inheritdoc}
* @param MiddlewareDispatcher<\Psr\Container\ContainerInterface|null> $dispatcher
*/
public function appendMiddlewareToDispatcher(MiddlewareDispatcher $dispatcher): RouteGroupInterface
{
Expand Down
2 changes: 1 addition & 1 deletion Slim/Routing/RouteRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
);
}

/** @var Route $route */
/** @var Route<\Psr\Container\ContainerInterface|null> $route */
$route = $request->getAttribute(RouteContext::ROUTE);
return $route->run($request);
}
Expand Down

0 comments on commit 542c05f

Please sign in to comment.