wip: rewrite di chapter
This commit is contained in:
parent
a4f171b98c
commit
988a532b78
18 changed files with 1667 additions and 0 deletions
37
app/config/container.php
Normal file
37
app/config/container.php
Normal file
|
@ -0,0 +1,37 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
return new class () implements \Psr\Container\ContainerInterface {
|
||||
|
||||
private readonly array $services;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->services = [
|
||||
\Psr\Http\Message\ServerRequestInterface::class => fn () => \Laminas\Diactoros\ServerRequestFactory::fromGlobals(),
|
||||
\Psr\Http\Message\ResponseInterface::class => fn () => new \Laminas\Diactoros\Response(),
|
||||
\FastRoute\Dispatcher::class => fn () => \FastRoute\simpleDispatcher(require __DIR__ . '/routes.php'),
|
||||
\Lubian\NoFramework\Service\Time\Clock::class => fn () => new \Lubian\NoFramework\Service\Time\SystemClock(),
|
||||
\Lubian\NoFramework\Action\Hello::class => fn () => new \Lubian\NoFramework\Action\Hello(
|
||||
$this->get(\Psr\Http\Message\ResponseInterface::class),
|
||||
$this->get(\Lubian\NoFramework\Service\Time\Clock::class)
|
||||
),
|
||||
\Lubian\NoFramework\Action\Other::class => fn () => new \Lubian\NoFramework\Action\Other(
|
||||
$this->get(\Psr\Http\Message\ResponseInterface::class)
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
public function get(string $id)
|
||||
{
|
||||
if (! $this->has($id)) {
|
||||
throw new class () extends \Exception implements \Psr\Container\NotFoundExceptionInterface {
|
||||
};
|
||||
}
|
||||
return $this->services[$id]();
|
||||
}
|
||||
|
||||
public function has(string $id): bool
|
||||
{
|
||||
return array_key_exists($id, $this->services);
|
||||
}
|
||||
};
|
10
app/config/routes.php
Normal file
10
app/config/routes.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use FastRoute\RouteCollector;
|
||||
use Lubian\NoFramework\Action\Hello;
|
||||
use Lubian\NoFramework\Action\Other;
|
||||
|
||||
return function (RouteCollector $r) {
|
||||
$r->addRoute('GET', '/hello[/{name}]', Hello::class);
|
||||
$r->addRoute('GET', '/other', Other::class);
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue