blob: 6300c8750e13036f0e90c13a73f61985e396673e [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\Translation\Provider;
13
14use Symfony\Component\Translation\Exception\UnsupportedSchemeException;
15
16/**
17 * @author Mathieu Santostefano <msantostefano@protonmail.com>
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020018 */
19class TranslationProviderCollectionFactory
20{
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010021 private iterable $factories;
22 private array $enabledLocales;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020023
24 /**
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010025 * @param iterable<mixed, ProviderFactoryInterface> $factories
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020026 */
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}