pine3ree / pine3ree-invokable-request-handler
Autowired invokable server-request handlers
0.6.0
2025-05-08 21:50 UTC
Requires
- php: ^7.4 || ^8.0
- pine3ree/pine3ree-params-resolver: ^0.8 || ^0.9
- psr/container: ^1.1.2 || ^2.0
- psr/http-server-handler: ^1.0 || ^2.0
Requires (Dev)
- laminas/laminas-diactoros: ^2.17 || ^3.0
- phpspec/prophecy-phpunit: ^1.1 || ^2.0
- phpstan/phpstan: ^1.12 || ^2.0
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.3
- squizlabs/php_codesniffer: ^3.5.7
- webimpress/coding-standard: ^1.3
This package is auto-updated.
Last update: 2025-05-08 21:52:16 UTC
README
This package provides a psr server-request handler that can be invoked directly.
Descendant classes or classes using the InvokableRequestHandlerTrait
must implement
the __invoke
method, whose dependencies are resolved either using the request
attributes or by the container, via the composed params-resolver. A type-hinted
ServerRequestInterface
dependency is resolved to the current request.
Example:
<?php namespace App\Controller\Shop\Product; use App\Model\Entity\Product; use App\Model\ORMInterface; use App\Session\SessionInterface; use App\Http\Message\Response\HtmlResponse; use App\Http\Message\Response\NotFoundResponse; use App\View\TemplateRendererInterface; use Psr\Http\Server\RequestHandlerInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use RuntimeException; use pine3ree\Http\Server\InvokableRequestHandler; /** * An invokable controller for route '/shop/product/{id}' */ class Read extends InvokableRequestHandler implements RequestHandlerInterface; { public function __invoke( ServerRequestInterface $request, ORMInterface $orm, TemplateRendererInterface $view, SessionInterface $session, // stored as request attribute under the SessionInterface:class key ?int $id = null // Route-match parameter stored as request attribute under the 'id' key ): ResponseInterface { $id = (int)$id; if (id < 1) { return new NotFoundResponse(); } $product = $orm->findById(Product::class, $id); if ($product === null) { return new NotFoundResponse("Product not found for id={$id}"); } $session->set('last_visited_product_id', $id); return new HtmlResponse( $view->render('shop/product/read.html.php', [ 'product' => $product, ]); ); } }