🔄 PrestaShop 9 : Évolution de la classe Tools et impact sur vos modules


PrestaShop 9 Tools Class Evolution

La classe Tools de PrestaShop évolue dans la version 9, avec des changements majeurs à anticiper. The PrestaShop Tools class evolves in version 9, with major changes to anticipate. La clase Tools de PrestaShop evoluciona en la versión 9, con cambios importantes que anticipar. La classe Tools di PrestaShop si evolve nella versione 9, con cambiamenti importanti da anticipare. Ces évolutions, alignées sur les standards PHP modernes, nécessitent une préparation en amont de vos modules. These changes, aligned with modern PHP standards, require advance preparation of your modules. Estos cambios, alineados con los estándares modernos de PHP, requieren una preparación previa de sus módulos. Questi cambiamenti, allineati con gli standard PHP moderni, richiedono una preparazione anticipata dei vostri moduli. Identifiez les points d'attention pour préparer sereinement la compatibilité de votre code. Identify the key points to smoothly prepare your code for compatibility. Identifique los puntos clave para preparar tranquilamente la compatibilidad de su código. Identificate i punti chiave per preparare serenamente la compatibilità del vostro codice.

Tools::getBytes()

CODE DÉPRÉCIÉ
public static function getBytes($length)
{
    $length = (int) $length;

    if ($length <= 0) {
        return false;
    }
}

Tools::redirectLink()

CODE DÉPRÉCIÉ
public static function redirectLink($url)
{
    Tools::displayAsDeprecated('Use Tools::redirect() instead');
    static::redirect($url);
}
NOUVEAU CODE
Tools::redirect($url);

Tools::displayNumber()

CODE DÉPRÉCIÉ
public static function displayNumber($number, $currency = null)
{
    $context = Context::getContext();
    $locale = static::getContextLocale($context);
    return $locale->formatNumber($number);
}
NOUVEAU CODE
$locale = Context::getContext()->getCurrentLocale();
$formattedNumber = $locale->formatNumber($number);

Tools::encrypt() et Tools::encryptIV()

CODE DÉPRÉCIÉ
$encrypted = Tools::encrypt($passwd);
$encryptedIV = Tools::encryptIV($data);
NOUVEAU CODE
$hashed = Tools::hash($passwd);
$hashedIV = Tools::hashIV($data);

Tools::getBrightness() et Tools::isBright()

CODE DÉPRÉCIÉ
public static function getBrightness($hex)
{
    if (Tools::strtolower($hex) == 'transparent') {
        return '129';
    }
}

public static function isBright($hex)
{
    if (null === self::$colorBrightnessCalculator) {
        self::$colorBrightnessCalculator = new ColorBrightnessCalculator();
    }
}
NOUVEAU CODE
$colorBrightnessCalculator = new ColorBrightnessCalculator();
$brightness = $colorBrightnessCalculator->getBrightness($hex);
$isBright = $colorBrightnessCalculator->isBright($hex);

Tools::replaceByAbsoluteURL()

CODE DÉPRÉCIÉ
public static function replaceByAbsoluteURL($matches)
{
    Tools::displayAsDeprecated('Use Media::replaceByAbsoluteURL($matches) instead');
    return Media::replaceByAbsoluteURL($matches);
}
NOUVEAU CODE
$absoluteUrl = Media::replaceByAbsoluteURL($matches);

Tools::generateIndex()

CODE DÉPRÉCIÉ
public static function generateIndex()
{
    PrestaShopAutoload::getInstance()->generateIndex();
}
NOUVEAU CODE
PrestaShopAutoload::getInstance()->generateIndex();

Tools::clearColorListCache()

CODE DÉPRÉCIÉ
public static function clearColorListCache($id_product = false)
{
    $current_template_dir = Context::getContext()->smarty->getTemplateDir();
    Context::getContext()->smarty->setTemplateDir(_PS_THEME_DIR_);
    Tools::clearCache(null, _PS_THEME_DIR_ . 'product-list-colors.tpl',
        Product::getColorsListCacheId((int) $id_product, false));
    Context::getContext()->smarty->setTemplateDir($current_template_dir);
}

Tools::cleanNonUnicodeSupport()

CODE DÉPRÉCIÉ
public static function cleanNonUnicodeSupport($pattern)
{
    @trigger_error(
        sprintf(
            '%s is deprecated since version 8.0.0. Its use is not required.',
            __METHOD__
        ),
        E_USER_DEPRECATED
    );

    return $pattern;
}

Tools::getDescriptionClean()

CODE DÉPRÉCIÉ
public static function getDescriptionClean($description)
{
    return strip_tags(stripslashes($description));
}
NOUVEAU CODE
$cleanDescription = strip_tags(stripslashes($description));

Tools::arrayReplaceRecursive()

CODE DÉPRÉCIÉ
public static function arrayReplaceRecursive($base, $replacements)
{
    Tools::displayAsDeprecated('Use PHP\\\'s array_replace_recursive() instead');
    return array_replace_recursive($base, $replacements);
}
NOUVEAU CODE
$result = array_replace_recursive($base, $replacements);

Tools::addonsRequest()

CODE DÉPRÉCIÉ
public static function addonsRequest($request, $params = array())
{
    Tools::displayAsDeprecated('Use Addons\\AddonsRequester instead');
    return false;
}
NOUVEAU CODE
$addonsRequester = new \PrestaShop\PrestaShop\Adapter\Addons\AddonsRequester();
$result = $addonsRequester->request($request, $params);

Tools::getMediaServer()

CODE DÉPRÉCIÉ
public static function getMediaServer($filename)
{
    Tools::displayAsDeprecated('Use Tools::getMediaServers() instead');
    return Tools::getMediaServers($filename);
}
NOUVEAU CODE
$mediaServer = Tools::getMediaServers($filename);

Tools::getHttpHost()

CODE DÉPRÉCIÉ
public static function getHttpHost($http = false, $entities = false, $ignore_port = false)
{
    Tools::displayAsDeprecated('Use Tools::getServerName() instead');
    return Tools::getServerName();
}
NOUVEAU CODE
$serverName = Tools::getServerName();

Tools::getShopDomain()

CODE DÉPRÉCIÉ
public static function getShopDomain($http = false, $entities = false)
{
    Tools::displayAsDeprecated('Use Tools::getServerName() instead');
    return Tools::getServerName();
}
NOUVEAU CODE
$serverName = Tools::getServerName();

Tools::displayPrice()

CODE DÉPRÉCIÉ
public static function displayPrice($price, $currency = null, $no_utf8 = false, Context $context = null)
{
    @trigger_error(
        'Tools::displayPrice() is deprecated since version 1.7.6.0. '
        . 'Use ' . Locale::class . '::formatPrice() instead.',
        E_USER_DEPRECATED
    );

    if (!is_numeric($price)) {
        return $price;
    }
}
NOUVEAU CODE
$locale = Context::getContext()->getCurrentLocale();
$formattedPrice = $locale->formatPrice($price, $currencyIsoCode);