You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.7 KiB

3 years ago
  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\Provider;
  11. use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
  12. /**
  13. * @author Mathieu Santostefano <msantostefano@protonmail.com>
  14. *
  15. * @experimental in 5.3
  16. */
  17. class TranslationProviderCollectionFactory
  18. {
  19. private $factories;
  20. private $enabledLocales;
  21. /**
  22. * @param ProviderFactoryInterface[] $factories
  23. */
  24. public function __construct(iterable $factories, array $enabledLocales)
  25. {
  26. $this->factories = $factories;
  27. $this->enabledLocales = $enabledLocales;
  28. }
  29. public function fromConfig(array $config): TranslationProviderCollection
  30. {
  31. $providers = [];
  32. foreach ($config as $name => $currentConfig) {
  33. $providers[$name] = $this->fromDsnObject(
  34. new Dsn($currentConfig['dsn']),
  35. !$currentConfig['locales'] ? $this->enabledLocales : $currentConfig['locales'],
  36. !$currentConfig['domains'] ? [] : $currentConfig['domains']
  37. );
  38. }
  39. return new TranslationProviderCollection($providers);
  40. }
  41. public function fromDsnObject(Dsn $dsn, array $locales, array $domains = []): ProviderInterface
  42. {
  43. foreach ($this->factories as $factory) {
  44. if ($factory->supports($dsn)) {
  45. return new FilteringProvider($factory->create($dsn), $locales, $domains);
  46. }
  47. }
  48. throw new UnsupportedSchemeException($dsn);
  49. }
  50. }