Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW UI] add middleware system and first middleware on update options page #1136

Merged
merged 11 commits into from
Jan 29, 2025
42 changes: 42 additions & 0 deletions classes/Router/Middleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\Router;

use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

class Middleware
{
/**
* @var UpgradeContainer
*/
protected $upgradeContainer;

public function __construct(UpgradeContainer $upgradeContainer)
{
$this->upgradeContainer = $upgradeContainer;
}

/**
* @param Routes::* $routeName
*
* @return Routes::*
*/
public function process(string $routeName): string
{
$route = RoutesConfig::ROUTES[$routeName];
$routeMiddlewares = $route['middleware'] ?? [];

$nextRoute = $routeName;

foreach ($routeMiddlewares as $middleware) {
$processedRoute = (new $middleware($this->upgradeContainer))->process();

if ($processedRoute !== null) {
$nextRoute = $processedRoute;
break;
}
}

return $nextRoute;
}
}
20 changes: 20 additions & 0 deletions classes/Router/Middlewares/AbstractMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\Router\Middlewares;

use PrestaShop\Module\AutoUpgrade\UpgradeContainer;

abstract class AbstractMiddleware
{
/**
* @var UpgradeContainer
*/
protected $upgradeContainer;

public function __construct(UpgradeContainer $upgradeContainer)
{
$this->upgradeContainer = $upgradeContainer;
}

abstract public function process(): ?string;
}
28 changes: 28 additions & 0 deletions classes/Router/Middlewares/LocalChannelXmlAndZipExist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\Router\Middlewares;

use PrestaShop\Module\AutoUpgrade\Router\Routes;

class LocalChannelXmlAndZipExist extends AbstractMiddleware
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The validation function does more than just check the presence of the zip/xml pair, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and no, it check validty only if we are on channel local.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for me in addition to validating the presence of files, it also validates the presence of versions and the match of this one. So the middleware doesn't really reflect what is being done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You think LocalChannelXmlAndZipAreValid is better ?

{
/**
* @return Routes::*|null
*
* @throws \Exception
*/
public function process(): ?string
{
$updateConfiguration = $this->upgradeContainer->getUpdateConfiguration();

if ($updateConfiguration->isChannelLocal()) {
$errors = $this->upgradeContainer->getLocalChannelConfigurationValidator()->validate($updateConfiguration->toArray());

if (!empty($errors)) {
return Routes::UPDATE_PAGE_VERSION_CHOICE;
}
}

return null;
}
}
14 changes: 14 additions & 0 deletions classes/Router/Middlewares/UpdateIsConfigured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\Router\Middlewares;

use PrestaShop\Module\AutoUpgrade\Parameters\UpgradeFileNames;
use PrestaShop\Module\AutoUpgrade\Router\Routes;

class UpdateIsConfigured extends AbstractMiddleware
{
public function process(): ?string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function process(): ?string
/**
* @return Routes::*|null
*/
public function process(): ?string

{
return $this->upgradeContainer->getFileStorage()->exists(UpgradeFileNames::UPDATE_CONFIG_FILENAME) ? null : Routes::UPDATE_PAGE_VERSION_CHOICE;
}
}
222 changes: 31 additions & 191 deletions classes/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@

namespace PrestaShop\Module\AutoUpgrade\Router;

use PrestaShop\Module\AutoUpgrade\Controller\Error404Controller;
use PrestaShop\Module\AutoUpgrade\Controller\ErrorReportController;
use PrestaShop\Module\AutoUpgrade\Controller\HomePageController;
use PrestaShop\Module\AutoUpgrade\Controller\LogsController;
use PrestaShop\Module\AutoUpgrade\Controller\RestorePageBackupSelectionController;
use PrestaShop\Module\AutoUpgrade\Controller\RestorePagePostRestoreController;
use PrestaShop\Module\AutoUpgrade\Controller\RestorePageRestoreController;
use PrestaShop\Module\AutoUpgrade\Controller\UpdatePageBackupController;
use PrestaShop\Module\AutoUpgrade\Controller\UpdatePageBackupOptionsController;
use PrestaShop\Module\AutoUpgrade\Controller\UpdatePagePostUpdateController;
use PrestaShop\Module\AutoUpgrade\Controller\UpdatePageUpdateController;
use PrestaShop\Module\AutoUpgrade\Controller\UpdatePageUpdateOptionsController;
use PrestaShop\Module\AutoUpgrade\Controller\UpdatePageVersionChoiceController;
use PrestaShop\Module\AutoUpgrade\UpgradeContainer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -56,181 +43,6 @@ public function __construct(UpgradeContainer $upgradeContainer)
$this->upgradeContainer = $upgradeContainer;
}

