add solutions for chapter 10
This commit is contained in:
parent
d880aeb9a6
commit
e91a166816
18 changed files with 1872 additions and 0 deletions
50
implementation/10/composer.json
Normal file
50
implementation/10/composer.json
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
{
|
||||||
|
"name": "lubian/no-framework",
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Lubian\\NoFramework\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "example",
|
||||||
|
"email": "test@example.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"php": ">=8.1",
|
||||||
|
"filp/whoops": "^2.14",
|
||||||
|
"laminas/laminas-diactoros": "^2.11",
|
||||||
|
"nikic/fast-route": "^1.3",
|
||||||
|
"psr/http-server-handler": "^1.0",
|
||||||
|
"psr/container": "^1.0",
|
||||||
|
"php-di/php-di": "^6.4"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpstan/phpstan": "^1.6",
|
||||||
|
"symfony/var-dumper": "^6.0",
|
||||||
|
"slevomat/coding-standard": "^7.2",
|
||||||
|
"symplify/easy-coding-standard": "^10.2",
|
||||||
|
"rector/rector": "^0.12.23",
|
||||||
|
"phpstan/extension-installer": "^1.1",
|
||||||
|
"phpstan/phpstan-strict-rules": "^1.2",
|
||||||
|
"thecodingmachine/phpstan-strict-rules": "^1.0"
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"dealerdirect/phpcodesniffer-composer-installer": true,
|
||||||
|
"phpstan/extension-installer": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"serve": [
|
||||||
|
"Composer\\Config::disableProcessTimeout",
|
||||||
|
"php -S 0.0.0.0:1235 -t public"
|
||||||
|
],
|
||||||
|
"phpstan": "./vendor/bin/phpstan analyze",
|
||||||
|
"baseline": "./vendor/bin/phpstan analyze --generate-baseline",
|
||||||
|
"check": "./vendor/bin/ecs",
|
||||||
|
"fix": "./vendor/bin/ecs --fix",
|
||||||
|
"rector": "./vendor/bin/rector process"
|
||||||
|
}
|
||||||
|
}
|
1466
implementation/10/composer.lock
generated
Normal file
1466
implementation/10/composer.lock
generated
Normal file
File diff suppressed because it is too large
Load diff
23
implementation/10/config/container.php
Normal file
23
implementation/10/config/container.php
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use DI\ContainerBuilder;
|
||||||
|
use Laminas\Diactoros\Response;
|
||||||
|
use Laminas\Diactoros\ServerRequestFactory;
|
||||||
|
use Lubian\NoFramework\Service\Time\Clock;
|
||||||
|
use Lubian\NoFramework\Service\Time\SystemClock;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
|
||||||
|
use function FastRoute\simpleDispatcher;
|
||||||
|
|
||||||
|
$builder = new ContainerBuilder;
|
||||||
|
$builder->addDefinitions(
|
||||||
|
[
|
||||||
|
ServerRequestInterface::class => fn () => ServerRequestFactory::fromGlobals(),
|
||||||
|
ResponseInterface::class => fn () => new Response,
|
||||||
|
FastRoute\Dispatcher::class => fn () => simpleDispatcher(require __DIR__ . '/routes.php'),
|
||||||
|
Clock::class => fn () => new SystemClock,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
return $builder->build();
|
12
implementation/10/config/routes.php
Normal file
12
implementation/10/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 RI;
|
||||||
|
|
||||||
|
return function (RouteCollector $r) {
|
||||||
|
$r->addRoute('GET', '/hello[/{name}]', Hello::class);
|
||||||
|
$r->addRoute('GET', '/other', [Other::class, 'handle']);
|
||||||
|
$r->addRoute('GET', '/', fn (RI $r) => $r->withStatus(302)->withHeader('Location', '/hello'));
|
||||||
|
};
|
89
implementation/10/ecs.php
Normal file
89
implementation/10/ecs.php
Normal file
|
@ -0,0 +1,89 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
|
||||||
|
use PhpCsFixer\Fixer\Operator\NewWithBracesFixer;
|
||||||
|
use PhpCsFixer\Fixer\PhpTag\BlankLineAfterOpeningTagFixer;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Classes\ClassConstantVisibilitySniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\ControlStructures\NewWithoutParenthesesSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Namespaces\AlphabeticallySortedUsesSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Namespaces\DisallowGroupUseSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Namespaces\MultipleUsesPerLineSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Namespaces\NamespaceSpacingSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Namespaces\ReferenceUsedNamesOnlySniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\Namespaces\UseSpacingSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\TypeHints\DeclareStrictTypesSniff;
|
||||||
|
use SlevomatCodingStandard\Sniffs\TypeHints\UnionTypeHintFormatSniff;
|
||||||
|
use Symplify\EasyCodingStandard\Config\ECSConfig;
|
||||||
|
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;
|
||||||
|
|
||||||
|
return static function (ECSConfig $config): void {
|
||||||
|
$config->parallel();
|
||||||
|
$config->paths([__DIR__ . '/src', __DIR__ . '/config', __DIR__ . '/ecs.php', __DIR__ . '/rector.php']);
|
||||||
|
$config->skip([BlankLineAfterOpeningTagFixer::class, OrderedImportsFixer::class, NewWithBracesFixer::class]);
|
||||||
|
|
||||||
|
$config->sets([
|
||||||
|
SetList::PSR_12,
|
||||||
|
SetList::STRICT,
|
||||||
|
SetList::ARRAY,
|
||||||
|
SetList::SPACES,
|
||||||
|
SetList::DOCBLOCK,
|
||||||
|
SetList::CLEAN_CODE,
|
||||||
|
SetList::COMMON,
|
||||||
|
SetList::COMMENTS,
|
||||||
|
SetList::NAMESPACES,
|
||||||
|
SetList::SYMPLIFY,
|
||||||
|
SetList::CONTROL_STRUCTURES,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// force visibility declaration on class constants
|
||||||
|
$config->ruleWithConfiguration(ClassConstantVisibilitySniff::class, [
|
||||||
|
'fixable' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// sort all use statements
|
||||||
|
$config->rules([
|
||||||
|
AlphabeticallySortedUsesSniff::class,
|
||||||
|
DisallowGroupUseSniff::class,
|
||||||
|
MultipleUsesPerLineSniff::class,
|
||||||
|
NamespaceSpacingSniff::class,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// import all namespaces, and event php core functions and classes
|
||||||
|
$config->ruleWithConfiguration(
|
||||||
|
ReferenceUsedNamesOnlySniff::class,
|
||||||
|
[
|
||||||
|
'allowFallbackGlobalConstants' => false,
|
||||||
|
'allowFallbackGlobalFunctions' => false,
|
||||||
|
'allowFullyQualifiedGlobalClasses' => false,
|
||||||
|
'allowFullyQualifiedGlobalConstants' => false,
|
||||||
|
'allowFullyQualifiedGlobalFunctions' => false,
|
||||||
|
'allowFullyQualifiedNameForCollidingClasses' => true,
|
||||||
|
'allowFullyQualifiedNameForCollidingConstants' => true,
|
||||||
|
'allowFullyQualifiedNameForCollidingFunctions' => true,
|
||||||
|
'searchAnnotations' => true,
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
// define newlines between use statements
|
||||||
|
$config->ruleWithConfiguration(UseSpacingSniff::class, [
|
||||||
|
'linesCountBeforeFirstUse' => 1,
|
||||||
|
'linesCountBetweenUseTypes' => 1,
|
||||||
|
'linesCountAfterLastUse' => 1,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// strict types declaration should be on same line as opening tag
|
||||||
|
$config->ruleWithConfiguration(DeclareStrictTypesSniff::class, [
|
||||||
|
'declareOnFirstLine' => true,
|
||||||
|
'spacesCountAroundEqualsSign' => 0,
|
||||||
|
]);
|
||||||
|
|
||||||
|
// disallow ?Foo typehint in favor of Foo|null
|
||||||
|
$config->ruleWithConfiguration(UnionTypeHintFormatSniff::class, [
|
||||||
|
'withSpaces' => 'no',
|
||||||
|
'shortNullable' => 'no',
|
||||||
|
'nullPosition' => 'last',
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Remove useless parentheses in new statements
|
||||||
|
$config->rule(NewWithoutParenthesesSniff::class);
|
||||||
|
};
|
6
implementation/10/phpstan-baseline.neon
Normal file
6
implementation/10/phpstan-baseline.neon
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
parameters:
|
||||||
|
ignoreErrors:
|
||||||
|
-
|
||||||
|
message: "#^Short ternary operator is not allowed\\. Use null coalesce operator if applicable or consider using long ternary\\.$#"
|
||||||
|
count: 1
|
||||||
|
path: src/Bootstrap.php
|
8
implementation/10/phpstan.neon
Normal file
8
implementation/10/phpstan.neon
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
includes:
|
||||||
|
- phpstan-baseline.neon
|
||||||
|
|
||||||
|
parameters:
|
||||||
|
level: max
|
||||||
|
paths:
|
||||||
|
- src
|
||||||
|
- config
|
BIN
implementation/10/public/favicon.ico
Normal file
BIN
implementation/10/public/favicon.ico
Normal file
Binary file not shown.
After Width: | Height: | Size: 15 KiB |
3
implementation/10/public/index.php
Normal file
3
implementation/10/public/index.php
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
require __DIR__ . '/../src/Bootstrap.php';
|
12
implementation/10/rector.php
Normal file
12
implementation/10/rector.php
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Config\RectorConfig;
|
||||||
|
use Rector\Set\ValueObject\LevelSetList;
|
||||||
|
|
||||||
|
return static function (RectorConfig $rectorConfig): void {
|
||||||
|
$rectorConfig->paths([__DIR__ . '/src', __DIR__ . '/config', __DIR__ . '/ecs.php', __DIR__ . '/rector.php']);
|
||||||
|
|
||||||
|
$rectorConfig->importNames();
|
||||||
|
|
||||||
|
$rectorConfig->sets([LevelSetList::UP_TO_PHP_81]);
|
||||||
|
};
|
24
implementation/10/src/Action/Hello.php
Normal file
24
implementation/10/src/Action/Hello.php
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework\Action;
|
||||||
|
|
||||||
|
use Lubian\NoFramework\Service\Time\Clock;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
final class Hello
|
||||||
|
{
|
||||||
|
public function __invoke(
|
||||||
|
ResponseInterface $response,
|
||||||
|
Clock $clock,
|
||||||
|
string $name = 'Stranger'
|
||||||
|
): ResponseInterface
|
||||||
|
{
|
||||||
|
$body = $response->getBody();
|
||||||
|
|
||||||
|
$body->write('Hello ' . $name . '!<br />');
|
||||||
|
$body->write('The time is: ' . $clock->now()->format('H:i:s'));
|
||||||
|
|
||||||
|
return $response->withBody($body)
|
||||||
|
->withStatus(200);
|
||||||
|
}
|
||||||
|
}
|
24
implementation/10/src/Action/Other.php
Normal file
24
implementation/10/src/Action/Other.php
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework\Action;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
|
final class Other implements RequestHandlerInterface
|
||||||
|
{
|
||||||
|
public function __construct(private readonly ResponseInterface $response)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||||
|
{
|
||||||
|
$body = $this->response->getBody();
|
||||||
|
|
||||||
|
$body->write('This works too!');
|
||||||
|
|
||||||
|
return $this->response->withBody($body)
|
||||||
|
->withStatus(200);
|
||||||
|
}
|
||||||
|
}
|
105
implementation/10/src/Bootstrap.php
Normal file
105
implementation/10/src/Bootstrap.php
Normal file
|
@ -0,0 +1,105 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework;
|
||||||
|
|
||||||
|
use FastRoute\Dispatcher;
|
||||||
|
use Invoker\InvokerInterface;
|
||||||
|
use Laminas\Diactoros\Response;
|
||||||
|
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 Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
use Throwable;
|
||||||
|
use Whoops\Handler\PrettyPageHandler;
|
||||||
|
use Whoops\Run;
|
||||||
|
|
||||||
|
use function assert;
|
||||||
|
use function error_log;
|
||||||
|
use function error_reporting;
|
||||||
|
use function getenv;
|
||||||
|
use function header;
|
||||||
|
use function sprintf;
|
||||||
|
use function strtolower;
|
||||||
|
|
||||||
|
use const E_ALL;
|
||||||
|
|
||||||
|
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 $t) {
|
||||||
|
error_log('ERROR: ' . $t->getMessage(), $t->getCode());
|
||||||
|
echo 'Oooopsie';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$whoops->register();
|
||||||
|
|
||||||
|
$container = require __DIR__ . '/../config/container.php';
|
||||||
|
assert($container instanceof ContainerInterface);
|
||||||
|
|
||||||
|
$request = $container->get(ServerRequestInterface::class);
|
||||||
|
assert($request instanceof ServerRequestInterface);
|
||||||
|
|
||||||
|
$dispatcher = $container->get(Dispatcher::class);
|
||||||
|
assert($dispatcher instanceof Dispatcher);
|
||||||
|
|
||||||
|
$routeInfo = $dispatcher->dispatch($request->getMethod(), $request->getUri() ->getPath(),);
|
||||||
|
|
||||||
|
try {
|
||||||
|
switch ($routeInfo[0]) {
|
||||||
|
case Dispatcher::FOUND:
|
||||||
|
foreach ($routeInfo[2] as $attributeName => $attributeValue) {
|
||||||
|
$request = $request->withAttribute($attributeName, $attributeValue);
|
||||||
|
}
|
||||||
|
$routeInfo[2]['request'] = $request;
|
||||||
|
assert($container instanceof InvokerInterface);
|
||||||
|
$response = $container->call($routeInfo[1], $routeInfo[2]);
|
||||||
|
assert($response instanceof ResponseInterface);
|
||||||
|
break;
|
||||||
|
case Dispatcher::METHOD_NOT_ALLOWED:
|
||||||
|
throw new MethodNotAllowed;
|
||||||
|
case Dispatcher::NOT_FOUND:
|
||||||
|
default:
|
||||||
|
throw new NotFound;
|
||||||
|
}
|
||||||
|
} catch (MethodNotAllowed) {
|
||||||
|
$response = (new Response)->withStatus(405);
|
||||||
|
$response->getBody()
|
||||||
|
->write('Method not Allowed');
|
||||||
|
} catch (NotFound) {
|
||||||
|
$response = (new Response)->withStatus(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();
|
9
implementation/10/src/Exception/InternalServerError.php
Normal file
9
implementation/10/src/Exception/InternalServerError.php
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
final class InternalServerError extends Exception
|
||||||
|
{
|
||||||
|
}
|
9
implementation/10/src/Exception/MethodNotAllowed.php
Normal file
9
implementation/10/src/Exception/MethodNotAllowed.php
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework\Exception;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
|
||||||
|
final class MethodNotAllowed extends Exception
|
||||||
|
{
|
||||||
|
}
|
9
implementation/10/src/Exception/NotFound.php
Normal file
9
implementation/10/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
|
||||||
|
{
|
||||||
|
}
|
10
implementation/10/src/Service/Time/Clock.php
Normal file
10
implementation/10/src/Service/Time/Clock.php
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework\Service\Time;
|
||||||
|
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
interface Clock
|
||||||
|
{
|
||||||
|
public function now(): DateTimeImmutable;
|
||||||
|
}
|
13
implementation/10/src/Service/Time/SystemClock.php
Normal file
13
implementation/10/src/Service/Time/SystemClock.php
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Lubian\NoFramework\Service\Time;
|
||||||
|
|
||||||
|
use DateTimeImmutable;
|
||||||
|
|
||||||
|
final class SystemClock implements Clock
|
||||||
|
{
|
||||||
|
public function now(): DateTimeImmutable
|
||||||
|
{
|
||||||
|
return new DateTimeImmutable;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue