add data from work folder
This commit is contained in:
parent
97579d6d91
commit
48c9c9467d
218 changed files with 16123 additions and 1233 deletions
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