UnsupportedSchemeException.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Translation\Exception;
  11. use Symfony\Component\Translation\Bridge;
  12. use Symfony\Component\Translation\Provider\Dsn;
  13. class UnsupportedSchemeException extends LogicException
  14. {
  15. private const SCHEME_TO_PACKAGE_MAP = [
  16. 'crowdin' => [
  17. 'class' => Bridge\Crowdin\CrowdinProviderFactory::class,
  18. 'package' => 'symfony/crowdin-translation-provider',
  19. ],
  20. 'loco' => [
  21. 'class' => Bridge\Loco\LocoProviderFactory::class,
  22. 'package' => 'symfony/loco-translation-provider',
  23. ],
  24. 'lokalise' => [
  25. 'class' => Bridge\Lokalise\LokaliseProviderFactory::class,
  26. 'package' => 'symfony/lokalise-translation-provider',
  27. ],
  28. 'phrase' => [
  29. 'class' => Bridge\Phrase\PhraseProviderFactory::class,
  30. 'package' => 'symfony/phrase-translation-provider',
  31. ],
  32. ];
  33. public function __construct(Dsn $dsn, ?string $name = null, array $supported = [])
  34. {
  35. $provider = $dsn->getScheme();
  36. if (false !== $pos = strpos($provider, '+')) {
  37. $provider = substr($provider, 0, $pos);
  38. }
  39. $package = self::SCHEME_TO_PACKAGE_MAP[$provider] ?? null;
  40. if ($package && !class_exists($package['class'])) {
  41. parent::__construct(sprintf('Unable to synchronize translations via "%s" as the provider is not installed. Try running "composer require %s".', $provider, $package['package']));
  42. return;
  43. }
  44. $message = sprintf('The "%s" scheme is not supported', $dsn->getScheme());
  45. if ($name && $supported) {
  46. $message .= sprintf('; supported schemes for translation provider "%s" are: "%s"', $name, implode('", "', $supported));
  47. }
  48. parent::__construct($message.'.');
  49. }
  50. }