add data from work folder
This commit is contained in:
parent
97579d6d91
commit
48c9c9467d
218 changed files with 16123 additions and 1233 deletions
0
implementation/13-refactoring/src/.gitkeep
Normal file
0
implementation/13-refactoring/src/.gitkeep
Normal file
31
implementation/13-refactoring/src/Action/Hello.php
Normal file
31
implementation/13-refactoring/src/Action/Hello.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Lubian\NoFramework\Service\Time\Now;
|
||||
use Lubian\NoFramework\Template\Renderer;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
final class Hello
|
||||
{
|
||||
public function __invoke(
|
||||
ResponseInterface $response,
|
||||
Now $now,
|
||||
Renderer $renderer,
|
||||
string $name = 'Stranger',
|
||||
): ResponseInterface {
|
||||
$body = $response->getBody();
|
||||
$data = [
|
||||
'now' => $now()->format('H:i:s'),
|
||||
'name' => $name,
|
||||
];
|
||||
|
||||
$content = $renderer->render('hello', $data);
|
||||
|
||||
$body->write($content);
|
||||
|
||||
return $response
|
||||
->withStatus(200)
|
||||
->withBody($body);
|
||||
}
|
||||
}
|
19
implementation/13-refactoring/src/Action/Other.php
Normal file
19
implementation/13-refactoring/src/Action/Other.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
final class Other
|
||||
{
|
||||
public function someFunctionName(ResponseInterface $response): ResponseInterface
|
||||
{
|
||||
$body = $response->getBody();
|
||||
|
||||
$body->write('This works too!');
|
||||
|
||||
return $response
|
||||
->withStatus(200)
|
||||
->withBody($body);
|
||||
}
|
||||
}
|
40
implementation/13-refactoring/src/Bootstrap.php
Normal file
40
implementation/13-refactoring/src/Bootstrap.php
Normal file
|
@ -0,0 +1,40 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework;
|
||||
|
||||
use Lubian\NoFramework\Factory\FileSystemSettingsProvider;
|
||||
use Lubian\NoFramework\Factory\SettingsContainerProvider;
|
||||
use Throwable;
|
||||
use Whoops\Handler\PrettyPageHandler;
|
||||
use Whoops\Run;
|
||||
|
||||
use function assert;
|
||||
use function error_log;
|
||||
use function error_reporting;
|
||||
|
||||
use const E_ALL;
|
||||
|
||||
require __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
error_reporting(E_ALL);
|
||||
|
||||
$settingsProvider = new FileSystemSettingsProvider(__DIR__ . '/../config/settings.php');
|
||||
$container = (new SettingsContainerProvider($settingsProvider))->getContainer();
|
||||
|
||||
$settings = $settingsProvider->getSettings();
|
||||
|
||||
$whoops = new Run;
|
||||
if ($settings->environment === 'dev') {
|
||||
$whoops->pushHandler(new PrettyPageHandler);
|
||||
} else {
|
||||
$whoops->pushHandler(function (Throwable $e): void {
|
||||
error_log('Error: ' . $e->getMessage(), $e->getCode());
|
||||
echo 'An Error happened';
|
||||
});
|
||||
}
|
||||
$whoops->register();
|
||||
|
||||
$app = $container->get(Kernel::class);
|
||||
assert($app instanceof Kernel);
|
||||
|
||||
$app->run();
|
|
@ -0,0 +1,9 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class InternalServerError extends Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class MethodNotAllowed extends Exception
|
||||
{
|
||||
}
|
9
implementation/13-refactoring/src/Exception/NotFound.php
Normal file
9
implementation/13-refactoring/src/Exception/NotFound.php
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class NotFound extends Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
interface ContainerProvider
|
||||
{
|
||||
public function getContainer(): ContainerInterface;
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
final class DiactorosRequestFactory implements RequestFactory
|
||||
{
|
||||
public function __construct(private readonly ServerRequestFactory $factory)
|
||||
{
|
||||
}
|
||||
|
||||
public function fromGlobals(): ServerRequestInterface
|
||||
{
|
||||
return $this->factory::fromGlobals();
|
||||
}
|
||||
|
||||
public function createServerRequest(string $method, $uri, array $serverParams = []): ServerRequestInterface
|
||||
{
|
||||
return $this->factory->createServerRequest($method, $uri, $serverParams);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
use Lubian\NoFramework\Settings;
|
||||
|
||||
final class FileSystemSettingsProvider implements SettingsProvider
|
||||
{
|
||||
public function __construct(
|
||||
private string $filePath
|
||||
) {
|
||||
}
|
||||
|
||||
public function getSettings(): Settings
|
||||
{
|
||||
return require $this->filePath;
|
||||
}
|
||||
}
|
11
implementation/13-refactoring/src/Factory/RequestFactory.php
Normal file
11
implementation/13-refactoring/src/Factory/RequestFactory.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
use Psr\Http\Message\ServerRequestFactoryInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
interface RequestFactory extends ServerRequestFactoryInterface
|
||||
{
|
||||
public function fromGlobals(): ServerRequestInterface;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
use DI\ContainerBuilder;
|
||||
use Lubian\NoFramework\Settings;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
final class SettingsContainerProvider implements ContainerProvider
|
||||
{
|
||||
public function __construct(
|
||||
private SettingsProvider $settingsProvider,
|
||||
) {
|
||||
}
|
||||
|
||||
public function getContainer(): ContainerInterface
|
||||
{
|
||||
$builder = new ContainerBuilder;
|
||||
$settings = $this->settingsProvider->getSettings();
|
||||
$dependencies = require $settings->dependenciesFile;
|
||||
$dependencies[Settings::class] = fn () => $settings;
|
||||
$builder->addDefinitions($dependencies);
|
||||
return $builder->build();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Factory;
|
||||
|
||||
use Lubian\NoFramework\Settings;
|
||||
|
||||
interface SettingsProvider
|
||||
{
|
||||
public function getSettings(): Settings;
|
||||
}
|
38
implementation/13-refactoring/src/Http/BasicEmitter.php
Normal file
38
implementation/13-refactoring/src/Http/BasicEmitter.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
use function header;
|
||||
use function sprintf;
|
||||
use function strtolower;
|
||||
|
||||
final class BasicEmitter implements Emitter
|
||||
{
|
||||
public function emit(ResponseInterface $response, bool $withoutBody = false): void
|
||||
{
|
||||
foreach ($response->getHeaders() as $name => $values) {
|
||||
$first = strtolower($name) !== 'set-cookie';
|
||||
foreach ($values as $value) {
|
||||
$header = sprintf('%s: %s', $name, $value);
|
||||
header($header, $first);
|
||||
$first = false;
|
||||
}
|
||||
}
|
||||
|
||||
$statusLine = sprintf(
|
||||
'HTTP/%s %s %s',
|
||||
$response->getProtocolVersion(),
|
||||
$response->getStatusCode(),
|
||||
$response->getReasonPhrase()
|
||||
);
|
||||
header($statusLine, true, $response->getStatusCode());
|
||||
|
||||
if ($withoutBody) {
|
||||
return;
|
||||
}
|
||||
|
||||
echo $response->getBody();
|
||||
}
|
||||
}
|
10
implementation/13-refactoring/src/Http/Emitter.php
Normal file
10
implementation/13-refactoring/src/Http/Emitter.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
interface Emitter
|
||||
{
|
||||
public function emit(ResponseInterface $response, bool $withoutBody = false): void;
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
use Invoker\InvokerInterface;
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
use function assert;
|
||||
|
||||
final class InvokerRoutedHandler implements RoutedRequestHandler
|
||||
{
|
||||
public function __construct(
|
||||
private readonly InvokerInterface $invoker,
|
||||
private string $routeAttributeName = '__route_handler',
|
||||
) {
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$handler = $request->getAttribute($this->routeAttributeName, false);
|
||||
assert($handler !== false);
|
||||
$vars = $request->getAttributes();
|
||||
$vars['request'] = $request;
|
||||
$response = $this->invoker->call($handler, $vars);
|
||||
if (! $response instanceof ResponseInterface) {
|
||||
throw new InternalServerError('Handler returned invalid response');
|
||||
}
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function setRouteAttributeName(string $routeAttributeName = '__route_handler'): void
|
||||
{
|
||||
$this->routeAttributeName = $routeAttributeName;
|
||||
}
|
||||
}
|
69
implementation/13-refactoring/src/Http/RouteMiddleware.php
Normal file
69
implementation/13-refactoring/src/Http/RouteMiddleware.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
use FastRoute\Dispatcher;
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Lubian\NoFramework\Exception\MethodNotAllowed;
|
||||
use Lubian\NoFramework\Exception\NotFound;
|
||||
use Psr\Http\Message\ResponseFactoryInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Throwable;
|
||||
|
||||
final class RouteMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Dispatcher $dispatcher,
|
||||
private readonly ResponseFactoryInterface $responseFactory,
|
||||
private readonly string $routeAttributeName = '__route_handler',
|
||||
) {
|
||||
}
|
||||
|
||||
private function decorateRequest(
|
||||
ServerRequestInterface $request,
|
||||
): ServerRequestInterface {
|
||||
$routeInfo = $this->dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getUri()->getPath(),
|
||||
);
|
||||
|
||||
if ($routeInfo[0] === Dispatcher::METHOD_NOT_ALLOWED) {
|
||||
throw new MethodNotAllowed;
|
||||
}
|
||||
|
||||
if ($routeInfo[0] === Dispatcher::FOUND) {
|
||||
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
|
||||
$request = $request->withAttribute($attributeName, $attributeValue);
|
||||
}
|
||||
return $request->withAttribute(
|
||||
$this->routeAttributeName,
|
||||
$routeInfo[1]
|
||||
);
|
||||
}
|
||||
|
||||
throw new NotFound;
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
try {
|
||||
$request = $this->decorateRequest($request);
|
||||
} catch (NotFound) {
|
||||
$response = $this->responseFactory->createResponse(404);
|
||||
$response->getBody()->write('Not Found');
|
||||
return $response;
|
||||
} catch (MethodNotAllowed) {
|
||||
return $this->responseFactory->createResponse(405);
|
||||
} catch (Throwable $t) {
|
||||
throw new InternalServerError($t->getMessage(), $t->getCode(), $t);
|
||||
}
|
||||
|
||||
if ($handler instanceof RoutedRequestHandler) {
|
||||
$handler->setRouteAttributeName($this->routeAttributeName);
|
||||
}
|
||||
return $handler->handle($request);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Http;
|
||||
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
interface RoutedRequestHandler extends RequestHandlerInterface
|
||||
{
|
||||
public function setRouteAttributeName(string $routeAttributeName = '__route_handler'): void;
|
||||
}
|
34
implementation/13-refactoring/src/Kernel.php
Normal file
34
implementation/13-refactoring/src/Kernel.php
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework;
|
||||
|
||||
use Lubian\NoFramework\Factory\RequestFactory;
|
||||
use Lubian\NoFramework\Http\Emitter;
|
||||
use Lubian\NoFramework\Http\RoutedRequestHandler;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
final class Kernel implements RequestHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RequestFactory $requestFactory,
|
||||
private readonly MiddlewareInterface $routeMiddleware,
|
||||
private readonly RoutedRequestHandler $handler,
|
||||
private readonly Emitter $emitter,
|
||||
) {
|
||||
}
|
||||
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
return $this->routeMiddleware->process($request, $this->handler);
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$request = $this->requestFactory->fromGlobals();
|
||||
$response = $this->handle($request);
|
||||
$this->emitter->emit($response);
|
||||
}
|
||||
}
|
10
implementation/13-refactoring/src/Service/Time/Now.php
Normal file
10
implementation/13-refactoring/src/Service/Time/Now.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Service\Time;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface Now
|
||||
{
|
||||
public function __invoke(): DateTimeImmutable;
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Service\Time;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
final class SystemClockNow implements Now
|
||||
{
|
||||
public function __invoke(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable;
|
||||
}
|
||||
}
|
14
implementation/13-refactoring/src/Settings.php
Normal file
14
implementation/13-refactoring/src/Settings.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework;
|
||||
|
||||
final class Settings
|
||||
{
|
||||
public function __construct(
|
||||
public readonly string $environment,
|
||||
public readonly string $dependenciesFile,
|
||||
public readonly string $templateDir,
|
||||
public readonly string $templateExtension,
|
||||
) {
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Template;
|
||||
|
||||
use Mustache_Engine;
|
||||
|
||||
final class MustacheRenderer implements Renderer
|
||||
{
|
||||
public function __construct(private Mustache_Engine $engine)
|
||||
{
|
||||
}
|
||||
|
||||
public function render(string $template, array $data = []): string
|
||||
{
|
||||
return $this->engine->render($template, $data);
|
||||
}
|
||||
}
|
11
implementation/13-refactoring/src/Template/Renderer.php
Normal file
11
implementation/13-refactoring/src/Template/Renderer.php
Normal file
|
@ -0,0 +1,11 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Template;
|
||||
|
||||
interface Renderer
|
||||
{
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function render(string $template, array $data = []): string;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue