Skip to content

Commit

Permalink
php-cs-fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbaggett committed Jun 10, 2024
1 parent 7a3b093 commit c128b4b
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

use PhpCsFixer\Runner\Parallel\ParallelConfig;

$finder = PhpCsFixer\Finder::create();

if (!defined('__PHPCS_ROOT__')) {
Expand Down
29 changes: 20 additions & 9 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

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
{
Expand All @@ -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();
}

Expand All @@ -33,7 +34,6 @@ protected function getFormData()
}

/**
* @return mixed
* @throws HttpBadRequestException
*/
protected function resolveArg(string $name)
Expand All @@ -46,7 +46,7 @@ protected function resolveArg(string $name)
}

/**
* @param array|object|null $data
* @param null|array|object $data
*/
protected function respondWithData($data = null, int $statusCode = 200): Response
{
Expand All @@ -62,13 +62,24 @@ protected function respond(ActionPayload $payload): Response

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');
}
}
26 changes: 13 additions & 13 deletions src/Actions/ActionError.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

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;

private ?string $description;

public function __construct(string $type, ?string $description = null)
{
$this->type = $type;
$this->type = $type;
$this->description = $description;
}

Expand All @@ -36,6 +34,7 @@ public function getType(): string
public function setType(string $type): self
{
$this->type = $type;

return $this;
}

Expand All @@ -47,14 +46,15 @@ public function getDescription(): ?string
public function setDescription(?string $description = null): self
{
$this->description = $description;

return $this;
}

#[\ReturnTypeWillChange]
public function jsonSerialize(): array
{
return [
'type' => $this->type,
'type' => $this->type,
'description' => $this->description,
];
}
Expand Down
14 changes: 6 additions & 8 deletions src/Actions/ActionPayload.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,25 @@

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;

private ?ActionError $error;

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
Expand All @@ -33,7 +31,7 @@ public function getStatusCode(): int
}

/**
* @return array|null|object
* @return null|array|object
*/
public function getData()
{
Expand Down
16 changes: 8 additions & 8 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -378,9 +378,7 @@ public function setupContainer(): Container
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(
Expand Down Expand Up @@ -638,12 +636,14 @@ public function getContainer(): ContainerInterface
}

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);
}

Expand Down
9 changes: 3 additions & 6 deletions src/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
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
Expand Down Expand Up @@ -53,7 +51,6 @@ public function loadRoutesFromAnnotations(
'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.', [
Expand Down Expand Up @@ -81,15 +78,15 @@ public function loadRoutesFromAnnotations(
}

$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);
}
Expand Down
1 change: 0 additions & 1 deletion src/Services/ConfigurationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Benzine\App;
use Benzine\Exceptions\BenzineConfigurationException;
use SebastianBergmann\Timer\Timer;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;

Expand Down
12 changes: 5 additions & 7 deletions src/Services/EnvironmentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,5 +81,4 @@ public function getPublicUrl(): string
{
return $this->getPublicHostname() . $this->getPublicPath();
}

}

0 comments on commit c128b4b

Please sign in to comment.