add data from work folder

This commit is contained in:
lubiana 2022-03-29 20:35:06 +02:00 committed by Andre Lubian
parent 97579d6d91
commit 48c9c9467d
218 changed files with 16123 additions and 1233 deletions

View file

@ -0,0 +1,47 @@
<?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,
'linebreak_after_opening_tag' => true,
'native_constant_invocation' => true,
'native_function_invocation' => [
'include' => ['@all']
],
'no_unused_imports' => true,
'global_namespace_import' => [
'import_classes' => true,
'import_constants' => true,
'import_functions' => true,
],
'ordered_interfaces' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
'imports_order' => [
'const',
'class',
'function',
],
],
])
->setFinder(
PhpCsFixer\Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/config',
])
);

View file

@ -0,0 +1,32 @@
{
"name": "lubian/no-framework",
"authors": [
{
"name": "lubian",
"email": "test@example.com"
}
],
"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"
},
"autoload": {
"psr-4": {
"Lubian\\NoFramework\\": "src/"
}
},
"require-dev": {
"phpstan/phpstan": "^1.4",
"php-cs-fixer/shim": "^3.8",
"symfony/var-dumper": "^6.0"
},
"scripts": {
"serve": "php -S 0.0.0.0:1234 -t public",
"phpstan": "./vendor/bin/phpstan analyze",
"style": "./vendor/bin/php-cs-fixer fix"
}
}

1020
implementation/10-invoker/composer.lock generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
$builder = new \DI\ContainerBuilder();
$builder->addDefinitions([
\Psr\Http\Message\ResponseInterface::class => fn () => new \Laminas\Diactoros\Response(),
\Psr\Http\Message\ServerRequestInterface::class => fn () => \Laminas\Diactoros\ServerRequestFactory::fromGlobals(),
\Lubian\NoFramework\Service\Time\Now::class => fn () => new \Lubian\NoFramework\Service\Time\SystemClockNow(),
]);
return $builder->build();

View file

@ -0,0 +1,18 @@
<?php
declare(strict_types=1);
use FastRoute\RouteCollector;
use Laminas\Diactoros\Response;
use Lubian\NoFramework\Action\Hello;
use Lubian\NoFramework\Action\Other;
return function (RouteCollector $r): void {
$r->addRoute('GET', '/hello[/{name}]', Hello::class);
$r->addRoute('GET', '/other-route', [Other::class, 'someFunctionName']);
$r->addRoute(
'GET',
'/',
fn (Response $r) => $r->withStatus(302)->withHeader('Location', '/hello')
);
};

View file

@ -0,0 +1,5 @@
parameters:
level: 9
paths:
- src
- config

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

View file

@ -0,0 +1,6 @@
<?php
declare(strict_types=1);
require __DIR__ . '/../src/Bootstrap.php';

View file

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Action;
use Lubian\NoFramework\Service\Time\Now;
use Psr\Http\Message\ResponseInterface;
final class Hello
{
public function __invoke(
ResponseInterface $response,
Now $now,
string $name = 'Stranger',
): ResponseInterface {
$response = $response->withStatus(200);
$response->getBody()->write('Hello ' . $name . '!');
$nowString = $now->get()->format('H:i:s');
$response->getBody()->write(' The Time is ' . $nowString);
return $response;
}
}

View file

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Action;
use Psr\Http\Message\ResponseInterface;
final class Other
{
public function __construct(private ResponseInterface $response)
{
}
public function someFunctionName(): ResponseInterface
{
$response = $this->response->withStatus(200);
$response->getBody()->write('This works too');
return $response;
}
}

View file

@ -0,0 +1,110 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework;
use const E_ALL;
use Exception;
use FastRoute\Dispatcher;
use Invoker\InvokerInterface;
use Lubian\NoFramework\Exception\InternalServerError;
use Lubian\NoFramework\Exception\MethodNotAllowed;
use Lubian\NoFramework\Exception\NotFound;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Throwable;
use Whoops\Handler\PrettyPageHandler;
use Whoops\Run;
use function error_log;
use function error_reporting;
use function FastRoute\simpleDispatcher;
use function getenv;
use function header;
use function sprintf;
use function strtolower;
require __DIR__ . '/../vendor/autoload.php';
$environment = getenv('ENVIRONMENT') ?: 'dev';
error_reporting(E_ALL);
$whoops = new Run();
if ($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();
/** @var ContainerInterface $container */
$container = require __DIR__ . '/../config/dependencies.php';
/** @var InvokerInterface $invoker */
$invoker = $container->get(InvokerInterface::class);
/** @var ServerRequestInterface $request */
$request = $container->get(ServerRequestInterface::class);
/** @var ResponseInterface $response */
$response = $container->get(ResponseInterface::class);
$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];
$args = $routeInfo[2];
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
$request = $request->withAttribute($attributeName, $attributeValue);
}
$args['request'] = $request;
$response = $container->call($handler, $args);
break;
case Dispatcher::METHOD_NOT_ALLOWED:
throw new MethodNotAllowed();
case Dispatcher::NOT_FOUND:
default:
throw new NotFound();
}
} catch (NotFound) {
$response = $response->withStatus(404);
$response->getBody()->write('Not Found');
} catch (MethodNotAllowed) {
$response = $response->withStatus(405);
$response->getBody()->write('Method not Allowed');
} catch (Exception $e) {
throw new InternalServerError($e->getMessage(), $e->getCode(), $e);
}
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();

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Exception;
use Exception;
final class InternalServerError extends Exception
{
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Exception;
use Exception;
final class MethodNotAllowed extends Exception
{
}

View file

@ -0,0 +1,11 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Exception;
use Exception;
final class NotFound extends Exception
{
}

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Service\Time;
use DateTimeImmutable;
interface Now
{
public function get(): DateTimeImmutable;
}

View file

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
namespace Lubian\NoFramework\Service\Time;
use DateTimeImmutable;
final class SystemClockNow implements Now
{
public function get(): DateTimeImmutable
{
return new DateTimeImmutable();
}
}