PrestaShop / PrestaShop

PrestaShop is the universal open-source software platform to build your e-commerce solution.
https://www.prestashop-project.org/
Other
7.9k stars 4.75k forks source link

The addition of a service during the update of a module is not taken into account by PrestaShop v1.7.8.11. #36170

Closed dylan-ramos closed 4 weeks ago

dylan-ramos commented 1 month ago

Prerequisites

Describe the bug and add attachments

Hello, I am encountering an issue with the declaration of services in modules for PrestaShop v1.7.8. When I add a service to an already installed module, I have to uninstall and then reinstall (reset) the module for the new services to be recognized. Is there a way to force the update of the list of services recognized by PrestaShop (directly in the module code or via CLI), or is this a bug?


Bonjour, Je rencontre un problème dans la déclaration des services dans les modules pour PrestaShop v1.7.8. Lorsque je rajoute un service dans un module déjà installé, je suis obligé de désinstaller puis réinstaller (reset) le module afin que les nouveaux services soient pris en comptes. Y a-t-il la possibilité de forcer l’actualisation de la liste des service pris en compte par PrestaShop (directement dans le code du module ou en cli) ou est-ce un bug ?

Expected behavior

I would like to know if there is a way to refresh the list of services for an existing module without having to reset it (I also feel that the command "cache:clear --env=dev|prod" does not always solve the problem). Thank you.


J’aurais voulu savoir s’il y a une manière d’actualiser la liste des services pour un modules déjà existant sans avoir à le reset (j’ai l’impression aussi que la commande « cache:clear –env=dev|prod » ne règle pas toujours le problème. En vous remerciant

Steps to reproduce

For example: Version 1 of the module

mymodule/config/services.yml

services:
    # V1.0.0
    # Data factory
    mymodule.data.factory.setting_data_factory:
        class: MyModule\Data\Factory\SettingDataFactory
        arguments:
            - '@mymodule.data.configuration.setting_configuration'

    # Data provider
    mymodule.data.configuration.setting_configuration:
        class: MyModule\Data\Configuration\SettingConfiguration
        arguments:
            - '@prestashop.adapter.legacy.configuration'
            - "@=service('prestashop.adapter.legacy.context').getLanguages()"
        public: true

    mymodule.data.setting_data_provider:
        class: MyModule\Data\Factory\SettingDataFactory
        factory: 'mymodule.data.factory.setting_data_factory:getData'

If I want to update the module as follows:

mymodule/config/services.yml

services:
    # V1.0.0
    # Data factory
    mymodule.data.factory.setting_data_factory:
        class: MyModule\Data\Factory\SettingDataFactory
        arguments:
            - '@mymodule.data.configuration.setting_configuration'

    # Data provider
    mymodule.data.configuration.setting_configuration:
        class: MyModule\Data\Configuration\SettingConfiguration
        arguments:
            - '@prestashop.adapter.legacy.configuration'
            - "@=service('prestashop.adapter.legacy.context').getLanguages()"
        public: true

    mymodule.data.setting_data_provider:
        class: MyModule\Data\Factory\SettingDataFactory
        factory: 'mymodule.data.factory.setting_data_factory:getData'
        public: true

    # V2.0.0
    # Data factory
    mymodule.data.factory.setting_data_factory_new:
        class: MyModule\Data\Factory\SettingDataFactoryNew
        arguments:
            - '@mymodule.data.configuration.setting_configuration_new'

    # Data provider
    mymodule.data.configuration.setting_configuration_new:
        class: MyModule\Data\Configuration\SettingConfigurationNew
        arguments:
            - '@prestashop.adapter.legacy.configuration'
            - "@=service('prestashop.adapter.legacy.context').getLanguages()"
        public: true

    mymodule.data.setting_data_provider_new:
        class: MyModule\Data\Factory\SettingDataFactoryNew
        factory: 'mymodule.data.factory.setting_data_factory_new:getData'
        public: true

While creating an upgrade file, for example:

mymodule/upgrade/upgrade-2.0.0.php

<?php

declare(strict_types=1);

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * @param $module
 *
 * @return bool
 */
function upgrade_module_2_0_0($module): bool
{
    $r = true;

    try {
        // TODO Can we perform an action here to force the update of the services?
    } catch (Throwable $e) {
        $r = false;

        PrestaShopLogger::addLog('mymodule - upgrade 2.0.0 - ' . __LINE__ . ' - Throwable #' . $e->getCode() . ' - ' . $e->getMessage() . '.', 3);
    }

    return (bool)$r;
}

Then the new services will not be recognized by PrestaShop (services suffixed with "new").

PrestaShop version(s) where the bug happened

1.7.8.11

PHP version(s) where the bug happened

7.2.34

If your bug is related to a module, specify its name and its version

No response

Your company or customer's name goes here (if applicable).

No response

Hlavtox commented 4 weeks ago

This was resolved on latest version by clearing cache on every module action. Sadly, nothing that you can do in 1.7.8.

If you want, you can wipe cache when upgrading the module.

dylan-ramos commented 4 weeks ago

Good evening, thank you for your response. Indeed, using the services:

This seems to solve the problem by adding the new services to the container. So I think I will add these few lines in the update files to avoid any unpleasant surprises 😊

<?php

declare(strict_types=1);

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * @param $module
 *
 * @return bool
 */
function upgrade_module_2_0_0($module): bool
{
    $r = true;

    try {
        /** @var \PrestaShop\PrestaShop\Core\Cache\Clearer\CacheClearerChain $cacheClearerChain */
        $cacheClearerChain = $module->get('prestashop.core.cache.clearer.cache_clearer_chain');
        $cacheClearerChain->clear();

        /** @var \PrestaShop\PrestaShop\Adapter\Cache\Clearer\SymfonyCacheClearer $symfonyClearerChain */
        $symfonyClearerChain = $module->get('prestashop.adapter.cache.clearer.symfony_cache_clearer');
        $symfonyClearerChain->clear();
    } catch (Throwable $e) {
        $r = false;

        PrestaShopLogger::addLog('mymodule - upgrade 2.0.0 - ' . __LINE__ . ' - Throwable #' . $e->getCode() . ' - ' . $e->getMessage() . '.', 3);
    }

    return (bool)$r;
}
        /** @var \PrestaShop\PrestaShop\Core\Cache\Clearer\CacheClearerChain $cacheClearerChain */
        $cacheClearerChain = $module->get('prestashop.core.cache.clearer.cache_clearer_chain');
        $cacheClearerChain->clear();

        // OR

        /** @var \PrestaShop\PrestaShop\Adapter\Cache\Clearer\SymfonyCacheClearer $symfonyClearerChain */
        $symfonyClearerChain = $module->get('prestashop.adapter.cache.clearer.symfony_cache_clearer');
        $symfonyClearerChain->clear();

Thank you again for your response.