From 2e67e070a1aa05d9766353a47b5f72e424c747f3 Mon Sep 17 00:00:00 2001 From: Samuel Gfeller Date: Thu, 18 Jan 2024 18:45:25 +0100 Subject: [PATCH] Renamed JsonEncoder and Renderer folder [SLE-192] --- config/container.php | 2 +- config/defaults.php | 5 ++- config/functions.php | 2 +- config/middleware.php | 4 +- .../Ajax/AccountUnlockProcessAction.php | 2 +- .../Authentication/Ajax/LoginSubmitAction.php | 4 +- .../Ajax/NewPasswordResetSubmitAction.php | 4 +- .../PasswordForgottenEmailSubmitAction.php | 4 +- .../Ajax/RegisterVerifyProcessAction.php | 2 +- .../Authentication/Page/LoginPageAction.php | 4 +- .../Authentication/Page/LogoutPageAction.php | 2 +- .../Page/PasswordResetPageAction.php | 2 +- .../Client/Ajax/ApiClientCreateAction.php | 8 ++-- .../Action/Client/Ajax/ClientCreateAction.php | 8 ++-- .../Action/Client/Ajax/ClientDeleteAction.php | 8 ++-- .../Client/Ajax/ClientFetchListAction.php | 8 ++-- .../Action/Client/Ajax/ClientUpdateAction.php | 8 ++-- ...chDropdownOptionsForClientCreateAction.php | 8 ++-- .../Client/Page/ClientListPageAction.php | 2 +- .../Client/Page/ClientReadPageAction.php | 2 +- .../Action/Common/TranslateAction.php | 8 ++-- .../Action/Dashboard/DashboardPageAction.php | 2 +- .../DashboardTogglePanelProcessAction.php | 6 +-- .../Action/Note/Ajax/NoteCreateAction.php | 8 ++-- .../Action/Note/Ajax/NoteDeleteAction.php | 8 ++-- .../Action/Note/Ajax/NoteFetchListAction.php | 8 ++-- .../Action/Note/Ajax/NoteUpdateAction.php | 8 ++-- .../Action/Note/Page/NoteReadPageAction.php | 2 +- ...etchDropdownOptionsForUserCreateAction.php | 6 +-- .../User/Ajax/PasswordChangeSubmitAction.php | 6 +-- .../User/Ajax/UserActivityFetchListAction.php | 6 +-- .../Action/User/Ajax/UserCreateAction.php | 8 ++-- .../Action/User/Ajax/UserDeleteAction.php | 8 ++-- .../Action/User/Ajax/UserFetchListAction.php | 6 +-- .../Action/User/Ajax/UserUpdateAction.php | 8 ++-- .../Action/User/Page/UserListPageAction.php | 2 +- .../Action/User/Page/UserReadPageAction.php | 2 +- .../ForbiddenExceptionMiddleware.php | 6 +-- .../InvalidOperationExceptionMiddleware.php | 6 +-- ...ddleware.php => PhpRendererMiddleware.php} | 20 ++++----- .../UserAuthenticationMiddleware.php | 8 ++-- .../ValidationExceptionMiddleware.php | 6 +-- .../JsonEncoder.php} | 6 +-- .../RedirectHandler.php | 2 +- .../TemplateRenderer.php | 11 ++--- .../Utility/JsImportCacheBuster.php | 18 ++++---- templates/layout.html.php | 44 +++++++++---------- templates/layout/assets.html.php | 17 +++---- 48 files changed, 161 insertions(+), 174 deletions(-) rename src/Application/Middleware/{PhpViewExtensionMiddleware.php => PhpRendererMiddleware.php} (81%) rename src/Application/{Responder/JsonResponder.php => Renderer/JsonEncoder.php} (88%) rename src/Application/{Responder => Renderer}/RedirectHandler.php (98%) rename src/Application/{Responder => Renderer}/TemplateRenderer.php (95%) diff --git a/config/container.php b/config/container.php index 29c6d42e..0f929994 100644 --- a/config/container.php +++ b/config/container.php @@ -142,7 +142,7 @@ $settings = $container->get('settings'); $rendererSettings = $settings['renderer']; - /** Global attributes are set in @see PhpViewExtensionMiddleware */ + /** Global attributes are set in @see PhpRendererMiddleware */ return new PhpRenderer($rendererSettings['path']); }, diff --git a/config/defaults.php b/config/defaults.php index 364072f2..ffc48fed 100644 --- a/config/defaults.php +++ b/config/defaults.php @@ -37,12 +37,13 @@ $settings['root_dir'] = dirname(__DIR__, 1); $settings['deployment'] = [ - // Version `null` or string. If JsImportCacheBuster is enabled, `null` removes all query param versions from js imports + // Version string or null. + // If JsImportCacheBuster is enabled, `null` removes all query param versions from js imports 'version' => '0.4.0', // When true, JsImportCacheBuster is enabled and goes through all js files and changes the version number from the imports 'update_js_imports_version' => true, // Disable in prod - 'assets_path' => $settings['root_dir'] . '/public/assets', + 'asset_path' => $settings['root_dir'] . '/public/assets', ]; $settings['public'] = [ diff --git a/config/functions.php b/config/functions.php index df1bc82e..2343138f 100644 --- a/config/functions.php +++ b/config/functions.php @@ -23,7 +23,7 @@ function html(?string $text = null): string * @param mixed ...$context Optional elements that should be inserted in the string with placeholders. * The function can be called like this: * __('The %s contains %d monkeys and %d birds.', 'tree', 5, 3); - * With the argument unpacking operator ...$context, they are accessible within the function as an array. + * With the argument unpacking operator ...$context, the arguments are accessible within the function as an array. * * @return string The translated string. */ diff --git a/config/middleware.php b/config/middleware.php index 284a3633..9c7da44a 100644 --- a/config/middleware.php +++ b/config/middleware.php @@ -3,7 +3,7 @@ use App\Application\Middleware\ForbiddenExceptionMiddleware; use App\Application\Middleware\InvalidOperationExceptionMiddleware; use App\Application\Middleware\NonFatalErrorHandlerMiddleware; -use App\Application\Middleware\PhpViewExtensionMiddleware; +use App\Application\Middleware\PhpRendererMiddleware; use App\Application\Middleware\ValidationExceptionMiddleware; use Odan\Session\Middleware\SessionStartMiddleware; use Selective\BasePath\BasePathMiddleware; @@ -21,7 +21,7 @@ // * Put everything possible before PhpViewExtensionMiddleware as if there is an error in a middleware, // * the error page (and layout as well as everything else) needs this middleware loaded to work. - $app->add(PhpViewExtensionMiddleware::class); + $app->add(PhpRendererMiddleware::class); // Retrieve and store ip address, user agent and user id (has to be BEFORE SessionStartMiddleware as it is using it // but after PhpViewExtensionMiddleware as it needs the user id) diff --git a/src/Application/Action/Authentication/Ajax/AccountUnlockProcessAction.php b/src/Application/Action/Authentication/Ajax/AccountUnlockProcessAction.php index d8c22238..a381ecc8 100644 --- a/src/Application/Action/Authentication/Ajax/AccountUnlockProcessAction.php +++ b/src/Application/Action/Authentication/Ajax/AccountUnlockProcessAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Authentication\Ajax; -use App\Application\Responder\RedirectHandler; +use App\Application\Renderer\RedirectHandler; use App\Domain\Authentication\Exception\InvalidTokenException; use App\Domain\Authentication\Exception\UserAlreadyVerifiedException; use App\Domain\Authentication\Service\AccountUnlockTokenVerifier; diff --git a/src/Application/Action/Authentication/Ajax/LoginSubmitAction.php b/src/Application/Action/Authentication/Ajax/LoginSubmitAction.php index 511330d9..4cd7935e 100644 --- a/src/Application/Action/Authentication/Ajax/LoginSubmitAction.php +++ b/src/Application/Action/Authentication/Ajax/LoginSubmitAction.php @@ -2,8 +2,8 @@ namespace App\Application\Action\Authentication\Ajax; -use App\Application\Responder\RedirectHandler; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\RedirectHandler; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Authentication\Exception\InvalidCredentialsException; use App\Domain\Authentication\Exception\UnableToLoginStatusNotActiveException; use App\Domain\Authentication\Service\LoginVerifier; diff --git a/src/Application/Action/Authentication/Ajax/NewPasswordResetSubmitAction.php b/src/Application/Action/Authentication/Ajax/NewPasswordResetSubmitAction.php index c8f12337..b94fbef0 100644 --- a/src/Application/Action/Authentication/Ajax/NewPasswordResetSubmitAction.php +++ b/src/Application/Action/Authentication/Ajax/NewPasswordResetSubmitAction.php @@ -2,8 +2,8 @@ namespace App\Application\Action\Authentication\Ajax; -use App\Application\Responder\RedirectHandler; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\RedirectHandler; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Authentication\Exception\InvalidTokenException; use App\Domain\Authentication\Service\PasswordResetterWithToken; use App\Domain\Validation\ValidationException; diff --git a/src/Application/Action/Authentication/Ajax/PasswordForgottenEmailSubmitAction.php b/src/Application/Action/Authentication/Ajax/PasswordForgottenEmailSubmitAction.php index 661f9ee6..e38b4d62 100644 --- a/src/Application/Action/Authentication/Ajax/PasswordForgottenEmailSubmitAction.php +++ b/src/Application/Action/Authentication/Ajax/PasswordForgottenEmailSubmitAction.php @@ -2,8 +2,8 @@ namespace App\Application\Action\Authentication\Ajax; -use App\Application\Responder\RedirectHandler; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\RedirectHandler; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Authentication\Service\PasswordRecoveryEmailSender; use App\Domain\Exception\DomainRecordNotFoundException; use App\Domain\Security\Exception\SecurityException; diff --git a/src/Application/Action/Authentication/Ajax/RegisterVerifyProcessAction.php b/src/Application/Action/Authentication/Ajax/RegisterVerifyProcessAction.php index 2d545b12..68d522d3 100644 --- a/src/Application/Action/Authentication/Ajax/RegisterVerifyProcessAction.php +++ b/src/Application/Action/Authentication/Ajax/RegisterVerifyProcessAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Authentication\Ajax; -use App\Application\Responder\RedirectHandler; +use App\Application\Renderer\RedirectHandler; use App\Domain\Authentication\Exception\InvalidTokenException; use App\Domain\Authentication\Exception\UserAlreadyVerifiedException; use App\Domain\Authentication\Service\RegisterTokenVerifier; diff --git a/src/Application/Action/Authentication/Page/LoginPageAction.php b/src/Application/Action/Authentication/Page/LoginPageAction.php index 03abd449..450be7c5 100644 --- a/src/Application/Action/Authentication/Page/LoginPageAction.php +++ b/src/Application/Action/Authentication/Page/LoginPageAction.php @@ -2,8 +2,8 @@ namespace App\Application\Action\Authentication\Page; -use App\Application\Responder\RedirectHandler; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\RedirectHandler; +use App\Application\Renderer\TemplateRenderer; use Odan\Session\SessionInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; diff --git a/src/Application/Action/Authentication/Page/LogoutPageAction.php b/src/Application/Action/Authentication/Page/LogoutPageAction.php index 7fa2de6b..8788ca04 100644 --- a/src/Application/Action/Authentication/Page/LogoutPageAction.php +++ b/src/Application/Action/Authentication/Page/LogoutPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Authentication\Page; -use App\Application\Responder\RedirectHandler; +use App\Application\Renderer\RedirectHandler; use Odan\Session\SessionInterface; use Odan\Session\SessionManagerInterface; use Psr\Http\Message\ResponseInterface as Response; diff --git a/src/Application/Action/Authentication/Page/PasswordResetPageAction.php b/src/Application/Action/Authentication/Page/PasswordResetPageAction.php index 5e6925d9..0e27ac07 100644 --- a/src/Application/Action/Authentication/Page/PasswordResetPageAction.php +++ b/src/Application/Action/Authentication/Page/PasswordResetPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Authentication\Page; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\TemplateRenderer; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as ServerRequest; use Psr\Log\LoggerInterface; diff --git a/src/Application/Action/Client/Ajax/ApiClientCreateAction.php b/src/Application/Action/Client/Ajax/ApiClientCreateAction.php index fd565641..b005b30a 100644 --- a/src/Application/Action/Client/Ajax/ApiClientCreateAction.php +++ b/src/Application/Action/Client/Ajax/ApiClientCreateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Client\Service\ClientCreatorFromApi; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -13,7 +13,7 @@ final readonly class ApiClientCreateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private ClientCreatorFromApi $clientCreatorFromClientSubmit, ) { } @@ -37,10 +37,10 @@ public function __invoke( $insertId = $this->clientCreatorFromClientSubmit->createClientFromClientSubmit($clientValues); if (0 !== $insertId) { - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null], 201); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201); } - $response = $this->jsonResponder->respondWithJson($response, [ + $response = $this->jsonEncoder->encodeAndAddToResponse($response, [ 'status' => 'warning', 'message' => 'Client not created', ]); diff --git a/src/Application/Action/Client/Ajax/ClientCreateAction.php b/src/Application/Action/Client/Ajax/ClientCreateAction.php index 72386c70..99360abc 100644 --- a/src/Application/Action/Client/Ajax/ClientCreateAction.php +++ b/src/Application/Action/Client/Ajax/ClientCreateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Client\Service\ClientCreator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -10,7 +10,7 @@ final readonly class ClientCreateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private ClientCreator $clientCreator, ) { } @@ -35,9 +35,9 @@ public function __invoke( $insertId = $this->clientCreator->createClient($clientValues); if (0 !== $insertId) { - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null], 201); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201); } - $response = $this->jsonResponder->respondWithJson($response, [ + $response = $this->jsonEncoder->encodeAndAddToResponse($response, [ 'status' => 'warning', 'message' => 'Client not created', ]); diff --git a/src/Application/Action/Client/Ajax/ClientDeleteAction.php b/src/Application/Action/Client/Ajax/ClientDeleteAction.php index 60046138..833fa3c1 100644 --- a/src/Application/Action/Client/Ajax/ClientDeleteAction.php +++ b/src/Application/Action/Client/Ajax/ClientDeleteAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Client\Service\ClientDeleter; use Odan\Session\SessionInterface; use Psr\Http\Message\ResponseInterface; @@ -11,7 +11,7 @@ final readonly class ClientDeleteAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private ClientDeleter $clientDeleter, private SessionInterface $session, ) { @@ -42,10 +42,10 @@ public function __invoke( // Add flash here as user gets redirected to client list after deletion $flash->add('success', __('Successfully deleted client.')); - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null]); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null]); } - $response = $this->jsonResponder->respondWithJson( + $response = $this->jsonEncoder->encodeAndAddToResponse( $response, ['status' => 'warning', 'message' => 'Client not deleted.'] ); diff --git a/src/Application/Action/Client/Ajax/ClientFetchListAction.php b/src/Application/Action/Client/Ajax/ClientFetchListAction.php index dcb66004..eebd4825 100644 --- a/src/Application/Action/Client/Ajax/ClientFetchListAction.php +++ b/src/Application/Action/Client/Ajax/ClientFetchListAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Client\Exception\InvalidClientFilterException; use App\Domain\Client\Service\ClientFinderWithFilter; use App\Test\Integration\Client\ClientListActionTest; @@ -13,7 +13,7 @@ final readonly class ClientFetchListAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private ClientFinderWithFilter $clientFilterFinder, ) { } @@ -36,9 +36,9 @@ public function __invoke( // Retrieve clients with given filter values (or none) $clientResultCollection = $this->clientFilterFinder->findClientsWithFilter($request->getQueryParams()); - return $this->jsonResponder->respondWithJson($response, $clientResultCollection); + return $this->jsonEncoder->encodeAndAddToResponse($response, $clientResultCollection); } catch (InvalidClientFilterException $invalidClientFilterException) { - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, /** @see ClientListActionTest::testClientListActionInvalidFilters() */ [ diff --git a/src/Application/Action/Client/Ajax/ClientUpdateAction.php b/src/Application/Action/Client/Ajax/ClientUpdateAction.php index 3511f6f7..9b2cb922 100644 --- a/src/Application/Action/Client/Ajax/ClientUpdateAction.php +++ b/src/Application/Action/Client/Ajax/ClientUpdateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Client\Service\ClientUpdater; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -10,7 +10,7 @@ final readonly class ClientUpdateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private ClientUpdater $clientUpdater, ) { } @@ -34,12 +34,12 @@ public function __invoke( $updateData = $this->clientUpdater->updateClient($clientId, $clientValues); if ($updateData['updated']) { - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, ['status' => 'success', 'data' => $updateData['data']] ); } - $response = $this->jsonResponder->respondWithJson($response, [ + $response = $this->jsonEncoder->encodeAndAddToResponse($response, [ 'status' => 'warning', 'message' => 'The client was not updated.', 'data' => $updateData['data'], diff --git a/src/Application/Action/Client/Ajax/FetchDropdownOptionsForClientCreateAction.php b/src/Application/Action/Client/Ajax/FetchDropdownOptionsForClientCreateAction.php index 8be8bec1..271a4224 100644 --- a/src/Application/Action/Client/Ajax/FetchDropdownOptionsForClientCreateAction.php +++ b/src/Application/Action/Client/Ajax/FetchDropdownOptionsForClientCreateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Client\Exception\InvalidClientFilterException; use App\Domain\Client\Service\ClientUtilFinder; use Fig\Http\Message\StatusCodeInterface; @@ -12,7 +12,7 @@ readonly class FetchDropdownOptionsForClientCreateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private ClientUtilFinder $clientUtilFinder, ) { } @@ -34,7 +34,7 @@ public function __invoke( try { $dropdownOptions = $this->clientUtilFinder->findClientDropdownValues(); } catch (InvalidClientFilterException $invalidClientFilterException) { - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, // Response format tested in PostFilterProvider.php [ @@ -45,6 +45,6 @@ public function __invoke( ); } - return $this->jsonResponder->respondWithJson($response, $dropdownOptions); + return $this->jsonEncoder->encodeAndAddToResponse($response, $dropdownOptions); } } diff --git a/src/Application/Action/Client/Page/ClientListPageAction.php b/src/Application/Action/Client/Page/ClientListPageAction.php index 8fae2dee..1cd6195e 100644 --- a/src/Application/Action/Client/Page/ClientListPageAction.php +++ b/src/Application/Action/Client/Page/ClientListPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Page; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Authorization\Privilege; use App\Domain\Client\Service\Authorization\ClientPermissionVerifier; use App\Domain\Client\Service\ClientListFilter\ClientListFilterChipProvider; diff --git a/src/Application/Action/Client/Page/ClientReadPageAction.php b/src/Application/Action/Client/Page/ClientReadPageAction.php index 51da7f47..3c544370 100644 --- a/src/Application/Action/Client/Page/ClientReadPageAction.php +++ b/src/Application/Action/Client/Page/ClientReadPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Client\Page; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Client\Service\ClientFinder; use App\Domain\Client\Service\ClientUtilFinder; use Psr\Http\Message\ResponseInterface; diff --git a/src/Application/Action/Common/TranslateAction.php b/src/Application/Action/Common/TranslateAction.php index 812411b5..00508c50 100644 --- a/src/Application/Action/Common/TranslateAction.php +++ b/src/Application/Action/Common/TranslateAction.php @@ -2,14 +2,14 @@ namespace App\Application\Action\Common; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; final readonly class TranslateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, ) { } @@ -30,9 +30,9 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res $translatedStrings[$string] = __($string); } - return $this->jsonResponder->respondWithJson($response, $translatedStrings); + return $this->jsonEncoder->encodeAndAddToResponse($response, $translatedStrings); } - return $this->jsonResponder->respondWithJson($response, ['error' => 'Wrong request body format.'], 400); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['error' => 'Wrong request body format.'], 400); } } diff --git a/src/Application/Action/Dashboard/DashboardPageAction.php b/src/Application/Action/Dashboard/DashboardPageAction.php index 2f9f6070..c9cbfb84 100644 --- a/src/Application/Action/Dashboard/DashboardPageAction.php +++ b/src/Application/Action/Dashboard/DashboardPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Dashboard; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Dashboard\DashboardPanelProvider; use App\Domain\FilterSetting\FilterModule; use App\Domain\FilterSetting\FilterSettingFinder; diff --git a/src/Application/Action/Dashboard/DashboardTogglePanelProcessAction.php b/src/Application/Action/Dashboard/DashboardTogglePanelProcessAction.php index c9f2c526..f9e5f2d7 100644 --- a/src/Application/Action/Dashboard/DashboardTogglePanelProcessAction.php +++ b/src/Application/Action/Dashboard/DashboardTogglePanelProcessAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Dashboard; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Application\Validation\MalformedRequestBodyChecker; use App\Domain\FilterSetting\FilterModule; use App\Domain\FilterSetting\FilterSettingSaver; @@ -14,7 +14,7 @@ final readonly class DashboardTogglePanelProcessAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private SessionInterface $session, private FilterSettingSaver $filterSettingSaver, private MalformedRequestBodyChecker $malformedRequestBodyChecker, @@ -43,7 +43,7 @@ public function __invoke( FilterModule::DASHBOARD_PANEL ); - return $this->jsonResponder->respondWithJson($response, ['success' => true]); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['success' => true]); } $flash = $this->session->getFlash(); $flash->add('error', __('Malformed request body syntax. Please contact an administrator.')); diff --git a/src/Application/Action/Note/Ajax/NoteCreateAction.php b/src/Application/Action/Note/Ajax/NoteCreateAction.php index cc60b612..06b114c4 100644 --- a/src/Application/Action/Note/Ajax/NoteCreateAction.php +++ b/src/Application/Action/Note/Ajax/NoteCreateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Note\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Note\Service\NoteCreator; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -10,7 +10,7 @@ final readonly class NoteCreateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private NoteCreator $noteCreator, ) { } @@ -36,7 +36,7 @@ public function __invoke( if (0 !== $noteCreationData['note_id']) { // camelCase according to Google recommendation - return $this->jsonResponder->respondWithJson($response, [ + return $this->jsonEncoder->encodeAndAddToResponse($response, [ 'status' => 'success', 'data' => [ 'userFullName' => $noteCreationData['user_full_name'], @@ -45,7 +45,7 @@ public function __invoke( ], ], 201); } - $response = $this->jsonResponder->respondWithJson($response, [ + $response = $this->jsonEncoder->encodeAndAddToResponse($response, [ 'status' => 'warning', 'message' => 'Note not created', ]); diff --git a/src/Application/Action/Note/Ajax/NoteDeleteAction.php b/src/Application/Action/Note/Ajax/NoteDeleteAction.php index 3bf1ef1d..acf7122f 100644 --- a/src/Application/Action/Note/Ajax/NoteDeleteAction.php +++ b/src/Application/Action/Note/Ajax/NoteDeleteAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Note\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Note\Service\NoteDeleter; use Odan\Session\SessionInterface; use Psr\Http\Message\ResponseInterface; @@ -11,7 +11,7 @@ final readonly class NoteDeleteAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private NoteDeleter $noteDeleter, private SessionInterface $session, ) { @@ -37,10 +37,10 @@ public function __invoke( $deleted = $this->noteDeleter->deleteNote($noteId); if ($deleted) { - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null]); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null]); } - $response = $this->jsonResponder->respondWithJson( + $response = $this->jsonEncoder->encodeAndAddToResponse( $response, ['status' => 'warning', 'message' => 'Note not deleted.'] ); diff --git a/src/Application/Action/Note/Ajax/NoteFetchListAction.php b/src/Application/Action/Note/Ajax/NoteFetchListAction.php index 1507e84c..d2f0860e 100644 --- a/src/Application/Action/Note/Ajax/NoteFetchListAction.php +++ b/src/Application/Action/Note/Ajax/NoteFetchListAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Note\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Note\Exception\InvalidNoteFilterException; use App\Domain\Note\Service\NoteFilterFinder; use Fig\Http\Message\StatusCodeInterface; @@ -12,7 +12,7 @@ final readonly class NoteFetchListAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private NoteFilterFinder $noteFilterFinder, ) { } @@ -35,7 +35,7 @@ public function __invoke( // Retrieve notes with given filter values (or none) $filteredNotes = $this->noteFilterFinder->findNotesWithFilter($request->getQueryParams()); } catch (InvalidNoteFilterException $invalidNoteFilterException) { - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, // Response format tested in NoteFilterProvider.php [ @@ -46,6 +46,6 @@ public function __invoke( ); } - return $this->jsonResponder->respondWithJson($response, $filteredNotes); + return $this->jsonEncoder->encodeAndAddToResponse($response, $filteredNotes); } } diff --git a/src/Application/Action/Note/Ajax/NoteUpdateAction.php b/src/Application/Action/Note/Ajax/NoteUpdateAction.php index 47e1c678..5f7895e1 100644 --- a/src/Application/Action/Note/Ajax/NoteUpdateAction.php +++ b/src/Application/Action/Note/Ajax/NoteUpdateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Note\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Note\Service\NoteUpdater; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -10,7 +10,7 @@ final readonly class NoteUpdateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private NoteUpdater $noteUpdater, ) { } @@ -35,9 +35,9 @@ public function __invoke( $updated = $this->noteUpdater->updateNote($noteIdToChange, $noteValues); if ($updated) { - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null]); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null]); } - $response = $this->jsonResponder->respondWithJson($response, [ + $response = $this->jsonEncoder->encodeAndAddToResponse($response, [ 'status' => 'warning', 'message' => 'The note was not updated.', ]); diff --git a/src/Application/Action/Note/Page/NoteReadPageAction.php b/src/Application/Action/Note/Page/NoteReadPageAction.php index f0394ec9..996ce507 100644 --- a/src/Application/Action/Note/Page/NoteReadPageAction.php +++ b/src/Application/Action/Note/Page/NoteReadPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\Note\Page; -use App\Application\Responder\RedirectHandler; +use App\Application\Renderer\RedirectHandler; use App\Domain\Note\Service\NoteFinder; use Odan\Session\SessionInterface; use Psr\Http\Message\ResponseInterface; diff --git a/src/Application/Action/User/Ajax/FetchDropdownOptionsForUserCreateAction.php b/src/Application/Action/User/Ajax/FetchDropdownOptionsForUserCreateAction.php index 43182236..3fad003c 100644 --- a/src/Application/Action/User/Ajax/FetchDropdownOptionsForUserCreateAction.php +++ b/src/Application/Action/User/Ajax/FetchDropdownOptionsForUserCreateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\User\Service\UserUtilFinder; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -10,7 +10,7 @@ readonly class FetchDropdownOptionsForUserCreateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserUtilFinder $userUtilFinder, ) { } @@ -31,6 +31,6 @@ public function __invoke( ): ResponseInterface { $dropdownOptions = $this->userUtilFinder->findUserDropdownValues(); - return $this->jsonResponder->respondWithJson($response, $dropdownOptions); + return $this->jsonEncoder->encodeAndAddToResponse($response, $dropdownOptions); } } diff --git a/src/Application/Action/User/Ajax/PasswordChangeSubmitAction.php b/src/Application/Action/User/Ajax/PasswordChangeSubmitAction.php index 09b8459a..96631d60 100644 --- a/src/Application/Action/User/Ajax/PasswordChangeSubmitAction.php +++ b/src/Application/Action/User/Ajax/PasswordChangeSubmitAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Authentication\Service\PasswordChanger; use Odan\Session\SessionInterface; use Odan\Session\SessionManagerInterface; @@ -15,7 +15,7 @@ readonly class PasswordChangeSubmitAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private SessionManagerInterface $sessionManager, private SessionInterface $session, private PasswordChanger $passwordChanger @@ -43,6 +43,6 @@ public function __invoke(ServerRequest $request, Response $response, array $args $this->sessionManager->regenerateId(); } - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null]); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null]); } } diff --git a/src/Application/Action/User/Ajax/UserActivityFetchListAction.php b/src/Application/Action/User/Ajax/UserActivityFetchListAction.php index acdbaa9d..815e72da 100644 --- a/src/Application/Action/User/Ajax/UserActivityFetchListAction.php +++ b/src/Application/Action/User/Ajax/UserActivityFetchListAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\FilterSetting\FilterModule; use App\Domain\FilterSetting\FilterSettingSaver; use App\Domain\UserActivity\Service\UserActivityFinder; @@ -12,7 +12,7 @@ readonly class UserActivityFetchListAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserActivityFinder $userActivityFinder, private FilterSettingSaver $filterSettingSaver, ) { @@ -47,6 +47,6 @@ public function __invoke( ); } - return $this->jsonResponder->respondWithJson($response, $userResultDataArray); + return $this->jsonEncoder->encodeAndAddToResponse($response, $userResultDataArray); } } diff --git a/src/Application/Action/User/Ajax/UserCreateAction.php b/src/Application/Action/User/Ajax/UserCreateAction.php index 38334156..5a937105 100644 --- a/src/Application/Action/User/Ajax/UserCreateAction.php +++ b/src/Application/Action/User/Ajax/UserCreateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\User\Service\UserCreator; use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as ServerRequest; @@ -13,7 +13,7 @@ { public function __construct( private LoggerInterface $logger, - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserCreator $userCreator, ) { } @@ -39,12 +39,12 @@ public function __invoke(ServerRequest $request, Response $response): Response $response = $response->withAddedHeader('Warning', 'The post could not be created'); } - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null], 201); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null], 201); } catch (TransportExceptionInterface $e) { // Flash message has to be added in the frontend as form is submitted via Ajax $this->logger->error('Mailer exception: ' . $e->getMessage()); - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, ['status' => 'error', 'message' => __('Email error. Please contact an administrator.')] ); diff --git a/src/Application/Action/User/Ajax/UserDeleteAction.php b/src/Application/Action/User/Ajax/UserDeleteAction.php index f95f72af..8ebffd03 100644 --- a/src/Application/Action/User/Ajax/UserDeleteAction.php +++ b/src/Application/Action/User/Ajax/UserDeleteAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\User\Service\UserDeleter; use Odan\Session\SessionInterface; use Odan\Session\SessionManagerInterface; @@ -12,7 +12,7 @@ final readonly class UserDeleteAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserDeleter $userDeleter, private SessionManagerInterface $sessionManager, private SessionInterface $session, @@ -58,10 +58,10 @@ public function __invoke( ); } - return $this->jsonResponder->respondWithJson($response, ['status' => 'success']); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success']); } - $response = $this->jsonResponder->respondWithJson( + $response = $this->jsonEncoder->encodeAndAddToResponse( $response, // response json body asserted in UserDeleteActionTest ['status' => 'warning', 'message' => 'User not deleted.'] diff --git a/src/Application/Action/User/Ajax/UserFetchListAction.php b/src/Application/Action/User/Ajax/UserFetchListAction.php index 1e0c27da..f99cf2d3 100644 --- a/src/Application/Action/User/Ajax/UserFetchListAction.php +++ b/src/Application/Action/User/Ajax/UserFetchListAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\User\Enum\UserStatus; use App\Domain\User\Service\UserFinder; use Psr\Http\Message\ResponseInterface; @@ -11,7 +11,7 @@ final readonly class UserFetchListAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserFinder $userFinder, ) { } @@ -36,7 +36,7 @@ public function __invoke( // $clientResultCollection = $this->clientFilterFinder->findClientsWithFilter($request->getQueryParams()); $userResultDataArray = $this->userFinder->findAllUsersResultDataForList(); - return $this->jsonResponder->respondWithJson($response, [ + return $this->jsonEncoder->encodeAndAddToResponse($response, [ 'userResultDataArray' => $userResultDataArray, 'statuses' => UserStatus::toTranslatedNamesArray(), ]); diff --git a/src/Application/Action/User/Ajax/UserUpdateAction.php b/src/Application/Action/User/Ajax/UserUpdateAction.php index 004f5892..c555fd4b 100644 --- a/src/Application/Action/User/Ajax/UserUpdateAction.php +++ b/src/Application/Action/User/Ajax/UserUpdateAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Ajax; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\User\Service\UserUpdater; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; @@ -10,7 +10,7 @@ final readonly class UserUpdateAction { public function __construct( - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserUpdater $userUpdater, ) { } @@ -35,11 +35,11 @@ public function __invoke( $updated = $this->userUpdater->updateUser($userIdToChange, $userValuesToChange); if ($updated) { - return $this->jsonResponder->respondWithJson($response, ['status' => 'success', 'data' => null]); + return $this->jsonEncoder->encodeAndAddToResponse($response, ['status' => 'success', 'data' => null]); } // If for example values didn't change - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, ['status' => 'warning', 'message' => 'User wasn\'t updated'] ); diff --git a/src/Application/Action/User/Page/UserListPageAction.php b/src/Application/Action/User/Page/UserListPageAction.php index 275e6125..d1c024cd 100644 --- a/src/Application/Action/User/Page/UserListPageAction.php +++ b/src/Application/Action/User/Page/UserListPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Page; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\TemplateRenderer; use App\Domain\User\Service\Authorization\UserPermissionVerifier; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; diff --git a/src/Application/Action/User/Page/UserReadPageAction.php b/src/Application/Action/User/Page/UserReadPageAction.php index a2fd29b6..b8b4a963 100644 --- a/src/Application/Action/User/Page/UserReadPageAction.php +++ b/src/Application/Action/User/Page/UserReadPageAction.php @@ -2,7 +2,7 @@ namespace App\Application\Action\User\Page; -use App\Application\Responder\TemplateRenderer; +use App\Application\Renderer\TemplateRenderer; use App\Domain\Exception\DomainRecordNotFoundException; use App\Domain\User\Enum\UserStatus; use App\Domain\User\Service\UserFinder; diff --git a/src/Application/Middleware/ForbiddenExceptionMiddleware.php b/src/Application/Middleware/ForbiddenExceptionMiddleware.php index 01aec163..6379d6e9 100644 --- a/src/Application/Middleware/ForbiddenExceptionMiddleware.php +++ b/src/Application/Middleware/ForbiddenExceptionMiddleware.php @@ -2,7 +2,7 @@ namespace App\Application\Middleware; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Authentication\Exception\ForbiddenException; use Fig\Http\Message\StatusCodeInterface; use Psr\Http\Message\ResponseFactoryInterface; @@ -15,7 +15,7 @@ { public function __construct( private ResponseFactoryInterface $responseFactory, - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, ) { } @@ -27,7 +27,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface // Create response (status code and header are added later) $response = $this->responseFactory->createResponse(); - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, [ 'status' => 'error', diff --git a/src/Application/Middleware/InvalidOperationExceptionMiddleware.php b/src/Application/Middleware/InvalidOperationExceptionMiddleware.php index f3055978..811296cf 100644 --- a/src/Application/Middleware/InvalidOperationExceptionMiddleware.php +++ b/src/Application/Middleware/InvalidOperationExceptionMiddleware.php @@ -3,7 +3,7 @@ namespace App\Application\Middleware; use App\Application\Data\UserNetworkSessionData; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Exception\InvalidOperationException; use Fig\Http\Message\StatusCodeInterface; use Psr\Http\Message\ResponseFactoryInterface; @@ -17,7 +17,7 @@ { public function __construct( private ResponseFactoryInterface $responseFactory, - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private UserNetworkSessionData $userNetworkSessionData, private LoggerInterface $logger, ) { @@ -35,7 +35,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface $request->getUri()->getPath() . ' with message: ' . $exception->getMessage() ); - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, [ 'status' => 'error', diff --git a/src/Application/Middleware/PhpViewExtensionMiddleware.php b/src/Application/Middleware/PhpRendererMiddleware.php similarity index 81% rename from src/Application/Middleware/PhpViewExtensionMiddleware.php rename to src/Application/Middleware/PhpRendererMiddleware.php index ef3c1ede..d8937a2e 100644 --- a/src/Application/Middleware/PhpViewExtensionMiddleware.php +++ b/src/Application/Middleware/PhpRendererMiddleware.php @@ -16,7 +16,7 @@ use Slim\Routing\RouteContext; use Slim\Views\PhpRenderer; -final class PhpViewExtensionMiddleware implements MiddlewareInterface +final class PhpRendererMiddleware implements MiddlewareInterface { private array $publicSettings; private bool $devSetting; @@ -36,10 +36,8 @@ public function __construct( $this->deploymentSettings = $settings->get('deployment'); } - public function process( - ServerRequestInterface $request, - RequestHandlerInterface $handler - ): ResponseInterface { + public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface + { $loggedInUserId = $this->session->get('user_id'); // The following has to work even with no connection to mysql to display the error page (layout needs those attr) $this->phpRenderer->setAttributes([ @@ -57,7 +55,10 @@ public function process( // Check and set user list authorization for "users" nav point if ($loggedInUserId) { - $this->checkAndSetUserListAuthorization($loggedInUserId); + // Check if the authenticated user is allowed to see user list and save the result to the session + $this->checkUserListAuthorization($loggedInUserId); + // Add the user list authorization as an attribute to the PhpRenderer + $this->phpRenderer->addAttribute('userListAuthorization', $this->session->get('isAllowedToSeeUserList')); } // Add version number to js imports @@ -69,11 +70,11 @@ public function process( } /** - * Check if the user is allowed to see the user list and set the result as an attribute for the PhpRenderer. + * Check if the user is allowed to see the user list and save the result to the session. * * @param int $loggedInUserId */ - private function checkAndSetUserListAuthorization(int $loggedInUserId): void + private function checkUserListAuthorization(int $loggedInUserId): void { // If the session already contains the information, the permission check can be skipped if ($this->session->get('isAllowedToSeeUserList') === null) { @@ -85,8 +86,5 @@ private function checkAndSetUserListAuthorization(int $loggedInUserId): void return; } } - - // Add the user list authorization as an attribute to the PhpRenderer - $this->phpRenderer->addAttribute('userListAuthorization', $this->session->get('isAllowedToSeeUserList')); } } diff --git a/src/Application/Middleware/UserAuthenticationMiddleware.php b/src/Application/Middleware/UserAuthenticationMiddleware.php index 0f44cc09..d28a0094 100644 --- a/src/Application/Middleware/UserAuthenticationMiddleware.php +++ b/src/Application/Middleware/UserAuthenticationMiddleware.php @@ -2,8 +2,8 @@ namespace App\Application\Middleware; -use App\Application\Responder\JsonResponder; -use App\Application\Responder\RedirectHandler; +use App\Application\Renderer\JsonEncoder; +use App\Application\Renderer\RedirectHandler; use App\Domain\User\Enum\UserStatus; use App\Domain\User\Service\UserFinder; use Odan\Session\SessionInterface; @@ -20,7 +20,7 @@ public function __construct( private SessionManagerInterface $sessionManager, private SessionInterface $session, - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, private RedirectHandler $redirectHandler, private RouteParserInterface $routeParser, private ResponseFactoryInterface $responseFactory, @@ -70,7 +70,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface // If it's a JSON request, return 401 with the login url and its possible query params if ($request->getHeaderLine('Content-Type') === 'application/json') { - return $this->jsonResponder->respondWithJson( + return $this->jsonEncoder->encodeAndAddToResponse( $response, ['loginUrl' => $this->routeParser->urlFor('login-page', [], $queryParams)], 401 diff --git a/src/Application/Middleware/ValidationExceptionMiddleware.php b/src/Application/Middleware/ValidationExceptionMiddleware.php index d4069d1a..baa8735e 100644 --- a/src/Application/Middleware/ValidationExceptionMiddleware.php +++ b/src/Application/Middleware/ValidationExceptionMiddleware.php @@ -2,7 +2,7 @@ namespace App\Application\Middleware; -use App\Application\Responder\JsonResponder; +use App\Application\Renderer\JsonEncoder; use App\Domain\Validation\ValidationException; use Psr\Http\Message\ResponseFactoryInterface; use Psr\Http\Message\ResponseInterface; @@ -14,7 +14,7 @@ { public function __construct( private ResponseFactoryInterface $responseFactory, - private JsonResponder $jsonResponder, + private JsonEncoder $jsonEncoder, ) { } @@ -33,7 +33,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface 'data' => ['errors' => $validationException->validationErrors], ]; - return $this->jsonResponder->respondWithJson($response, $responseData, 422); + return $this->jsonEncoder->encodeAndAddToResponse($response, $responseData, 422); } } } diff --git a/src/Application/Responder/JsonResponder.php b/src/Application/Renderer/JsonEncoder.php similarity index 88% rename from src/Application/Responder/JsonResponder.php rename to src/Application/Renderer/JsonEncoder.php index 9c6172fd..dc286dd5 100644 --- a/src/Application/Responder/JsonResponder.php +++ b/src/Application/Renderer/JsonEncoder.php @@ -1,10 +1,10 @@ phpRenderer->render($response, $template, $data); } @@ -84,9 +81,9 @@ public function renderOnValidationError( * @param array|null $preloadValues * @param array $queryParams same query params passed to page to be added again to form after validation error * + * @return ResponseInterface * @throws \Throwable * - * @return ResponseInterface */ public function respondWithFormThrottle( ResponseInterface $response, diff --git a/src/Infrastructure/Utility/JsImportCacheBuster.php b/src/Infrastructure/Utility/JsImportCacheBuster.php index 45d1d349..7785b60e 100644 --- a/src/Infrastructure/Utility/JsImportCacheBuster.php +++ b/src/Infrastructure/Utility/JsImportCacheBuster.php @@ -12,13 +12,13 @@ final class JsImportCacheBuster { private ?string $version; - private string $assetsPath; + private string $assetPath; public function __construct(Settings $settings) { $deploymentSettings = $settings->get('deployment'); $this->version = $deploymentSettings['version']; - $this->assetsPath = $deploymentSettings['assets_path']; + $this->assetPath = $deploymentSettings['asset_path']; } /** @@ -26,20 +26,18 @@ public function __construct(Settings $settings) * are modified so that the imports have the version number at the * end of the file name as query parameters to break cache on * version change. - * This is far from an ideal solution as all files are reloaded on the client, - * but it is good enough for now especially if function is called only on - * actual not too frequent changes or on development. - * Performance wise, this function takes 10 and 20ms when content is unchanged - * and between 30 and 50ms when content is replaced. + * This function is called in PhpRendererMiddleware only on dev env. + * Performance wise, this function takes between 10 and 20ms when content + * is unchanged and between 30 and 50ms when content is replaced. * * @return void */ public function addVersionToJsImports(): void { // $start = hrtime(true); - if (is_dir($this->assetsPath)) { + if (is_dir($this->assetPath)) { $rii = new RecursiveIteratorIterator( - new RecursiveDirectoryIterator($this->assetsPath, FilesystemIterator::SKIP_DOTS) + new RecursiveDirectoryIterator($this->assetPath, FilesystemIterator::SKIP_DOTS) ); foreach ($rii as $file) { @@ -81,7 +79,7 @@ public function addVersionToJsImports(): void } } } - // Divided by a million gets ms and a billion (+9) seconds + // Divided by a million gets milliseconds and a billion (+9) seconds // var_dump('Time used: ' . (hrtime(true) - $start) / 1e+6 . ' ms'); } } diff --git a/templates/layout.html.php b/templates/layout.html.php index 58c23bee..0ec6b924 100644 --- a/templates/layout.html.php +++ b/templates/layout.html.php @@ -32,21 +32,21 @@ 'assets/navbar/side-navbar.css', 'assets/general/page-component/flash-message/flash-message.css', ]; -$layoutJs = ['assets/navbar/navbar.js',]; -$layoutJsModules = ['assets/general/general-js/default.js',]; + $layoutJs = ['assets/navbar/navbar.js',]; + $layoutJsModules = ['assets/general/general-js/default.js',]; -// fetch() includes another template into the current template -// Include template which contains HTML to include assets -echo $this->fetch( - 'layout/assets.html.php', // Merge layout assets and from sub templates - [ - 'stylesheets' => array_merge($layoutCss, $css ?? []), - 'scripts' => array_merge($layoutJs, $js ?? []), - // The type="module" allows the use of import and export inside a JS file. - 'jsModules' => array_merge($layoutJsModules, $jsModules ?? []), - ] -); -?> + // fetch() includes another template in the current template + // Include template that renders the asset paths + echo $this->fetch( + 'layout/assets.html.php', + [ // Merge layout assets and assets required by templates (added via $this->addAttribute()) + 'stylesheets' => array_merge($layoutCss, $css ?? []), + 'scripts' => array_merge($layoutJs, $js ?? []), + // The type="module" allows the use of import and export inside a JS file. + 'jsModules' => array_merge($layoutJsModules, $jsModules ?? []), + ] + ); + ?> <?= $config['app_name'] ?> '; } // Javascript module files foreach ($jsModules ?? [] as $modulePath) { - // "assets/" not default since some paths could be external urls - // Default use of defer because it allows faster parsing and less bugs [SLE-77] - // If dev, then time is appended to break cache always (especially for mobile) echo ''; }