Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
TranslatorFactory | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
20 | |
0.00% |
0 / 1 |
__invoke | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
6 | |||
enableCaching | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Translator factory. |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2019. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Translator |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
27 | * @link https://vufind.org Main Site |
28 | */ |
29 | |
30 | namespace VuFind\I18n\Translator; |
31 | |
32 | use Laminas\I18n\Translator\TranslatorInterface; |
33 | use Laminas\ServiceManager\Exception\ServiceNotCreatedException; |
34 | use Laminas\ServiceManager\Exception\ServiceNotFoundException; |
35 | use Laminas\ServiceManager\Factory\DelegatorFactoryInterface; |
36 | use Psr\Container\ContainerExceptionInterface as ContainerException; |
37 | use Psr\Container\ContainerInterface; |
38 | use VuFind\I18n\Locale\LocaleSettings; |
39 | |
40 | use function extension_loaded; |
41 | |
42 | /** |
43 | * Translator factory. |
44 | * |
45 | * @category VuFind |
46 | * @package Translator |
47 | * @author Demian Katz <demian.katz@villanova.edu> |
48 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
49 | * @link https://vufind.org Main Site |
50 | */ |
51 | class TranslatorFactory implements DelegatorFactoryInterface |
52 | { |
53 | use \VuFind\I18n\Translator\LanguageInitializerTrait; |
54 | |
55 | /** |
56 | * A factory that creates delegates of a given service |
57 | * |
58 | * @param ContainerInterface $container Container |
59 | * @param string $name Service name |
60 | * @param callable $callback Primary factory |
61 | * @param null|array $options Options |
62 | * |
63 | * @return object |
64 | * @throws ServiceNotFoundException if unable to resolve the service. |
65 | * @throws ServiceNotCreatedException if an exception is raised when |
66 | * creating a service. |
67 | * @throws ContainerException&\Throwable if any other error occurs |
68 | * |
69 | * @SuppressWarnings(PHPMD.UnusedFormalParameter) |
70 | */ |
71 | public function __invoke( |
72 | ContainerInterface $container, |
73 | $name, |
74 | callable $callback, |
75 | array $options = null |
76 | ) { |
77 | $translator = $callback(); |
78 | if (!extension_loaded('intl')) { |
79 | error_log( |
80 | 'Translation broken due to missing PHP intl extension.' |
81 | ); |
82 | return $translator; |
83 | } |
84 | $settings = $container->get(LocaleSettings::class); |
85 | $language = $settings->getUserLocale(); |
86 | $this->enableCaching($translator, $container); |
87 | $this->addLanguageToTranslator($translator, $settings, $language); |
88 | |
89 | return $translator; |
90 | } |
91 | |
92 | /** |
93 | * Add caching to a translator object |
94 | * |
95 | * @param TranslatorInterface $translator Translator object |
96 | * @param ContainerInterface $container Service manager |
97 | * |
98 | * @return void |
99 | */ |
100 | protected function enableCaching( |
101 | TranslatorInterface $translator, |
102 | ContainerInterface $container |
103 | ): void { |
104 | // Set up language caching for better performance: |
105 | try { |
106 | $translator->setCache( |
107 | $container->get(\VuFind\Cache\Manager::class)->getCache('language') |
108 | ); |
109 | } catch (\Exception $e) { |
110 | // Don't let a cache failure kill the whole application, but make |
111 | // note of it: |
112 | $logger = $container->get(\VuFind\Log\Logger::class); |
113 | $logger->debug( |
114 | 'Problem loading cache: ' . $e::class . ' exception: ' |
115 | . $e->getMessage() |
116 | ); |
117 | } |
118 | } |
119 | } |