Test Failed
Pull Request — master (#996)
by Maxim
10:25
created

I18nBootloader::addDirectory()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Bootloader;
6
7
use Spiral\Boot\Bootloader\Bootloader;
8
use Spiral\Boot\DirectoriesInterface;
9
use Spiral\Boot\Environment\DebugMode;
10
use Spiral\Boot\EnvironmentInterface;
11
use Spiral\Config\ConfiguratorInterface;
12
use Spiral\Config\Patch\Append;
13
use Spiral\Core\Container\SingletonInterface;
14
use Spiral\Translator\Catalogue\CacheInterface;
15
use Spiral\Translator\Catalogue\CatalogueLoader;
16
use Spiral\Translator\Catalogue\CatalogueManager;
17
use Spiral\Translator\Catalogue\LoaderInterface;
18
use Spiral\Translator\CatalogueManagerInterface;
19
use Spiral\Translator\Config\TranslatorConfig;
20
use Spiral\Translator\MemoryCache;
21
use Spiral\Translator\Translator;
22
use Spiral\Translator\TranslatorInterface;
23
use Symfony\Component\Translation\Dumper;
24
use Symfony\Component\Translation\IdentityTranslator;
25
use Symfony\Component\Translation\Loader;
26
27
/**
28
 * Attention, the default language would not be automatically reset in finalizers. Make sure to properly design your
29
 * middleware.
30
 */
31
final class I18nBootloader extends Bootloader implements SingletonInterface
32
{
33
    protected const SINGLETONS = [
34
        \Symfony\Contracts\Translation\TranslatorInterface::class => TranslatorInterface::class,
35
        TranslatorInterface::class => Translator::class,
36
        CatalogueManagerInterface::class => CatalogueManager::class,
37
        LoaderInterface::class => CatalogueLoader::class,
38
        CacheInterface::class => MemoryCache::class,
39
        IdentityTranslator::class => [self::class, 'identityTranslator'],
40
    ];
41 304
42
    public function __construct(
43
        private readonly ConfiguratorInterface $config
44 304
    ) {
45
    }
46 304
47
    public function init(EnvironmentInterface $env, DirectoriesInterface $dirs, DebugMode $debugMode): void
48 304
    {
49 304
        if (!$dirs->has('locale')) {
50
            $dirs->set('locale', $dirs->get('app') . 'locale/');
51
        }
52 304
53 304
        $this->config->setDefaults(
54 304
            TranslatorConfig::CONFIG,
55 304
            [
56 304
                'locale' => $env->get('LOCALE', 'en'),
57 304
                'fallbackLocale' => $env->get('LOCALE', 'en'),
58 304
                'directory' => $dirs->get('locale'),
59 304
                'directories' => [],
60 304
                'autoRegister' => $debugMode->isEnabled(),
61 304
                'loaders' => [
62 304
                    'php' => Loader\PhpFileLoader::class,
63 304
                    'po' => Loader\PoFileLoader::class,
64 304
                    'csv' => Loader\CsvFileLoader::class,
65 304
                    'json' => Loader\JsonFileLoader::class,
66 304
                ],
67 304
                'dumpers' => [
68 304
                    'php' => Dumper\PhpFileDumper::class,
69 304
                    'po' => Dumper\PoFileDumper::class,
70 304
                    'csv' => Dumper\CsvFileDumper::class,
71 304
                    'json' => Dumper\JsonFileDumper::class,
72
                ],
73 304
                'domains' => [
74 304
                    // by default we can store all messages in one domain
75 304
                    'messages' => ['*'],
76 304
                ],
77
            ]
78
        );
79
    }
80
81
    /**
82 32
     * @param non-empty-string $directory
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string.
Loading history...
83
     */
84 32
    public function addDirectory(string $directory): void
85
    {
86
        $this->config->modify(TranslatorConfig::CONFIG, new Append('directories', null, $directory));
87
    }
88
89
    private function identityTranslator(): IdentityTranslator
90
    {
91
        return new IdentityTranslator();
92
    }
93
}
94