const ROUTES =
/* HOME PAGE */
[
Routes::HOME_PAGE => [
'controller' => HomePageController::class,
'method' => 'index',
],
Routes::HOME_PAGE_SUBMIT_FORM => [
'controller' => HomePageController::class,
'method' => 'submit',
],
/* UPDATE PAGE */
/* step: version choice */
Routes::UPDATE_PAGE_VERSION_CHOICE => [
'controller' => UpdatePageVersionChoiceController::class,
'method' => 'index',
],
Routes::UPDATE_STEP_VERSION_CHOICE => [
'controller' => UpdatePageVersionChoiceController::class,
'method' => 'step',
],
Routes::UPDATE_STEP_VERSION_CHOICE_SAVE_FORM => [
'controller' => UpdatePageVersionChoiceController::class,
'method' => 'save',
],
Routes::UPDATE_STEP_VERSION_CHOICE_SUBMIT_FORM => [
'controller' => UpdatePageVersionChoiceController::class,
'method' => 'submit',
],
Routes::UPDATE_STEP_VERSION_CHOICE_CORE_TEMPERED_FILES_DIALOG => [
'controller' => UpdatePageVersionChoiceController::class,
'method' => 'coreTemperedFilesDialog',
],
Routes::UPDATE_STEP_VERSION_CHOICE_THEME_TEMPERED_FILES_DIALOG => [
'controller' => UpdatePageVersionChoiceController::class,
'method' => 'themeTemperedFilesDialog',
],
/* step: update options */
Routes::UPDATE_PAGE_UPDATE_OPTIONS => [
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'index',
],
Routes::UPDATE_STEP_UPDATE_OPTIONS => [
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'step',
],
Routes::UPDATE_STEP_UPDATE_OPTIONS_SAVE_OPTION => [
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'saveOption',
],
Routes::UPDATE_STEP_UPDATE_OPTIONS_SUBMIT_FORM => [
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'submit',
],
/* step: backup */
Routes::UPDATE_PAGE_BACKUP_OPTIONS => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'index',
],
Routes::UPDATE_STEP_BACKUP_OPTIONS => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'step',
],
Routes::UPDATE_STEP_BACKUP_SAVE_OPTION => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'saveOption',
],
Routes::UPDATE_STEP_BACKUP_SUBMIT_BACKUP => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'submitBackup',
],
Routes::UPDATE_STEP_BACKUP_SUBMIT_UPDATE => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'submitUpdate',
],
Routes::UPDATE_STEP_BACKUP_CONFIRM_BACKUP => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'startBackup',
],
Routes::UPDATE_STEP_BACKUP_CONFIRM_UPDATE => [
'controller' => UpdatePageBackupOptionsController::class,
'method' => 'startUpdate',
],
Routes::UPDATE_PAGE_BACKUP => [
'controller' => UpdatePageBackupController::class,
'method' => 'index',
],
Routes::UPDATE_STEP_BACKUP => [
'controller' => UpdatePageBackupController::class,
'method' => 'step',
],
/* step: update */
Routes::UPDATE_PAGE_UPDATE => [
'controller' => UpdatePageUpdateController::class,
'method' => 'index',
],
Routes::UPDATE_STEP_UPDATE => [
'controller' => UpdatePageUpdateController::class,
'method' => 'step',
],
/* step: post update */
Routes::UPDATE_PAGE_POST_UPDATE => [
'controller' => UpdatePagePostUpdateController::class,
'method' => 'index',
],
Routes::UPDATE_STEP_POST_UPDATE => [
'controller' => UpdatePagePostUpdateController::class,
'method' => 'step',
],
/* RESTORE PAGE */
/* step: backup selection */
Routes::RESTORE_PAGE_BACKUP_SELECTION => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'index',
],
Routes::RESTORE_STEP_BACKUP_SELECTION => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'step',
],
Routes::RESTORE_STEP_BACKUP_SELECTION_SAVE_FORM => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'save',
],
Routes::RESTORE_STEP_BACKUP_SELECTION_SUBMIT_RESTORE_FORM => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'submitRestore',
],
Routes::RESTORE_STEP_BACKUP_SELECTION_CONFIRM_RESTORE_FORM => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'startRestore',
],
Routes::RESTORE_STEP_BACKUP_SELECTION_SUBMIT_DELETE_FORM => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'submitDelete',
],
Routes::RESTORE_STEP_BACKUP_SELECTION_CONFIRM_DELETE_FORM => [
'controller' => RestorePageBackupSelectionController::class,
'method' => 'confirmDelete',
],
/* step: restore */
Routes::RESTORE_PAGE_RESTORE => [
'controller' => RestorePageRestoreController::class,
'method' => 'index',
],
Routes::RESTORE_STEP_RESTORE => [
'controller' => RestorePageRestoreController::class,
'method' => 'step',
],
/* step: post restore */
Routes::RESTORE_PAGE_POST_RESTORE => [
'controller' => RestorePagePostRestoreController::class,
'method' => 'index',
],
Routes::RESTORE_STEP_POST_RESTORE => [
'controller' => RestorePagePostRestoreController::class,
'method' => 'step',
],
/* COMMON */
/* error reporting */
Routes::DISPLAY_ERROR_REPORT_MODAL => [
'controller' => ErrorReportController::class,
'method' => 'displayErrorReportModal',
],
/* logs */
Routes::DOWNLOAD_LOGS => [
'controller' => LogsController::class,
'method' => 'getDownloadLogsButton',
],

Routes::ERROR_404 => [
'controller' => Error404Controller::class,
'method' => 'index',
],
];

/**
* @param Request $request
*
Expand All @@ -239,10 +51,38 @@ public function __construct(UpgradeContainer $upgradeContainer)
public function handle(Request $request)
{
$routeName = $request->query->get('route') ?? Routes::HOME_PAGE;
$route = self::ROUTES[$routeName] ?? self::ROUTES[Routes::ERROR_404];
$route = array_key_exists($routeName, RoutesConfig::ROUTES) ? $routeName : Routes::ERROR_404;
ga-devfront marked this conversation as resolved.
Show resolved Hide resolved

$route = (new Middleware($this->upgradeContainer))->process($route);

if ($routeName !== $route) {
$newUrl = $this->changeRouteRequestParam($request, $route);

header('Location: ' . $newUrl, true, 302);
exit;
ga-devfront marked this conversation as resolved.
Show resolved Hide resolved
}

$routeParams = RoutesConfig::ROUTES[$route];
$method = $routeParams['method'];

return (new $routeParams['controller']($this->upgradeContainer, $request))->$method();
}

/**
* @param Request $request
* @param Routes::* $route
*
* @return string
*/
private function changeRouteRequestParam(Request $request, string $route): string
ga-devfront marked this conversation as resolved.
Show resolved Hide resolved
{
$baseUrl = $request->getSchemeAndHttpHost() . $request->getBaseUrl();
$queryParams = $request->query->all();

$queryParams['route'] = $route;

$method = $route['method'];
$queryString = http_build_query($queryParams);
ga-devfront marked this conversation as resolved.
Show resolved Hide resolved

return (new $route['controller']($this->upgradeContainer, $request))->$method();
return $baseUrl . '?' . $queryString;
ga-devfront marked this conversation as resolved.
Show resolved Hide resolved
}
}
Loading
Loading