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

Context refactorisation for controller #34390

Merged
merged 2 commits into from Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/config/config.yml
Expand Up @@ -20,6 +20,17 @@ parameters:
mail_themes_dir: "%kernel.project_dir%%mail_themes_uri%"
modules_translation_paths: [ ]
api_base_path: !php/const PrestaShopBundle\Api\Api::API_BASE_PATH
prestashop.controllers_all_shop_context:
- AdminAccess
- AdminFeatureFlag
- AdminLanguages
- AdminProfiles
- AdminSpecificPriceRule
- AdminStatuses
- AdminSecurity
- AdminSecuritySessionEmployee
- AdminSecuritySessionCustomer
- AdminTranslations

# Autowires Core controllers
services:
Expand Down
3 changes: 2 additions & 1 deletion classes/Context.php
Expand Up @@ -28,6 +28,7 @@
use PrestaShop\PrestaShop\Adapter\ContainerFinder;
use PrestaShop\PrestaShop\Adapter\Module\Repository\ModuleRepository;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Context\LegacyControllerContext;
use PrestaShop\PrestaShop\Core\Exception\ContainerNotFoundException;
use PrestaShop\PrestaShop\Core\Localization\CLDR\ComputingPrecision;
use PrestaShop\PrestaShop\Core\Localization\Locale;
Expand Down Expand Up @@ -76,7 +77,7 @@ class ContextCore
/** @var Employee|null */
public $employee;

/** @var AdminController|FrontController|LegacyControllerBridgeInterface|null */
/** @var AdminController|FrontController|LegacyControllerBridgeInterface|LegacyControllerContext|null */
public $controller;

/** @var string */
Expand Down
8 changes: 4 additions & 4 deletions classes/shop/Shop.php
Expand Up @@ -51,16 +51,16 @@ class ShopCore extends ObjectModel
public $active = true;
public $deleted;

/** @var string Physical uri of main url (read only) */
/** @var ?string Physical uri of main url (read only) */
public $physical_uri;

/** @var string Virtual uri of main url (read only) */
/** @var ?string Virtual uri of main url (read only) */
public $virtual_uri;

/** @var string Domain of main url (read only) */
/** @var ?string Domain of main url (read only) */
public $domain;

/** @var string Domain SSL of main url (read only) */
/** @var ?string Domain SSL of main url (read only) */
public $domain_ssl;

/** @var ShopGroup|null Shop group object */
Expand Down
12 changes: 11 additions & 1 deletion phpstan-baseline.neon
Expand Up @@ -205,6 +205,16 @@ parameters:
count: 1
path: src/Core/Context/EmployeeContextBuilder.php

-
message: "#^Namespace Media is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 4
path: src/Core/Context/LegacyControllerContext.php

-
message: "#^Namespace Tools is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 3
path: src/Core/Context/LegacyControllerContextBuilder.php

-
message: "#^Class Shop is forbidden, No legacy calls inside the prestashop bundle\\. Please create an interface and an adapter if you need to\\.$#"
count: 2
Expand Down Expand Up @@ -972,7 +982,7 @@ parameters:

-
message: "#^Using _PS_JS_DIR_ is forbidden, Use dependency injection instead$#"
count: 10
count: 8
jolelievre marked this conversation as resolved.
Show resolved Hide resolved
path: src/PrestaShopBundle/Bridge/AdminController/LegacyControllerBridge.php

-
Expand Down
16 changes: 16 additions & 0 deletions src/Adapter/ContextStateManager.php
Expand Up @@ -30,10 +30,12 @@

use Cart;
use Context;
use Controller;
use Country;
use Currency;
use Customer;
use Language;
use PrestaShop\PrestaShop\Core\Context\LegacyControllerContext;
use Shop;

/**
Expand All @@ -48,6 +50,7 @@ class ContextStateManager
{
private const MANAGED_FIELDS = [
'cart',
'controller',
'country',
'currency',
'language',
Expand Down Expand Up @@ -97,6 +100,19 @@ public function setCart(?Cart $cart): self
return $this;
}

/**
* Sets context controller and saves previous value
*
* @return $this
*/
public function setController(LegacyControllerContext|Controller|null $legacyController): self
{
$this->saveContextField('controller');
$this->getContext()->controller = $legacyController;

return $this;
}

/**
* Sets context country and saves previous value
*
Expand Down
181 changes: 181 additions & 0 deletions src/Core/Context/LegacyControllerContext.php
@@ -0,0 +1,181 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @copyright Since 2007 PrestaShop SA and Contributors
* @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
*/

declare(strict_types=1);

namespace PrestaShop\PrestaShop\Core\Context;

use Media;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Traversable;

/**
* This class ensures compatibility with the context controller in pages migrated to Symfony.
* It encompasses the majority of public fields found in a legacy controller.
*/
class LegacyControllerContext
{
public ?string $className;
Copy link
Contributor

Choose a reason for hiding this comment

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

Not necessarily to be done in this PR, but I think most of those fields are only read and never written, or at least they should never be written Since we can now use the readonly property we should think about which fields could be forced as read only

This would enforce even more the contract that the controller is supposed to follow and reduce the potential bugs due to external modules and code modifying some internal fields

But again not blocking for this PR


/**
* List of CSS files.
*
* @var string[]
*/
public array $css_files = [];

/**
* List of JavaScript files.
*
* @var string[]
*/
public array $js_files = [];

// Controller type. Possible values: 'front', 'modulefront', 'admin', 'moduleadmin'.
public string $controller_type;

// Controller name.
public string $php_self;

/**
* Errors displayed after post processing
*
* @var array<string|int, string|bool>
*/
public array $errors = [];

public array $warnings = [];

public array $informations = [];

public array $confirmations = [];
jolelievre marked this conversation as resolved.
Show resolved Hide resolved

public string $currentIndex;

public int $id = -1;

// Security token
public ?string $token;

public string $override_folder;

// Image type
public string $imageType = 'jpg';

// Current controller name without suffix
public string $controller_name;

public int $multishop_context = -1;

// Bootstrap variable
public array|Traversable $page_header_toolbar_btn = [];

// Dependency container
protected ContainerInterface $container;

public function __construct(
ContainerInterface $container,
string $controller_name,
string $controller_type,
string $php_self,
int $multishop_context,
?string $className,
int $id,
?string $token,
string $override_folder,
) {
$this->container = $container;
$this->controller_type = $controller_type;
$this->php_self = $php_self;
$this->controller_name = $controller_name;
$this->multishop_context = $multishop_context;
$this->className = $className;
$this->id = $id;
$this->token = $token;
$this->override_folder = $override_folder;
}

public function addCSS($css_uri, $css_media_type = 'all', $offset = null, $check_path = true): void
{
if (!is_array($css_uri)) {
$css_uri = [$css_uri];
}

foreach ($css_uri as $css_file => $media) {
if (is_string($css_file) && strlen($css_file) > 1) {
if ($check_path) {
$css_path = Media::getCSSPath($css_file, $media);
} else {
$css_path = [$css_file => $media];
}
} else {
if ($check_path) {
$css_path = Media::getCSSPath($media, $css_media_type);
} else {
$css_path = [$media => $css_media_type];
}
}

$key = is_array($css_path) ? key($css_path) : $css_path;
if ($css_path && (!isset($this->css_files[$key]) || ($this->css_files[$key] != reset($css_path)))) {
$size = count($this->css_files);
if ($offset === null || $offset > $size || $offset < 0 || !is_numeric($offset)) {
$offset = $size;
}

$this->css_files = array_merge(array_slice($this->css_files, 0, $offset), $css_path, array_slice($this->css_files, $offset));
}
}
}

public function addJS($js_uri, $check_path = true): void
{
if (!is_array($js_uri)) {
$js_uri = [$js_uri];
}

foreach ($js_uri as $js_file) {
$js_file = explode('?', $js_file);
$version = '';
if (isset($js_file[1]) && $js_file[1]) {
$version = $js_file[1];
}
$js_path = $js_file = $js_file[0];
if ($check_path) {
$js_path = Media::getJSPath($js_file);
}

if ($js_path && !in_array($js_path, $this->js_files)) {
$this->js_files[] = $js_path . ($version ? '?' . $version : '');
}
}
}

public function getContainer(): ContainerInterface
{
return $this->container;
}
}