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
use PhpCsFixer\Runner\Parallel\ParallelConfig;
$finder = PhpCsFixer\Finder::create();
if (!defined('__PHPCS_ROOT__')) {

View file

@ -4,12 +4,11 @@ declare(strict_types=1);
namespace Benzine\Actions;
use App\Domain\DomainException\DomainRecordNotFoundException;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Slim\Exception\HttpBadRequestException;
use Slim\Exception\HttpNotFoundException;
use Slim\Views\Twig;
abstract class Action
{
@ -19,8 +18,10 @@ abstract class Action
protected array $args;
public function __construct(protected LoggerInterface $logger)
{
public function __construct(
protected LoggerInterface $logger,
protected Twig $twig,
) {
$this->response = new \Slim\Psr7\Response();
}
@ -33,7 +34,6 @@ abstract class Action
}
/**
* @return mixed
* @throws HttpBadRequestException
*/
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
{
@ -62,13 +62,24 @@ abstract class Action
return $this->response
->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
->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;
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 NOT_ALLOWED = 'NOT_ALLOWED';
public const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED';
public const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
public const SERVER_ERROR = 'SERVER_ERROR';
public const UNAUTHENTICATED = 'UNAUTHENTICATED';
public const VALIDATION_ERROR = 'VALIDATION_ERROR';
public const VERIFICATION_ERROR = 'VERIFICATION_ERROR';
public const NOT_ALLOWED = 'NOT_ALLOWED';
public const NOT_IMPLEMENTED = 'NOT_IMPLEMENTED';
public const RESOURCE_NOT_FOUND = 'RESOURCE_NOT_FOUND';
public const SERVER_ERROR = 'SERVER_ERROR';
public const UNAUTHENTICATED = 'UNAUTHENTICATED';
public const VALIDATION_ERROR = 'VALIDATION_ERROR';
public const VERIFICATION_ERROR = 'VERIFICATION_ERROR';
private string $type;
@ -24,7 +22,7 @@ class ActionError implements JsonSerializable
public function __construct(string $type, ?string $description = null)
{
$this->type = $type;
$this->type = $type;
$this->description = $description;
}
@ -36,6 +34,7 @@ class ActionError implements JsonSerializable
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
@ -47,6 +46,7 @@ class ActionError implements JsonSerializable
public function setDescription(?string $description = null): self
{
$this->description = $description;
return $this;
}
@ -54,7 +54,7 @@ class ActionError implements JsonSerializable
public function jsonSerialize(): array
{
return [
'type' => $this->type,
'type' => $this->type,
'description' => $this->description,
];
}

View file

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

View file

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

View file

@ -10,8 +10,6 @@ use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Monolog\Logger;
use Psr\Http\Message\ServerRequestInterface;
use PushToLive\Kernel;
use SebastianBergmann\Timer\Timer;
use Slim\App;
class Router
@ -53,7 +51,6 @@ class Router
'Benzine\\Controllers\\' . $fileClassName,
];
foreach ($expectedClasses as $expectedClass) {
if (!class_exists($expectedClass)) {
$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);
foreach($routeAttibutes as $routeAttibute){
foreach($routeAttibute->getArguments()['methods'] as $httpMethod){
foreach ($routeAttibutes as $routeAttibute) {
foreach ($routeAttibute->getArguments()['methods'] as $httpMethod) {
$newRoute = (new Route())
->setHttpMethod($httpMethod)
->setRouterPattern('/' . ltrim($routeAttibute->getArguments()['path'], '/'))
->setCallback($expectedClass . ':' . $method->name)
->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) {
$newRoute->addValidDomain($domain);
}

View file

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

View file

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