php-cs-fixer

This commit is contained in:
Greyscale 2024-06-10 06:31:06 +02:00
parent 7a3b093cf7
commit c128b4bcce
No known key found for this signature in database
GPG key ID: 74BAFF55434DA4B2
8 changed files with 58 additions and 52 deletions

View file

@ -1,4 +1,7 @@
<?php <?php
use PhpCsFixer\Runner\Parallel\ParallelConfig;
$finder = PhpCsFixer\Finder::create(); $finder = PhpCsFixer\Finder::create();
if (!defined('__PHPCS_ROOT__')) { if (!defined('__PHPCS_ROOT__')) {

View file

@ -4,12 +4,11 @@ declare(strict_types=1);
namespace Benzine\Actions; namespace Benzine\Actions;
use App\Domain\DomainException\DomainRecordNotFoundException;
use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use Slim\Exception\HttpBadRequestException; use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException; use Slim\Views\Twig;
abstract class Action abstract class Action
{ {
@ -19,8 +18,10 @@ abstract class Action
protected array $args; protected array $args;
public function __construct(protected LoggerInterface $logger) public function __construct(
{ protected LoggerInterface $logger,
protected Twig $twig,
) {
$this->response = new \Slim\Psr7\Response(); $this->response = new \Slim\Psr7\Response();
} }
@ -33,7 +34,6 @@ abstract class Action
} }
/** /**
* @return mixed
* @throws HttpBadRequestException * @throws HttpBadRequestException
*/ */
protected function resolveArg(string $name) protected function resolveArg(string $name)
@ -46,7 +46,7 @@ abstract class Action
} }
/** /**
* @param array|object|null $data * @param null|array|object $data
*/ */
protected function respondWithData($data = null, int $statusCode = 200): Response protected function respondWithData($data = null, int $statusCode = 200): Response
{ {
@ -62,13 +62,24 @@ abstract class Action
return $this->response return $this->response
->withHeader('Content-Type', 'application/json') ->withHeader('Content-Type', 'application/json')
->withStatus($payload->getStatusCode()); ->withStatus($payload->getStatusCode())
;
} }
protected function redirect(string $redirectUrl) : Response protected function redirect(string $redirectUrl): Response
{ {
return $this->response return $this->response
->withHeader('Location', $redirectUrl) ->withHeader('Location', $redirectUrl)
->withStatus(302); ->withStatus(302)
;
}
protected function render($response, string $template, array $data = []): Response
{
return $this->twig->render(
$response,
$template,
$data
)->withHeader('Content-Type', 'text/html');
} }
} }

View file

@ -4,19 +4,17 @@ declare(strict_types=1);
namespace Benzine\Actions; namespace Benzine\Actions;
use JsonSerializable; class ActionError implements \JsonSerializable
class ActionError implements JsonSerializable
{ {
public const BAD_REQUEST = 'BAD_REQUEST'; public const BAD_REQUEST = 'BAD_REQUEST';
public const INSUFFICIENT_PRIVILEGES = 'INSUFFICIENT_PRIVILEGES'; public const INSUFFICIENT_PRIVILEGES = 'INSUFFICIENT_PRIVILEGES';
public const NOT_ALLOWED = 'NOT_ALLOWED'; public const NOT_ALLOWED = 'NOT_ALLOWED';
public const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED'; public const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED';
public const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND'; public const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
public const SERVER_ERROR = 'SERVER_ERROR'; public const SERVER_ERROR = 'SERVER_ERROR';
public const UNAUTHENTICATED = 'UNAUTHENTICATED'; public const UNAUTHENTICATED = 'UNAUTHENTICATED';
public const VALIDATION_ERROR = 'VALIDATION_ERROR'; public const VALIDATION_ERROR = 'VALIDATION_ERROR';
public const VERIFICATION_ERROR = 'VERIFICATION_ERROR'; public const VERIFICATION_ERROR = 'VERIFICATION_ERROR';
private string $type; private string $type;
@ -24,7 +22,7 @@ class ActionError implements JsonSerializable
public function __construct(string $type, ?string $description = null) public function __construct(string $type, ?string $description = null)
{ {
$this->type = $type; $this->type = $type;
$this->description = $description; $this->description = $description;
} }
@ -36,6 +34,7 @@ class ActionError implements JsonSerializable
public function setType(string $type): self public function setType(string $type): self
{ {
$this->type = $type; $this->type = $type;
return $this; return $this;
} }
@ -47,6 +46,7 @@ class ActionError implements JsonSerializable
public function setDescription(?string $description = null): self public function setDescription(?string $description = null): self
{ {
$this->description = $description; $this->description = $description;
return $this; return $this;
} }
@ -54,7 +54,7 @@ class ActionError implements JsonSerializable
public function jsonSerialize(): array public function jsonSerialize(): array
{ {
return [ return [
'type' => $this->type, 'type' => $this->type,
'description' => $this->description, 'description' => $this->description,
]; ];
} }

View file

@ -4,14 +4,12 @@ declare(strict_types=1);
namespace Benzine\Actions; namespace Benzine\Actions;
use JsonSerializable; class ActionPayload implements \JsonSerializable
class ActionPayload implements JsonSerializable
{ {
private int $statusCode; private int $statusCode;
/** /**
* @var array|object|null * @var null|array|object
*/ */
private $data; private $data;
@ -19,12 +17,12 @@ class ActionPayload implements JsonSerializable
public function __construct( public function __construct(
int $statusCode = 200, int $statusCode = 200,
$data = null, $data = null,
?ActionError $error = null ?ActionError $error = null
) { ) {
$this->statusCode = $statusCode; $this->statusCode = $statusCode;
$this->data = $data; $this->data = $data;
$this->error = $error; $this->error = $error;
} }
public function getStatusCode(): int public function getStatusCode(): int
@ -33,7 +31,7 @@ class ActionPayload implements JsonSerializable
} }
/** /**
* @return array|null|object * @return null|array|object
*/ */
public function getData() public function getData()
{ {

View file

@ -74,7 +74,7 @@ class App
protected bool $isSessionsEnabled = true; protected bool $isSessionsEnabled = true;
protected bool $interrogateControllersComplete = false; protected bool $interrogateControllersComplete = false;
protected ?CachePoolChain $cachePoolChain = null; protected ?CachePoolChain $cachePoolChain = null;
private array $viewPaths = []; private array $viewPaths = [APP_ROOT . '/views'];
private string $cachePath = APP_ROOT . '/cache'; private string $cachePath = APP_ROOT . '/cache';
private string $logPath = APP_ROOT . '/logs'; private string $logPath = APP_ROOT . '/logs';
private array $supportedLanguages = ['en_US']; private array $supportedLanguages = ['en_US'];
@ -378,9 +378,7 @@ class App
return $monolog; return $monolog;
}); });
$container->set(LoggerInterface::class, function (Logger $logger) { $container->set(LoggerInterface::class, fn (Logger $logger) => $logger);
return $logger;
});
$container->set(Redis::class, function (ConfigurationService $configurationService, Logger $logger, EnvironmentService $environmentService) { $container->set(Redis::class, function (ConfigurationService $configurationService, Logger $logger, EnvironmentService $environmentService) {
return new Redis( return new Redis(
@ -638,12 +636,14 @@ class App
} }
protected static Timer $timer; protected static Timer $timer;
static public function Timing(){
public static function Timing(): void
{
$duration = self::$timer->stop(); $duration = self::$timer->stop();
# Get caller // Get caller
$caller = debug_backtrace()[0]; $caller = debug_backtrace()[0];
if($duration->asSeconds() >= 1) { if ($duration->asSeconds() >= 1) {
$timingMessage = sprintf("%f seconds: (%s:%d)", $duration->asSeconds(), $caller['file'], $caller['line']); $timingMessage = sprintf('%f seconds: (%s:%d)', $duration->asSeconds(), $caller['file'], $caller['line']);
\Kint::dump($timingMessage); \Kint::dump($timingMessage);
} }

View file

@ -10,8 +10,6 @@ use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry; use Doctrine\Common\Annotations\AnnotationRegistry;
use Monolog\Logger; use Monolog\Logger;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use PushToLive\Kernel;
use SebastianBergmann\Timer\Timer;
use Slim\App; use Slim\App;
class Router class Router
@ -53,7 +51,6 @@ class Router
'Benzine\\Controllers\\' . $fileClassName, 'Benzine\\Controllers\\' . $fileClassName,
]; ];
foreach ($expectedClasses as $expectedClass) { foreach ($expectedClasses as $expectedClass) {
if (!class_exists($expectedClass)) { if (!class_exists($expectedClass)) {
$this->logger->warning('While loading routes from annotations in {file}, expected class {expectedClass} does not exist.', [ $this->logger->warning('While loading routes from annotations in {file}, expected class {expectedClass} does not exist.', [
@ -81,15 +78,15 @@ class Router
} }
$routeAttibutes = $method->getAttributes(\Benzine\Annotations\Route::class); $routeAttibutes = $method->getAttributes(\Benzine\Annotations\Route::class);
foreach($routeAttibutes as $routeAttibute){ foreach ($routeAttibutes as $routeAttibute) {
foreach($routeAttibute->getArguments()['methods'] as $httpMethod){ foreach ($routeAttibute->getArguments()['methods'] as $httpMethod) {
$newRoute = (new Route()) $newRoute = (new Route())
->setHttpMethod($httpMethod) ->setHttpMethod($httpMethod)
->setRouterPattern('/' . ltrim($routeAttibute->getArguments()['path'], '/')) ->setRouterPattern('/' . ltrim($routeAttibute->getArguments()['path'], '/'))
->setCallback($expectedClass . ':' . $method->name) ->setCallback($expectedClass . ':' . $method->name)
->setWeight($routeAttibute->getArguments()['weight'] ?? 100) ->setWeight($routeAttibute->getArguments()['weight'] ?? 100)
; ;
if(isset($routeAttibute->getArguments()['domains']) && is_array($routeAttibute->getArguments()['domains'])){ if (isset($routeAttibute->getArguments()['domains']) && is_array($routeAttibute->getArguments()['domains'])) {
foreach ($routeAttibute->getArguments()['domains'] as $domain) { foreach ($routeAttibute->getArguments()['domains'] as $domain) {
$newRoute->addValidDomain($domain); $newRoute->addValidDomain($domain);
} }

View file

@ -6,7 +6,6 @@ namespace Benzine\Services;
use Benzine\App; use Benzine\App;
use Benzine\Exceptions\BenzineConfigurationException; use Benzine\Exceptions\BenzineConfigurationException;
use SebastianBergmann\Timer\Timer;
use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml; use Symfony\Component\Yaml\Yaml;

View file

@ -11,21 +11,20 @@ class EnvironmentService
public function __construct() public function __construct()
{ {
$this->environmentVariables = array_merge($_SERVER, $_ENV); $this->environmentVariables = array_merge($_SERVER, $_ENV);
if(file_exists(APP_ROOT . '/.env')){ if (file_exists(APP_ROOT . '/.env')) {
$env = file_get_contents(APP_ROOT . '/.env'); $env = file_get_contents(APP_ROOT . '/.env');
$lines = explode("\n", $env); $lines = explode("\n", $env);
foreach($lines as $line){ foreach ($lines as $line) {
$line = trim($line); $line = trim($line);
if($line == ''){ if ($line == '') {
continue; continue;
} }
$parts = explode('=', $line); $parts = explode('=', $line);
$this->environmentVariables[$parts[0]] = $parts[1]; $this->environmentVariables[$parts[0]] = $parts[1];
} }
} }
ksort($this->environmentVariables); ksort($this->environmentVariables);
} }
public function has(string $key): bool public function has(string $key): bool
@ -82,5 +81,4 @@ class EnvironmentService
{ {
return $this->getPublicHostname() . $this->getPublicPath(); return $this->getPublicHostname() . $this->getPublicPath();
} }
} }