TranslationProviderCollectionFactory.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. class TranslationProviderCollectionFactory
  16. {
  17. private $factories;
  18. private $enabledLocales;
  19. /**
  20. * @param iterable<mixed, ProviderFactoryInterface> $factories
  21. */
  22. public function __construct(iterable $factories, array $enabledLocales)
  23. {
  24. $this->factories = $factories;
  25. $this->enabledLocales = $enabledLocales;
  26. }
  27. public function fromConfig(array $config): TranslationProviderCollection
  28. {
  29. $providers = [];
  30. foreach ($config as $name => $currentConfig) {
  31. $providers[$name] = $this->fromDsnObject(
  32. new Dsn($currentConfig['dsn']),
  33. !$currentConfig['locales'] ? $this->enabledLocales : $currentConfig['locales'],
  34. !$currentConfig['domains'] ? [] : $currentConfig['domains']
  35. );
  36. }
  37. return new TranslationProviderCollection($providers);
  38. }
  39. public function fromDsnObject(Dsn $dsn, array $locales, array $domains = []): ProviderInterface
  40. {
  41. foreach ($this->factories as $factory) {
  42. if ($factory->supports($dsn)) {
  43. return new FilteringProvider($factory->create($dsn), $locales, $domains);
  44. }
  45. }
  46. throw new UnsupportedSchemeException($dsn);
  47. }
  48. }