From 77cbb17f4d629aebdfbc0dd83244ec14d785e208 Mon Sep 17 00:00:00 2001 From: MBorne Date: Tue, 3 Dec 2024 10:29:13 +0100 Subject: [PATCH] chore: PHP 8.3 + start improving logs... --- composer.json | 2 +- config/packages/dev/monolog.yaml | 20 ----- config/packages/monolog.yaml | 42 ++++------ config/packages/prod/monolog.yaml | 16 ---- config/packages/test/monolog.yaml | 12 --- docker-compose.yml | 4 +- src/Controller/Api/ValidationsController.php | 88 +++++++------------- 7 files changed, 49 insertions(+), 135 deletions(-) delete mode 100644 config/packages/dev/monolog.yaml delete mode 100644 config/packages/prod/monolog.yaml delete mode 100644 config/packages/test/monolog.yaml diff --git a/composer.json b/composer.json index 069e399..d897ebe 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "project", "license": "AGPL-3.0-or-later", "require": { - "php": "^7.2 || ^8.0", + "php": "^8.3", "ext-ctype": "*", "ext-iconv": "*", "ext-zip": "*", diff --git a/config/packages/dev/monolog.yaml b/config/packages/dev/monolog.yaml deleted file mode 100644 index 6d7eedd..0000000 --- a/config/packages/dev/monolog.yaml +++ /dev/null @@ -1,20 +0,0 @@ -monolog: - handlers: - main: - type: rotating_file - path: "%kernel.logs_dir%/%kernel.environment%.log" - level: debug - channels: ["!event"] - max_files: 10 - # uncomment to get logging in your browser - # you may have to allow bigger header sizes in your Web server configuration - #firephp: - # type: firephp - # level: info - #chromephp: - # type: chromephp - # level: info - console: - type: console - process_psr_3_messages: false - channels: ["!event", "!doctrine", "!console"] diff --git a/config/packages/monolog.yaml b/config/packages/monolog.yaml index 9db7d8a..e72317d 100644 --- a/config/packages/monolog.yaml +++ b/config/packages/monolog.yaml @@ -10,14 +10,6 @@ when@dev: path: "%kernel.logs_dir%/%kernel.environment%.log" level: debug channels: ["!event"] - # uncomment to get logging in your browser - # you may have to allow bigger header sizes in your Web server configuration - #firephp: - # type: firephp - # level: info - #chromephp: - # type: chromephp - # level: info console: type: console process_psr_3_messages: false @@ -25,17 +17,17 @@ when@dev: when@test: monolog: - handlers: - main: - type: fingers_crossed - action_level: error - handler: nested - excluded_http_codes: [404, 405] - channels: ["!event"] - nested: - type: stream - path: "%kernel.logs_dir%/%kernel.environment%.log" - level: debug + handlers: + main: + type: stream + path: "%kernel.logs_dir%/%kernel.environment%.log" + level: debug + deprecation: + path: "%kernel.logs_dir%/deprecation.log" + channels: ["deprecation"] + type: rotating_file + max_files: 1 + when@prod: monolog: @@ -54,9 +46,9 @@ when@prod: console: type: console process_psr_3_messages: false - channels: ["!event", "!doctrine"] - deprecation: - type: stream - channels: [deprecation] - path: php://stderr - formatter: monolog.formatter.json + channels: ["!event", "!doctrine", "!deprecation"] + # deprecation: + # type: stream + # channels: [deprecation] + # path: php://stderr + # formatter: monolog.formatter.json diff --git a/config/packages/prod/monolog.yaml b/config/packages/prod/monolog.yaml deleted file mode 100644 index bce781c..0000000 --- a/config/packages/prod/monolog.yaml +++ /dev/null @@ -1,16 +0,0 @@ -monolog: - handlers: - main: - type: fingers_crossed - action_level: error - handler: nested - excluded_http_codes: [404, 405] - buffer_size: 50 # How many messages should be saved? Prevent memory leaks - nested: - type: stream - path: "%kernel.logs_dir%/%kernel.environment%.log" - level: debug - console: - type: console - process_psr_3_messages: false - channels: ["!event", "!doctrine"] diff --git a/config/packages/test/monolog.yaml b/config/packages/test/monolog.yaml deleted file mode 100644 index d1c23b1..0000000 --- a/config/packages/test/monolog.yaml +++ /dev/null @@ -1,12 +0,0 @@ -monolog: - handlers: - main: - type: fingers_crossed - action_level: info - handler: nested - excluded_http_codes: [404, 405] - channels: ["!event"] - nested: - type: stream - path: "%kernel.logs_dir%/%kernel.environment%.log" - level: debug diff --git a/docker-compose.yml b/docker-compose.yml index f7fac61..2f191a1 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -16,8 +16,8 @@ services: - http_proxy=${HTTP_PROXY} - https_proxy=${HTTPS_PROXY} - APP_ENV=${APP_ENV} - - DB_CREATE=1 - - DB_UPGRADE=1 + - DB_CREATE=${DB_CREATE:-1} + - DB_UPGRADE=${DB_UPGRADE:-1} - POSTGRES_USER=${POSTGRES_USER} - POSTGRES_PASSWORD=${POSTGRES_PASSWORD} - DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/validator_api?serverVersion=13&charset=utf8 diff --git a/src/Controller/Api/ValidationsController.php b/src/Controller/Api/ValidationsController.php index 19f021d..317f9f4 100755 --- a/src/Controller/Api/ValidationsController.php +++ b/src/Controller/Api/ValidationsController.php @@ -12,6 +12,7 @@ use JMS\Serializer\Serializer; use JMS\Serializer\SerializerInterface; use League\Flysystem\FilesystemOperator; +use Psr\Log\LoggerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpFoundation\JsonResponse; @@ -25,54 +26,16 @@ */ class ValidationsController extends AbstractController { - - /** - * @var ValidationsStorage - */ - private $storage; - - /** - * Helper class to validate arguments. - * - * @var ValidatorArgumentsService - */ - private $valArgsService; - - /** - * @var MimeTypeGuesserService - */ - private $mimeTypeGuesserService; - - /** - * @var Serializer - */ - private $serializer; - - /** - * @var ValidationRepository - */ - private $repository; - - /** - * @var FilesystemOperator - */ - private $dataStorage; - public function __construct( - ValidationRepository $repository, - SerializerInterface $serializer, - ValidationsStorage $storage, - FilesystemOperator $dataStorage, - ValidatorArgumentsService $valArgsService, - MimeTypeGuesserService $mimeTypeGuesserService - ) - { - $this->repository = $repository; - $this->storage = $storage; - $this->dataStorage = $dataStorage; - $this->valArgsService = $valArgsService; - $this->mimeTypeGuesserService = $mimeTypeGuesserService; - $this->serializer = $serializer; + private ValidationRepository $repository, + private SerializerInterface $serializer, + private ValidationsStorage $storage, + private FilesystemOperator $dataStorage, + private ValidatorArgumentsService $valArgsService, + private MimeTypeGuesserService $mimeTypeGuesserService, + private LoggerInterface $logger + ) { + } /** @@ -122,7 +85,7 @@ public function readConsole($uid) throw new ApiException("Validation has been archived", Response::HTTP_FORBIDDEN); } - $validationDirectory = $this->storage->getDirectory($validation) ; + $validationDirectory = $this->storage->getDirectory($validation); $filepath = $validationDirectory . '/validator-debug.log'; $content = $this->dataStorage->read($filepath); @@ -152,8 +115,8 @@ public function getValidationCsv($uid, CsvReportWriter $csvWriter) $csvWriter->write($validation); }); $response->headers->set('Content-Type', 'application/force-download'); - $filename = $uid.'-results.csv'; - $response->headers->set('Content-Disposition', 'attachment; filename="'.$filename.'"'); + $filename = $uid . '-results.csv'; + $response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"'); return $response; } @@ -175,6 +138,12 @@ public function uploadDataset(Request $request) if (!$file) { throw new ApiException("Argument [dataset] is missing", Response::HTTP_BAD_REQUEST); } + + $this->logger->info('handle new upload...',[ + 'path_name' => $file->getPathName(), + 'client_original_name' => $file->getClientOriginalName() + ]); + /* * Ensure that input file is a ZIP file. */ @@ -193,11 +162,11 @@ public function uploadDataset(Request $request) // Save file to storage $uploadDirectory = $validation->getUid() . '/upload/'; - if (! $this->dataStorage->directoryExists($uploadDirectory)) { + if (!$this->dataStorage->directoryExists($uploadDirectory)) { $this->dataStorage->createDirectory($uploadDirectory); } - $fileLocation = $uploadDirectory . $validation->getDatasetName().'.zip'; - if ($this->dataStorage->fileExists($fileLocation)){ + $fileLocation = $uploadDirectory . $validation->getDatasetName() . '.zip'; + if ($this->dataStorage->fileExists($fileLocation)) { $this->dataStorage->delete($fileLocation); } $stream = fopen($file->getRealPath(), 'r+'); @@ -275,13 +244,13 @@ public function deleteValidation($uid) $em->flush(); $fs = new FileSystem(); - $validationDirectory = $this->storage->getDirectory($validation) ; + $validationDirectory = $this->storage->getDirectory($validation); if ($fs->exists($validationDirectory)) { $fs->remove($validationDirectory); } // Delete from storage - if ($this->dataStorage->directoryExists($validationDirectory)){ + if ($this->dataStorage->directoryExists($validationDirectory)) { $this->dataStorage->deleteDirectory($validationDirectory); } @@ -314,7 +283,7 @@ public function downloadNormalizedData($uid) throw new ApiException("Validation hasn't been executed yet", Response::HTTP_FORBIDDEN); } - $validationDirectory = $this->storage->getDirectory($validation) ; + $validationDirectory = $this->storage->getDirectory($validation); $zipFilepath = $validationDirectory . '/validation/' . $validation->getDatasetName() . '.zip'; return $this->getDownloadResponse($zipFilepath, $validation->getDatasetName() . "-normalized.zip"); } @@ -337,7 +306,7 @@ public function downloadSourceData($uid) throw new ApiException("Validation has been archived", Response::HTTP_FORBIDDEN); } - $uploadDirectory = $validation->getUid() . '/upload/' ; + $uploadDirectory = $validation->getUid() . '/upload/'; $zipFilepath = $uploadDirectory . $validation->getDatasetName() . '.zip'; return $this->getDownloadResponse($zipFilepath, $validation->getDatasetName() . "-source.zip"); } @@ -351,7 +320,7 @@ public function downloadSourceData($uid) */ private function getDownloadResponse($filepath, $filename) { - if (! $this->dataStorage->has($filepath)) { + if (!$this->dataStorage->has($filepath)) { throw new ApiException("Requested files not found for this validation", Response::HTTP_FORBIDDEN); } @@ -361,7 +330,8 @@ private function getDownloadResponse($filepath, $filename) fpassthru($stream); exit(); }, 200, [ - 'Content-Transfer-Encoding', 'binary', + 'Content-Transfer-Encoding', + 'binary', 'Content-Type' => 'application/zip', 'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), 'Content-Length' => fstat($stream)['size']