From 215595be5a6bfa7e97893079ad5d6289e7581d07 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 3 Oct 2019 09:55:29 +0200 Subject: [PATCH] [FrameworkBundle] encourage installing intl when String is available --- .../DependencyInjection/FrameworkExtension.php | 13 +++++++++++-- .../Component/String/Slugger/AsciiSlugger.php | 9 +++------ src/Symfony/Component/String/composer.json | 3 ++- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 5675e56726..a8d30e8e16 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -196,9 +196,18 @@ class FrameworkExtension extends Extension } // If the slugger is used but the String component is not available, we should throw an error - if (!class_exists(SluggerInterface::class)) { + if (!interface_exists(SluggerInterface::class)) { $container->register('slugger', 'stdClass') - ->addError('You cannot use the "slugger" since the String component is not installed. Try running "composer require symfony/string".'); + ->addError('You cannot use the "slugger" service since the String component is not installed. Try running "composer require symfony/string".'); + } else { + if (!interface_exists(LocaleAwareInterface::class)) { + $container->register('slugger', 'stdClass') + ->addError('You cannot use the "slugger" service since the Translation contracts are not installed. Try running "composer require symfony/translation".'); + } + + if (!\extension_loaded('intl')) { + @trigger_error('Please install the "intl" PHP extension for best performance.', E_USER_DEPRECATED); + } } if (isset($config['secret'])) { diff --git a/src/Symfony/Component/String/Slugger/AsciiSlugger.php b/src/Symfony/Component/String/Slugger/AsciiSlugger.php index e359f823a2..d703467e02 100644 --- a/src/Symfony/Component/String/Slugger/AsciiSlugger.php +++ b/src/Symfony/Component/String/Slugger/AsciiSlugger.php @@ -107,7 +107,7 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface private function createTransliterator(string $locale): ?\Transliterator { - if (isset($this->transliterators[$locale])) { + if (\array_key_exists($locale, $this->transliterators)) { return $this->transliterators[$locale]; } @@ -118,7 +118,7 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface // Locale not supported and no parent, fallback to any-latin if (false === $str = strrchr($locale, '_')) { - return null; + return $this->transliterators[$locale] = null; } // Try to use the parent locale (ie. try "de" for "de_AT") and cache both locales @@ -126,11 +126,8 @@ class AsciiSlugger implements SluggerInterface, LocaleAwareInterface if ($id = self::LOCALE_TO_TRANSLITERATOR_ID[$parent] ?? null) { $transliterator = \Transliterator::create($id.'/BGN') ?? \Transliterator::create($id); - $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator; - - return $transliterator; } - return null; + return $this->transliterators[$locale] = $this->transliterators[$parent] = $transliterator ?? null; } } diff --git a/src/Symfony/Component/String/composer.json b/src/Symfony/Component/String/composer.json index 705abed3b8..bcbaa7575d 100644 --- a/src/Symfony/Component/String/composer.json +++ b/src/Symfony/Component/String/composer.json @@ -19,7 +19,8 @@ "php": "^7.2.9", "symfony/polyfill-intl-grapheme": "~1.0", "symfony/polyfill-intl-normalizer": "~1.0", - "symfony/polyfill-mbstring": "~1.0" + "symfony/polyfill-mbstring": "~1.0", + "symfony/translation-contracts": "^1.1|^2.0" }, "autoload": { "psr-4": { "Symfony\\Component\\String\\": "" },