add data from work folder
This commit is contained in:
parent
97579d6d91
commit
48c9c9467d
218 changed files with 16123 additions and 1233 deletions
38
implementation/12-configuration/.php-cs-fixer.php
Normal file
38
implementation/12-configuration/.php-cs-fixer.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php declare(strict_types=1);
|
||||
/*
|
||||
* This document has been generated with
|
||||
* https://mlocati.github.io/php-cs-fixer-configurator/#version:3.1.0|configurator
|
||||
* you can change this configuration by importing this file.
|
||||
*/
|
||||
$config = new PhpCsFixer\Config();
|
||||
return $config
|
||||
->setRiskyAllowed(true)
|
||||
->setRules([
|
||||
'@PSR12:risky' => true,
|
||||
'@PSR12' => true,
|
||||
'@PHP80Migration' => true,
|
||||
'@PHP80Migration:risky' => true,
|
||||
'@PHP81Migration' => true,
|
||||
'array_indentation' => true,
|
||||
'include' => true,
|
||||
'blank_line_after_opening_tag' => false,
|
||||
'native_constant_invocation' => true,
|
||||
'new_with_braces' => false,
|
||||
'native_function_invocation' => [
|
||||
'include' => ['@all']
|
||||
],
|
||||
'no_unused_imports' => true,
|
||||
'global_namespace_import' => [
|
||||
'import_classes' => true,
|
||||
'import_constants' => true,
|
||||
'import_functions' => true,
|
||||
],
|
||||
'ordered_interfaces' => true,
|
||||
])
|
||||
->setFinder(
|
||||
PhpCsFixer\Finder::create()
|
||||
->in([
|
||||
__DIR__ . '/src',
|
||||
__DIR__ . '/config'
|
||||
])
|
||||
);
|
9
implementation/12-configuration/.phpcs.xml.dist
Normal file
9
implementation/12-configuration/.phpcs.xml.dist
Normal file
|
@ -0,0 +1,9 @@
|
|||
<?xml version="1.0"?>
|
||||
<ruleset>
|
||||
<arg name="basepath" value="."/>
|
||||
|
||||
<file>src</file>
|
||||
<file>config</file>
|
||||
|
||||
<rule ref="HardMode"/>
|
||||
</ruleset>
|
46
implementation/12-configuration/composer.json
Normal file
46
implementation/12-configuration/composer.json
Normal file
|
@ -0,0 +1,46 @@
|
|||
{
|
||||
"name": "lubian/no-framework",
|
||||
"require": {
|
||||
"php": "^8.1",
|
||||
"filp/whoops": "^2.14",
|
||||
"laminas/laminas-diactoros": "^2.8",
|
||||
"nikic/fast-route": "^1.3",
|
||||
"psr/http-server-handler": "^1.0",
|
||||
"php-di/php-di": "^6.3",
|
||||
"mustache/mustache": "^2.14"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lubian\\NoFramework\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "lubian",
|
||||
"email": "test@example.com"
|
||||
}
|
||||
],
|
||||
"require-dev": {
|
||||
"phpstan/phpstan": "^1.5",
|
||||
"php-cs-fixer/shim": "^3.8",
|
||||
"symfony/var-dumper": "^6.0",
|
||||
"squizlabs/php_codesniffer": "^3.6",
|
||||
"phpstan/extension-installer": "^1.1",
|
||||
"phpstan/phpstan-strict-rules": "^1.1",
|
||||
"thecodingmachine/phpstan-strict-rules": "^1.0",
|
||||
"mnapoli/hard-mode": "^0.3.0"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"phpstan/extension-installer": true,
|
||||
"dealerdirect/phpcodesniffer-composer-installer": true
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"serve": "php -S 0.0.0.0:1234 -t public",
|
||||
"phpstan": "./vendor/bin/phpstan analyze",
|
||||
"baseline": "./vendor/bin/phpstan analyze --generate-baseline",
|
||||
"check": "./vendor/bin/phpcs",
|
||||
"fix": "./vendor/bin/php-cs-fixer fix && ./vendor/bin/phpcbf"
|
||||
}
|
||||
}
|
1550
implementation/12-configuration/composer.lock
generated
Normal file
1550
implementation/12-configuration/composer.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
22
implementation/12-configuration/config/dependencies.php
Normal file
22
implementation/12-configuration/config/dependencies.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use Laminas\Diactoros\ResponseFactory;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Lubian\NoFramework\Service\Time\Now;
|
||||
use Lubian\NoFramework\Service\Time\SystemClockNow;
|
||||
use Lubian\NoFramework\Settings;
|
||||
use Lubian\NoFramework\Template\MustacheRenderer;
|
||||
use Lubian\NoFramework\Template\Renderer;
|
||||
use Mustache_Engine as ME;
|
||||
use Mustache_Loader_FilesystemLoader as MLF;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
return [
|
||||
ResponseInterface::class => fn (ResponseFactory $rf) => $rf->createResponse(),
|
||||
ServerRequestInterface::class => fn (ServerRequestFactory $rf) => $rf::fromGlobals(),
|
||||
Now::class => fn (SystemClockNow $n) => $n,
|
||||
Renderer::class => fn (Mustache_Engine $e) => new MustacheRenderer($e),
|
||||
MLF::class => fn (Settings $s) => new MLF($s->templateDir, ['extension' => $s->templateExtension]),
|
||||
ME::class => fn (MLF $mfl) => new ME(['loader' => $mfl]),
|
||||
];
|
12
implementation/12-configuration/config/routes.php
Normal file
12
implementation/12-configuration/config/routes.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use FastRoute\RouteCollector;
|
||||
use Lubian\NoFramework\Action\Hello;
|
||||
use Lubian\NoFramework\Action\Other;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
|
||||
return function (RouteCollector $r): void {
|
||||
$r->addRoute('GET', '/hello[/{name}]', Hello::class);
|
||||
$r->addRoute('GET', '/another-route', [Other::class, 'someFunctionName']);
|
||||
$r->addRoute('GET', '/', fn (Response $r) => $r->withStatus(302)->withHeader('Location', '/hello'));
|
||||
};
|
10
implementation/12-configuration/config/settings.php
Normal file
10
implementation/12-configuration/config/settings.php
Normal file
|
@ -0,0 +1,10 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
use Lubian\NoFramework\Settings;
|
||||
|
||||
return new Settings(
|
||||
environment: 'dev',
|
||||
dependenciesFile: __DIR__ . '/dependencies.php',
|
||||
templateDir: __DIR__ . '/../templates',
|
||||
templateExtension: '.html',
|
||||
);
|
0
implementation/12-configuration/phpstan-baseline.neon
Normal file
0
implementation/12-configuration/phpstan-baseline.neon
Normal file
8
implementation/12-configuration/phpstan.neon
Normal file
8
implementation/12-configuration/phpstan.neon
Normal file
|
@ -0,0 +1,8 @@
|
|||
includes:
|
||||
- phpstan-baseline.neon
|
||||
|
||||
parameters:
|
||||
level: max
|
||||
paths:
|
||||
- src
|
||||
- config
|
BIN
implementation/12-configuration/public/favicon.ico
Normal file
BIN
implementation/12-configuration/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
5
implementation/12-configuration/public/index.php
Normal file
5
implementation/12-configuration/public/index.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/../src/Bootstrap.php';
|
0
implementation/12-configuration/src/.gitkeep
Normal file
0
implementation/12-configuration/src/.gitkeep
Normal file
31
implementation/12-configuration/src/Action/Hello.php
Normal file
31
implementation/12-configuration/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/12-configuration/src/Action/Other.php
Normal file
19
implementation/12-configuration/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);
|
||||
}
|
||||
}
|
111
implementation/12-configuration/src/Bootstrap.php
Normal file
111
implementation/12-configuration/src/Bootstrap.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework;
|
||||
|
||||
use FastRoute\Dispatcher;
|
||||
use Invoker\InvokerInterface;
|
||||
use Laminas\Diactoros\ResponseFactory;
|
||||
use Lubian\NoFramework\Exception\InternalServerError;
|
||||
use Lubian\NoFramework\Exception\MethodNotAllowed;
|
||||
use Lubian\NoFramework\Exception\NotFound;
|
||||
use Lubian\NoFramework\Factory\FileSystemSettingsProvider;
|
||||
use Lubian\NoFramework\Factory\SettingsContainerProvider;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Throwable;
|
||||
use Whoops\Handler\PrettyPageHandler;
|
||||
use Whoops\Run;
|
||||
|
||||
use function assert;
|
||||
use function error_log;
|
||||
use function error_reporting;
|
||||
use function FastRoute\simpleDispatcher;
|
||||
use function header;
|
||||
use function sprintf;
|
||||
use function strtolower;
|
||||
|
||||
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();
|
||||
|
||||
|
||||
$request = $container->get(ServerRequestInterface::class);
|
||||
assert($request instanceof ServerRequestInterface);
|
||||
|
||||
$responseFactory = $container->get(ResponseFactory::class);
|
||||
assert($responseFactory instanceof ResponseFactory);
|
||||
|
||||
$routeDefinitionCallback = require __DIR__ . '/../config/routes.php';
|
||||
$dispatcher = simpleDispatcher($routeDefinitionCallback);
|
||||
|
||||
$routeInfo = $dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getUri()->getPath(),
|
||||
);
|
||||
|
||||
try {
|
||||
switch ($routeInfo[0]) {
|
||||
case Dispatcher::FOUND:
|
||||
$handler = $routeInfo[1];
|
||||
$vars = $routeInfo[2] ?? [];
|
||||
foreach ($vars as $attributeName => $attributeValue) {
|
||||
$request = $request->withAttribute($attributeName, $attributeValue);
|
||||
}
|
||||
$vars['request'] = $request;
|
||||
$invoker = $container->get(InvokerInterface::class);
|
||||
assert($invoker instanceof InvokerInterface);
|
||||
$response = $invoker->call($handler, $vars);
|
||||
assert($response instanceof ResponseInterface);
|
||||
break;
|
||||
case Dispatcher::METHOD_NOT_ALLOWED:
|
||||
throw new MethodNotAllowed;
|
||||
|
||||
case Dispatcher::NOT_FOUND:
|
||||
default:
|
||||
throw new NotFound;
|
||||
}
|
||||
} catch (MethodNotAllowed) {
|
||||
$response = $responseFactory->createResponse(405);
|
||||
} catch (NotFound) {
|
||||
$response = $responseFactory->createResponse(404);
|
||||
$response->getBody()->write('Not Found');
|
||||
} catch (Throwable $t) {
|
||||
throw new InternalServerError($t->getMessage(), $t->getCode(), $t);
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
echo $response->getBody();
|
|
@ -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
|
||||
{
|
||||
}
|
|
@ -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,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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
10
implementation/12-configuration/src/Service/Time/Now.php
Normal file
10
implementation/12-configuration/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/12-configuration/src/Settings.php
Normal file
14
implementation/12-configuration/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/12-configuration/src/Template/Renderer.php
Normal file
11
implementation/12-configuration/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;
|
||||
}
|
11
implementation/12-configuration/templates/hello.html
Normal file
11
implementation/12-configuration/templates/hello.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Hello World</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello {{name}}</h1>
|
||||
<p>The time is {{now}}</p>
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue