Skip to content

Commit

Permalink
Create controller context
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Pichat committed Nov 7, 2023
1 parent 5f99d73 commit e2b84ff
Show file tree
Hide file tree
Showing 7 changed files with 534 additions and 12 deletions.
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\LegacyController as ControllerContext;
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|ControllerContext|null */
public $controller;

/** @var string */
Expand Down
15 changes: 15 additions & 0 deletions src/Adapter/ContextStateManager.php
Expand Up @@ -34,6 +34,7 @@
use Currency;
use Customer;
use Language;
use PrestaShop\PrestaShop\Core\Context\LegacyController;
use Shop;

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

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

return $this;
}

/**
* Sets context country and saves previous value
*
Expand Down
279 changes: 279 additions & 0 deletions src/Core/Context/LegacyController.php
@@ -0,0 +1,279 @@
<?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)
*/

namespace PrestaShop\PrestaShop\Core\Context;

use Media;

Check failure on line 29 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 29 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
use Module;

Check failure on line 30 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Namespace Module is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 30 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Namespace Module is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
use Symfony\Component\DependencyInjection\ContainerInterface;
use Traversable;

class LegacyController
{
// Dependency container
protected ContainerInterface $container;

public ?string $className;

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

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

// List of PHP errors.
public array $php_errors = [];

// If AJAX parameter is detected in request, set this flag to true.
public bool $ajax = false;

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

// Controller name.
public string $php_self;

public ?Module $module;

Check failure on line 67 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Class Module is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 67 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Class Module is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

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

public string $path;

public string $currentIndex;

public string $content;

public array $warnings = [];

public array $informations = [];

public array $confirmations = [];

public string|false $shopShareDatas = false;

public array $_languages = [];

public int $default_form_language;

public int $allow_employee_form_lang;

public string $layout = 'layout.tpl';

public bool $bootstrap = false;

public string $template = 'content.tpl';

public string $table = 'configuration';

public ?string $list_id;

public array $tabAccess;

public int $id = -1;

public bool $required_database = false;

// Security token
public string $token;

// "shop" or "group_shop"
public ?string $shopLinkType;

public array $tpl_option_vars = [];

public array $tpl_view_vars = [];

public array $tpl_required_fields_vars = [];

public ?string $base_tpl_view = null;

public ?string $base_tpl_form = null;

// If you want more fieldsets in the form
public bool $multiple_fieldsets = false;

public array $fields_value = [];

public ?int $max_image_size = null;

// Automatically join language table if true
public bool $lang = false;

// Cache for translations
public array $cache_lang = [];

// Required_fields to display in the Required Fields form
public array $required_fields = [];

public string $override_folder;

// Name and directory where class image are located
public array $fieldImageSettings = [];

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

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

public int $multishop_context = -1;

public bool $multishop_context_group = true;

// Bootstrap variable
public bool $show_page_header_toolbar = false;

// Bootstrap variable
public string $page_header_toolbar_title;

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

// Bootstrap variable
public ?bool $show_form_cancel_button;

public string $admin_webpath;

/** @var array<string, string|array> */
public array $modals = [];

// Auth cookie lifetime
public const AUTH_COOKIE_LIFETIME = 3600;

public array $_conf;

public array $_error;

public int $_lang;

public string $tpl_folder;

public function __construct(
ContainerInterface $container,
string $controller_name,
string $controller_type,
string $php_self,
int $multishop_context,
?string $className,
int $id,
string $token,
array $_conf,
array $_error,
?string $shopLinkType,
string $override_folder,
string $tpl_folder,
string $admin_webpath,
) {
$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->_conf = $_conf;
$this->_error = $_error;
$this->shopLinkType = $shopLinkType;
$this->override_folder = $override_folder;
$this->tpl_folder = $tpl_folder;
$this->admin_webpath = $admin_webpath;
}

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);

Check failure on line 228 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 228 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
} else {
$css_path = [$css_file => $media];
}
} else {
if ($check_path) {
$css_path = Media::getCSSPath($media, $css_media_type);

Check failure on line 234 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 234 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
} 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);

Check failure on line 266 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.

Check failure on line 266 in src/Core/Context/LegacyController.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Namespace Media is forbidden, No legacy calls inside the prestashop bundle. Please create an interface and an adapter if you need to.
}

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

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

0 comments on commit e2b84ff

Please sign in to comment.