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