Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
78.43% covered (warning)
78.43%
40 / 51
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
I18nServiceProvider
78.00% covered (warning)
78.00%
39 / 50
50.00% covered (danger)
50.00%
1 / 2
2.04
0.00% covered (danger)
0.00%
0 / 1
 getDefinitions
77.55% covered (warning)
77.55%
38 / 49
0.00% covered (danger)
0.00%
0 / 1
1.01
 getExtensions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/** @license For full copyright and license information, please see the LICENSE.md file. */
6
7namespace App\Core\Bootstrap\Provider;
8
9defined('AMMONLY_APP') || exit('Direct script access is forbidden.');
10
11use App\Core\Translation\DatabaseMessageSource;
12use App\Core\Translation\LocaleContext;
13use App\Core\Translation\LocaleContextInterface;
14use App\Core\Translation\Middleware\LocaleDetectionMiddleware;
15use App\Core\Translation\Presentation\Api\TranslationApiController;
16use App\Modules\User\Infrastructure\Repository\SqlUserRepository;
17use App\Modules\User\Presentation\Api\LocaleApiController;
18use Nyholm\Psr7\Factory\Psr17Factory;
19use PDO;
20use Psr\Container\ContainerInterface;
21use Yiisoft\Di\ServiceProviderInterface;
22use Yiisoft\Session\SessionInterface;
23use Yiisoft\Translator\CategorySource;
24use Yiisoft\Translator\SimpleMessageFormatter;
25use Yiisoft\Translator\Translator;
26use Yiisoft\Translator\TranslatorInterface;
27use Yiisoft\User\CurrentUser;
28
29/**
30 * I18nServiceProvider.
31 *
32 * Registers translation engine, database message source, locale context, and middleware.
33 *
34 * @package App\Core\Bootstrap\Provider
35 */
36final class I18nServiceProvider implements ServiceProviderInterface
37{
38    /**
39     * @return array<string, mixed> Definitions mapping for PSR-11 container.
40     */
41    public function getDefinitions(): array
42    {
43        return [
44            TranslationApiController::class => static fn(
45                Psr17Factory $psr17,
46                PDO $pdo
47            ): TranslationApiController => new TranslationApiController($psr17, $pdo),
48
49            LocaleContext::class => static fn(
50                TranslationApiController $translationApi
51            ): LocaleContext => new LocaleContext($translationApi),
52            LocaleContextInterface::class => static fn(LocaleContext $c): LocaleContextInterface => $c,
53
54            DatabaseMessageSource::class => static fn(
55                TranslationApiController $translationApi
56            ): DatabaseMessageSource => new DatabaseMessageSource(
57                $translationApi,
58                dirname(__DIR__, 4) . '/storage/cache/translations'
59            ),
60
61            TranslatorInterface::class => static function (ContainerInterface $c): TranslatorInterface {
62                $localeContext = $c->get(LocaleContext::class);
63                $dbSource = $c->get(DatabaseMessageSource::class);
64                $translator = new Translator(
65                    locale: $localeContext->getLocale(),
66                    fallbackLocale: 'en'
67                );
68                $translator->addCategorySources(
69                    new CategorySource('app', $dbSource, new SimpleMessageFormatter()),
70                    new CategorySource('engine', $dbSource, new SimpleMessageFormatter())
71                );
72                return $translator;
73            },
74
75            LocaleDetectionMiddleware::class => static fn(
76                LocaleContext $ctx,
77                TranslatorInterface $trans,
78                SessionInterface $session
79            ): LocaleDetectionMiddleware => new LocaleDetectionMiddleware($ctx, $trans, $session),
80
81            LocaleApiController::class => static fn(
82                LocaleContext $ctx,
83                TranslatorInterface $trans,
84                SessionInterface $session,
85                CurrentUser $user,
86                SqlUserRepository $userRepo,
87                Psr17Factory $psr17
88            ): LocaleApiController => new LocaleApiController(
89                $ctx,
90                $trans,
91                $session,
92                $user,
93                $userRepo,
94                $psr17
95            ),
96        ];
97    }
98
99    /**
100     * @return array<string, mixed> Service extensions mapping.
101     */
102    public function getExtensions(): array
103    {
104        return [];
105    }
106}