add data from work folder
This commit is contained in:
parent
97579d6d91
commit
48c9c9467d
218 changed files with 16123 additions and 1233 deletions
44
implementation/09-dependency-injector/.php-cs-fixer.php
Normal file
44
implementation/09-dependency-injector/.php-cs-fixer.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?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' => 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',
|
||||
])
|
||||
);
|
34
implementation/09-dependency-injector/composer.json
Normal file
34
implementation/09-dependency-injector/composer.json
Normal file
|
@ -0,0 +1,34 @@
|
|||
{
|
||||
"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"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Lubian\\NoFramework\\": "src/"
|
||||
}
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "lubian",
|
||||
"email": "test@example.com"
|
||||
}
|
||||
],
|
||||
"scripts": {
|
||||
"serve": "php -S localhost:1234 -t ./public",
|
||||
"prodserve": "ENVIRONMENT=prod php -S localhost:1234 -t ./public",
|
||||
"phpstan": "./vendor/bin/phpstan analyze",
|
||||
"baseline": "./vendor/bin/phpstan analyze --generate-baseline",
|
||||
"style": "./vendor/bin/php-cs-fixer fix"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpstan/phpstan": "^1.4",
|
||||
"php-cs-fixer/shim": "^3.7",
|
||||
"symfony/var-dumper": "^6.0"
|
||||
}
|
||||
}
|
1020
implementation/09-dependency-injector/composer.lock
generated
Normal file
1020
implementation/09-dependency-injector/composer.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
$builder = new \DI\ContainerBuilder();
|
||||
$builder->addDefinitions([
|
||||
\Psr\Http\Message\ResponseInterface::class => \DI\create(\Laminas\Diactoros\Response::class),
|
||||
\Psr\Http\Message\ServerRequestInterface::class => fn () => \Laminas\Diactoros\ServerRequestFactory::fromGlobals(),
|
||||
]);
|
||||
|
||||
return $builder->build();
|
8
implementation/09-dependency-injector/config/routes.php
Normal file
8
implementation/09-dependency-injector/config/routes.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
return function (\FastRoute\RouteCollector $r): void {
|
||||
$r->addRoute('GET', '/hello[/{name}]', [\Lubian\NoFramework\Action\Hello::class, 'handle']);
|
||||
$r->addRoute('GET', '/another-route', [\Lubian\NoFramework\Action\Another::class, 'handle']);
|
||||
};
|
|
@ -0,0 +1,7 @@
|
|||
parameters:
|
||||
ignoreErrors:
|
||||
-
|
||||
message: "#^Cannot call method handle\\(\\) on mixed\\.$#"
|
||||
count: 3
|
||||
path: src/Bootstrap.php
|
||||
|
7
implementation/09-dependency-injector/phpstan.neon
Normal file
7
implementation/09-dependency-injector/phpstan.neon
Normal file
|
@ -0,0 +1,7 @@
|
|||
includes:
|
||||
- phpstan-baseline.neon
|
||||
|
||||
parameters:
|
||||
level: 9
|
||||
paths:
|
||||
- src
|
5
implementation/09-dependency-injector/public/index.php
Normal file
5
implementation/09-dependency-injector/public/index.php
Normal file
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types = 1);
|
||||
|
||||
require __DIR__ . '/../src/Bootstrap.php';
|
18
implementation/09-dependency-injector/src/Action/Action.php
Normal file
18
implementation/09-dependency-injector/src/Action/Action.php
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
|
||||
abstract class Action implements \Psr\Http\Server\RequestHandlerInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected ResponseInterface $response
|
||||
) {
|
||||
}
|
||||
|
||||
abstract public function handle(ServerRequestInterface $request): ResponseInterface;
|
||||
}
|
19
implementation/09-dependency-injector/src/Action/Another.php
Normal file
19
implementation/09-dependency-injector/src/Action/Another.php
Normal file
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
final class Another extends Action
|
||||
{
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$this->response->getBody()->write('This works too!');
|
||||
return $this->response;
|
||||
}
|
||||
}
|
20
implementation/09-dependency-injector/src/Action/Hello.php
Normal file
20
implementation/09-dependency-injector/src/Action/Hello.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
final class Hello extends Action
|
||||
{
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$name = $request->getAttribute('name', 'Stranger');
|
||||
$this->response->getBody()->write('Hello ' . $name . '!');
|
||||
return $this->response;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
final class InternalServerError extends Action
|
||||
{
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$this->response->getBody()->write('Internal Server Error.');
|
||||
return $this->response->withStatus(500);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
final class NotAllowed extends Action
|
||||
{
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$this->response->getBody()->write('Method Not Allowed');
|
||||
return $this->response->withStatus(405);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Action;
|
||||
|
||||
use Laminas\Diactoros\Response;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
final class NotFound extends Action
|
||||
{
|
||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||
{
|
||||
$this->response->getBody()->write('Page not found');
|
||||
return $this->response->withStatus(404);
|
||||
}
|
||||
}
|
106
implementation/09-dependency-injector/src/Bootstrap.php
Normal file
106
implementation/09-dependency-injector/src/Bootstrap.php
Normal file
|
@ -0,0 +1,106 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework;
|
||||
|
||||
use const E_ALL;
|
||||
use Exception;
|
||||
use FastRoute\Dispatcher;
|
||||
use Laminas\Diactoros\Response;
|
||||
use Laminas\Diactoros\ServerRequestFactory;
|
||||
use Lubian\NoFramework\Action\InternalServerError;
|
||||
use Lubian\NoFramework\Action\NotAllowed;
|
||||
use Lubian\NoFramework\Action\NotFound;
|
||||
use Lubian\NoFramework\Exception\InternalServerErrorException;
|
||||
use Lubian\NoFramework\Exception\NotAllowedException;
|
||||
use Lubian\NoFramework\Exception\NotFoundException;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
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;
|
||||
|
||||
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 ServerRequestInterface $request */
|
||||
$request = $container->get(ServerRequestInterface::class);
|
||||
|
||||
$routeDefinitionCallback = require __DIR__ . '/../config/routes.php';
|
||||
$dispatcher = simpleDispatcher($routeDefinitionCallback);
|
||||
|
||||
$routeInfo = $dispatcher->dispatch(
|
||||
$request->getMethod(),
|
||||
$request->getUri()->getPath(),
|
||||
);
|
||||
|
||||
try {
|
||||
switch ($routeInfo[0]) {
|
||||
case Dispatcher::METHOD_NOT_ALLOWED:
|
||||
throw new NotAllowedException();
|
||||
case Dispatcher::FOUND:
|
||||
/** @var RequestHandlerInterface $handler */
|
||||
$handler = $container->get($routeInfo[1][0]);
|
||||
$method = $routeInfo[1][1];
|
||||
if (!$handler instanceof RequestHandlerInterface) {
|
||||
throw new InternalServerErrorException();
|
||||
}
|
||||
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
|
||||
$request = $request->withAttribute($attributeName, $attributeValue);
|
||||
}
|
||||
$response = $handler->$method($request);
|
||||
break;
|
||||
case Dispatcher::NOT_FOUND:
|
||||
default:
|
||||
throw new NotFoundException();
|
||||
}
|
||||
} catch (NotAllowedException) {
|
||||
$response = $container->get(NotAllowed::class)->handle($request);
|
||||
} catch (NotFoundException) {
|
||||
$response = $container->get(NotFound::class)->handle($request);
|
||||
} catch (Exception) {
|
||||
$response = $container->get(InternalServerError::class)->handle($request);
|
||||
}
|
||||
|
||||
/** @var string $name */
|
||||
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,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class InternalServerErrorException extends Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class NotAllowedException extends Exception
|
||||
{
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lubian\NoFramework\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
final class NotFoundException extends Exception
|
||||
{
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue