Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.43% |
40 / 51 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| I18nServiceProvider | |
78.00% |
39 / 50 |
|
50.00% |
1 / 2 |
2.04 | |
0.00% |
0 / 1 |
| getDefinitions | |
77.55% |
38 / 49 |
|
0.00% |
0 / 1 |
1.01 | |||
| getExtensions | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** @license For full copyright and license information, please see the LICENSE.md file. */ |
| 6 | |
| 7 | namespace App\Core\Bootstrap\Provider; |
| 8 | |
| 9 | defined('AMMONLY_APP') || exit('Direct script access is forbidden.'); |
| 10 | |
| 11 | use App\Core\Translation\DatabaseMessageSource; |
| 12 | use App\Core\Translation\LocaleContext; |
| 13 | use App\Core\Translation\LocaleContextInterface; |
| 14 | use App\Core\Translation\Middleware\LocaleDetectionMiddleware; |
| 15 | use App\Core\Translation\Presentation\Api\TranslationApiController; |
| 16 | use App\Modules\User\Infrastructure\Repository\SqlUserRepository; |
| 17 | use App\Modules\User\Presentation\Api\LocaleApiController; |
| 18 | use Nyholm\Psr7\Factory\Psr17Factory; |
| 19 | use PDO; |
| 20 | use Psr\Container\ContainerInterface; |
| 21 | use Yiisoft\Di\ServiceProviderInterface; |
| 22 | use Yiisoft\Session\SessionInterface; |
| 23 | use Yiisoft\Translator\CategorySource; |
| 24 | use Yiisoft\Translator\SimpleMessageFormatter; |
| 25 | use Yiisoft\Translator\Translator; |
| 26 | use Yiisoft\Translator\TranslatorInterface; |
| 27 | use 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 | */ |
| 36 | final 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 | } |