php-cs-fixer sends its regards.
This commit is contained in:
parent
15892541b2
commit
2876dcab12
15 changed files with 86 additions and 92 deletions
|
|
@ -17,5 +17,4 @@ class JsonSchema
|
|||
* @Required
|
||||
*/
|
||||
public string $schema;
|
||||
|
||||
}
|
||||
|
|
|
|||
22
src/App.php
22
src/App.php
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Benzine;
|
||||
|
||||
use Benzine\Exceptions\JsonErrorHandler;
|
||||
use Benzine\Middleware\JsonResponseExecTimeMiddleware;
|
||||
use Benzine\Middleware\JsonValidationMiddleware;
|
||||
use Benzine\ORM\Connection\Databases;
|
||||
|
|
@ -50,7 +49,6 @@ use Symfony\Component\Translation;
|
|||
use Tuupola\Middleware\ServerTimingMiddleware;
|
||||
use Twig;
|
||||
use Twig\Loader\FilesystemLoader;
|
||||
use Zeuxisoo\Whoops\Slim\WhoopsMiddleware;
|
||||
|
||||
class App
|
||||
{
|
||||
|
|
@ -114,7 +112,7 @@ class App
|
|||
if ($this->debugMode) {
|
||||
$this->app->addErrorMiddleware(true, true, true, $this->logger);
|
||||
}
|
||||
|
||||
|
||||
$this->debugBar['time']->startMeasure('interrogateTranslations', 'Time to interrogate translation files');
|
||||
$this->interrogateTranslations();
|
||||
$this->debugBar['time']->stopMeasure('interrogateTranslations');
|
||||
|
|
@ -190,10 +188,10 @@ class App
|
|||
->useAutowiring(true)
|
||||
->useAnnotations(true)
|
||||
;
|
||||
//if ((new Filesystem())->exists($this->getCachePath())) {
|
||||
// if ((new Filesystem())->exists($this->getCachePath())) {
|
||||
// $container->enableCompilation($this->getCachePath());
|
||||
// $container->writeProxiesToFile(true, "{$this->getCachePath()}/injection-proxies");
|
||||
//}
|
||||
// }
|
||||
|
||||
$container = $container->build();
|
||||
|
||||
|
|
@ -402,7 +400,7 @@ class App
|
|||
return (new DebugBar())
|
||||
->addCollector(new PhpInfoCollector())
|
||||
->addCollector(new MessagesCollector())
|
||||
//->addCollector(new RequestDataCollector())
|
||||
// ->addCollector(new RequestDataCollector())
|
||||
->addCollector(new TimeDataCollector())
|
||||
->addCollector(new MemoryCollector())
|
||||
->addCollector(new ExceptionsCollector())
|
||||
|
|
@ -427,7 +425,7 @@ class App
|
|||
|
||||
$this->router = $container->get(Router::class);
|
||||
|
||||
//!\Kint::dump($this->environmentService->all());exit;
|
||||
// !\Kint::dump($this->environmentService->all());exit;
|
||||
return $container;
|
||||
}
|
||||
|
||||
|
|
@ -435,11 +433,11 @@ class App
|
|||
{
|
||||
// Middlewares
|
||||
$this->app->addBodyParsingMiddleware();
|
||||
//$this->app->add($container->get(\Middlewares\Geolocation::class));
|
||||
// $this->app->add($container->get(\Middlewares\Geolocation::class));
|
||||
$this->app->add($container->get(\Middlewares\TrailingSlash::class));
|
||||
//$this->app->add($container->get(\Middlewares\Whoops::class));
|
||||
//$this->app->add($container->get(\Middlewares\Minifier::class));
|
||||
//$this->app->add($container->get(\Middlewares\GzipEncoder::class));
|
||||
// $this->app->add($container->get(\Middlewares\Whoops::class));
|
||||
// $this->app->add($container->get(\Middlewares\Minifier::class));
|
||||
// $this->app->add($container->get(\Middlewares\GzipEncoder::class));
|
||||
$this->app->add($container->get(\Middlewares\ContentLength::class));
|
||||
}
|
||||
|
||||
|
|
@ -450,8 +448,10 @@ class App
|
|||
{
|
||||
if (!self::$isInitialised) {
|
||||
$calledClass = get_called_class();
|
||||
|
||||
/** @var App $tempApp */
|
||||
$tempApp = new $calledClass();
|
||||
|
||||
/** @var ConfigurationService $config */
|
||||
$config = $tempApp->get(ConfigurationService::class);
|
||||
$configCoreClass = $config->getCore();
|
||||
|
|
|
|||
|
|
@ -103,18 +103,20 @@ abstract class AbstractController
|
|||
}
|
||||
|
||||
// Generate an etag
|
||||
$etag = md5($filesystem->lastModified($filename) . $filename);
|
||||
$etag = md5($filesystem->lastModified($filename).$filename);
|
||||
$response = $this->cacheProvider->withEtag($response, $etag);
|
||||
|
||||
// Detect mimetype for content-type header from file meta
|
||||
$mimetype = (new ExtensionMimeTypeDetector())
|
||||
->detectMimeTypeFromPath($filename);
|
||||
->detectMimeTypeFromPath($filename)
|
||||
;
|
||||
|
||||
// No dice? Early-load the data and interrogate that for mimetype then I GUESS.
|
||||
if (!$mimetype) {
|
||||
$data = $filesystem->read($filename);
|
||||
$mimetype = (new FinfoMimeTypeDetector())
|
||||
->detectMimeTypeFromBuffer($data);
|
||||
->detectMimeTypeFromBuffer($data)
|
||||
;
|
||||
}
|
||||
|
||||
// If we have mimetype by this point, send the contenttype
|
||||
|
|
@ -125,7 +127,7 @@ abstract class AbstractController
|
|||
// Send back the response
|
||||
$response
|
||||
->getBody()
|
||||
->write($data ?? $filesystem->read($filename))
|
||||
->write($data ?? $filesystem->read($filename))
|
||||
;
|
||||
|
||||
return $response;
|
||||
|
|
|
|||
|
|
@ -123,5 +123,4 @@ abstract class AbstractCrudController extends AbstractController
|
|||
$response
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,13 +5,14 @@ namespace Benzine\Exceptions;
|
|||
use Slim\Interfaces\ErrorRendererInterface;
|
||||
use Throwable;
|
||||
|
||||
class JsonErrorHandler implements ErrorRendererInterface {
|
||||
class JsonErrorHandler implements ErrorRendererInterface
|
||||
{
|
||||
public function __invoke(Throwable $exception, bool $displayErrorDetails): string
|
||||
{
|
||||
return json_encode([
|
||||
'error' => $exception->getMessage(),
|
||||
'where' => $exception->getFile() . ":" . $exception->getLine(),
|
||||
'where' => $exception->getFile().':'.$exception->getLine(),
|
||||
'code' => $exception->getCode(),
|
||||
], JSON_PRETTY_PRINT);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,12 +5,15 @@ namespace Benzine\Guzzle;
|
|||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\StreamInterface;
|
||||
|
||||
class JsonResponse implements ResponseInterface {
|
||||
class JsonResponse implements ResponseInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected ResponseInterface $response
|
||||
){}
|
||||
) {
|
||||
}
|
||||
|
||||
public function json(){
|
||||
public function json()
|
||||
{
|
||||
return json_decode($this->response->getBody()->getContents(), false);
|
||||
}
|
||||
|
||||
|
|
@ -83,4 +86,4 @@ class JsonResponse implements ResponseInterface {
|
|||
{
|
||||
return $this->response->getReasonPhrase();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,36 +2,27 @@
|
|||
|
||||
namespace Benzine\Middleware;
|
||||
|
||||
use Benzine\Annotations\JsonSchema;
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Slim\Psr7\Factory\StreamFactory;
|
||||
use Swaggest\JsonSchema\Exception;
|
||||
use Swaggest\JsonSchema\Schema;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use Slim\Routing\RouteContext;
|
||||
|
||||
class JsonResponseExecTimeMiddleware implements MiddlewareInterface{
|
||||
|
||||
class JsonResponseExecTimeMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
|
||||
$response = $handler->handle($request);
|
||||
$response->getBody()->rewind();
|
||||
$responseJson = json_decode($response->getBody()->getContents(), true);
|
||||
if($responseJson === null){
|
||||
|
||||
if ($responseJson === null) {
|
||||
return $response;
|
||||
}
|
||||
$memoryUsageBytes = memory_get_peak_usage();
|
||||
$responseJson['Exec'] = [
|
||||
'TimeSeconds' => (float) number_format(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'],5),
|
||||
'TimeSeconds' => (float) number_format(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 5),
|
||||
'MemoryBytes' => $memoryUsageBytes,
|
||||
'MemoryMegaBytes' => (float) number_format($memoryUsageBytes/1024/1024,3),
|
||||
'MemoryMegaBytes' => (float) number_format($memoryUsageBytes / 1024 / 1024, 3),
|
||||
];
|
||||
|
||||
$replacementResponse = new Response();
|
||||
|
|
@ -43,4 +34,4 @@ class JsonResponseExecTimeMiddleware implements MiddlewareInterface{
|
|||
|
||||
return $replacementResponse;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,18 +4,17 @@ namespace Benzine\Middleware;
|
|||
|
||||
use Benzine\Annotations\JsonSchema;
|
||||
use Doctrine\Common\Annotations\AnnotationReader;
|
||||
use Swaggest\JsonSchema\Exception;
|
||||
use Swaggest\JsonSchema\Schema;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
use Slim\Psr7\Request;
|
||||
use Slim\Psr7\Response;
|
||||
use Slim\Routing\RouteContext;
|
||||
use Swaggest\JsonSchema\Exception;
|
||||
use Swaggest\JsonSchema\Schema;
|
||||
|
||||
class JsonValidationMiddleware implements MiddlewareInterface{
|
||||
|
||||
class JsonValidationMiddleware implements MiddlewareInterface
|
||||
{
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
// get the route out of the router...
|
||||
|
|
@ -24,7 +23,7 @@ class JsonValidationMiddleware implements MiddlewareInterface{
|
|||
// Load an annotation reader
|
||||
$reader = new AnnotationReader();
|
||||
// Break up our route into class & method
|
||||
list ($class, $method) = explode(":", $route->getCallable());
|
||||
[$class, $method] = explode(':', $route->getCallable());
|
||||
// Create the reflection class for our class..
|
||||
$rc = new \ReflectionClass($class);
|
||||
// .. And snag the method
|
||||
|
|
@ -33,7 +32,7 @@ class JsonValidationMiddleware implements MiddlewareInterface{
|
|||
// Try to read a json schema annotation..
|
||||
$jsonSchemaAnnotation = $reader->getMethodAnnotation($method, JsonSchema::class);
|
||||
// No annotation? Return early.
|
||||
if(!($jsonSchemaAnnotation instanceof JsonSchema)){
|
||||
if (!($jsonSchemaAnnotation instanceof JsonSchema)) {
|
||||
return $handler->handle($request);
|
||||
}
|
||||
|
||||
|
|
@ -47,12 +46,12 @@ class JsonValidationMiddleware implements MiddlewareInterface{
|
|||
);
|
||||
|
||||
// Throw it through validation.. if it passes, continue
|
||||
try{
|
||||
try {
|
||||
// Validate it...
|
||||
$schema->in(json_decode($request->getBody()->getContents()));
|
||||
// And if we get here, we're golden.
|
||||
return $handler->handle($request);
|
||||
}catch (Exception $exception){
|
||||
} catch (Exception $exception) {
|
||||
// Whelp, we've failed validation, build a failure message.
|
||||
$response = new Response();
|
||||
$content = json_encode([
|
||||
|
|
@ -68,5 +67,4 @@ class JsonValidationMiddleware implements MiddlewareInterface{
|
|||
return $response->withStatus(400);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ abstract class AbstractLuaExtension
|
|||
$this->hash = $this->redis->getUnderlyingRedis()->script('load', $this->getScript());
|
||||
}
|
||||
}
|
||||
//printf("Loaded \"%s\" as \"%s\"\n", $this->getFunctionNames()[0], $this->hash);
|
||||
// printf("Loaded \"%s\" as \"%s\"\n", $this->getFunctionNames()[0], $this->hash);
|
||||
}
|
||||
|
||||
abstract protected function getScript(): string;
|
||||
|
|
|
|||
|
|
@ -295,6 +295,21 @@ class Redis
|
|||
throw new \RedisException('Do not directly call connect()');
|
||||
}
|
||||
|
||||
public function emit(array $message)
|
||||
{
|
||||
return $this->redis->publish(strtolower(APP_NAME), json_encode($message));
|
||||
}
|
||||
|
||||
public function listen($callback): void
|
||||
{
|
||||
ini_set('default_socket_timeout', -1);
|
||||
|
||||
try {
|
||||
$this->redis->psubscribe([strtolower(APP_NAME)], $callback);
|
||||
} catch (\RedisException $exception) {
|
||||
}
|
||||
}
|
||||
|
||||
private function runBeforeRedisCommand(): void
|
||||
{
|
||||
if (!$this->redis->isConnected()) {
|
||||
|
|
@ -305,17 +320,4 @@ class Redis
|
|||
$this->initialiseExtensions();
|
||||
}
|
||||
}
|
||||
|
||||
public function emit(array $message){
|
||||
return $this->redis->publish(strtolower(APP_NAME), json_encode($message));
|
||||
}
|
||||
|
||||
public function listen($callback){
|
||||
ini_set("default_socket_timeout", -1);
|
||||
try {
|
||||
$this->redis->psubscribe([strtolower(APP_NAME)], $callback);
|
||||
}catch(\RedisException $exception){
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,15 +55,14 @@ class Router
|
|||
continue;
|
||||
}
|
||||
|
||||
|
||||
$rc = new \ReflectionClass($expectedClass);
|
||||
if ($rc->isAbstract()) {
|
||||
continue;
|
||||
}
|
||||
#\Kint::dump(
|
||||
# $expectedClass,
|
||||
# $rc
|
||||
#);
|
||||
// \Kint::dump(
|
||||
// $expectedClass,
|
||||
// $rc
|
||||
// );
|
||||
|
||||
foreach ($rc->getMethods() as $method) {
|
||||
if (!$method->isPublic()) {
|
||||
|
|
@ -78,8 +77,8 @@ class Router
|
|||
foreach ($routeAnnotation->methods as $httpMethod) {
|
||||
$newRoute = new Route();
|
||||
|
||||
#\Kint::dump($method);
|
||||
#exit;
|
||||
// \Kint::dump($method);
|
||||
// exit;
|
||||
|
||||
$newRoute
|
||||
->setHttpMethod($httpMethod)
|
||||
|
|
|
|||
|
|
@ -105,7 +105,7 @@ class ConfigurationService
|
|||
{
|
||||
if (!$path) {
|
||||
$path = getcwd();
|
||||
//$path = dirname($this->environmentService->get('SCRIPT_FILENAME'));
|
||||
// $path = dirname($this->environmentService->get('SCRIPT_FILENAME'));
|
||||
}
|
||||
|
||||
if (!(new Filesystem())->exists($path.'/.benzine.yml')) {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ abstract class AbstractQueueWorker extends AbstractWorker
|
|||
|
||||
/** @var string Name of the input redis queue */
|
||||
protected ?string $inputQueue;
|
||||
|
||||
/** @var string[] Name of the output redis queues */
|
||||
protected ?array $outputQueues;
|
||||
|
||||
|
|
|
|||
|
|
@ -4,48 +4,47 @@ namespace Benzine\Workers;
|
|||
|
||||
use Benzine\Redis\Redis;
|
||||
use Benzine\Services\EnvironmentService;
|
||||
use Benzine\Workers\AbstractWorker;
|
||||
use Monolog\Logger;
|
||||
|
||||
abstract class WaitForEmitWorker extends AbstractWorker
|
||||
{
|
||||
public $callback;
|
||||
protected array $messageTypes = [];
|
||||
|
||||
public function __construct(
|
||||
protected Redis $redis,
|
||||
Logger $logger,
|
||||
EnvironmentService $environmentService
|
||||
) {
|
||||
parent::__construct($logger, $environmentService);
|
||||
$this->setCallback([$this, 'message']);
|
||||
}
|
||||
|
||||
public function addMessageTypeListener(string $messageType)
|
||||
{
|
||||
$this->messageTypes[] = $messageType;
|
||||
$this->logger->debug("Added {$messageType} to message type handlers.");
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public $callback;
|
||||
|
||||
public function setCallback($callback)
|
||||
{
|
||||
$this->callback = $callback;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __construct(
|
||||
protected Redis $redis,
|
||||
Logger $logger,
|
||||
EnvironmentService $environmentService
|
||||
)
|
||||
{
|
||||
parent::__construct($logger, $environmentService);
|
||||
$this->setCallback([$this, 'message']);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
$this->logger->debug("Running Emit Worker");
|
||||
$this->redis->listen(array($this, "recv"));
|
||||
$this->logger->debug('Running Emit Worker');
|
||||
$this->redis->listen([$this, 'recv']);
|
||||
}
|
||||
|
||||
public function recv($redis, $pattern, $chan, $msg)
|
||||
public function recv($redis, $pattern, $chan, $msg): void
|
||||
{
|
||||
$json = json_decode($msg, true);
|
||||
if (in_array($json['MESSAGE_TYPE'], $this->messageTypes)) {
|
||||
if (in_array($json['MESSAGE_TYPE'], $this->messageTypes, true)) {
|
||||
call_user_func($this->callback, $json);
|
||||
}
|
||||
}
|
||||
|
|
@ -53,4 +52,4 @@ abstract class WaitForEmitWorker extends AbstractWorker
|
|||
public function iterate(): bool
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ namespace Benzine\Tests;
|
|||
|
||||
use Benzine\Tests\Traits\AppTestTrait;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Slim\Http\Request;
|
||||
|
||||
abstract class AbstractRoutesTestCase extends AbstractBaseTestCase
|
||||
{
|
||||
|
|
@ -15,7 +14,8 @@ abstract class AbstractRoutesTestCase extends AbstractBaseTestCase
|
|||
parent::setUp();
|
||||
}
|
||||
|
||||
public function getApp(){
|
||||
public function getApp()
|
||||
{
|
||||
return $this->slimApp;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue