TranslationProviderCollectionFactory.php 1.6 KB

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