Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\Translation\Provider; |
| 13 | |
| 14 | use Symfony\Component\Translation\Exception\UnsupportedSchemeException; |
| 15 | |
| 16 | /** |
| 17 | * @author Mathieu Santostefano <msantostefano@protonmail.com> |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 18 | */ |
| 19 | class TranslationProviderCollectionFactory |
| 20 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 21 | private iterable $factories; |
| 22 | private array $enabledLocales; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 23 | |
| 24 | /** |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 25 | * @param iterable<mixed, ProviderFactoryInterface> $factories |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 26 | */ |
| 27 | public function __construct(iterable $factories, array $enabledLocales) |
| 28 | { |
| 29 | $this->factories = $factories; |
| 30 | $this->enabledLocales = $enabledLocales; |
| 31 | } |
| 32 | |
| 33 | public function fromConfig(array $config): TranslationProviderCollection |
| 34 | { |
| 35 | $providers = []; |
| 36 | foreach ($config as $name => $currentConfig) { |
| 37 | $providers[$name] = $this->fromDsnObject( |
| 38 | new Dsn($currentConfig['dsn']), |
| 39 | !$currentConfig['locales'] ? $this->enabledLocales : $currentConfig['locales'], |
| 40 | !$currentConfig['domains'] ? [] : $currentConfig['domains'] |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | return new TranslationProviderCollection($providers); |
| 45 | } |
| 46 | |
| 47 | public function fromDsnObject(Dsn $dsn, array $locales, array $domains = []): ProviderInterface |
| 48 | { |
| 49 | foreach ($this->factories as $factory) { |
| 50 | if ($factory->supports($dsn)) { |
| 51 | return new FilteringProvider($factory->create($dsn), $locales, $domains); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | throw new UnsupportedSchemeException($dsn); |
| 56 | } |
| 57 | } |