Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
50.00% |
15 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ServiceInitializer | |
50.00% |
15 / 30 |
|
0.00% |
0 / 2 |
38.50 | |
0.00% |
0 / 1 |
isCacheEnabled | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
__invoke | |
83.33% |
15 / 18 |
|
0.00% |
0 / 1 |
8.30 |
1 | <?php |
2 | |
3 | /** |
4 | * VuFind Service Initializer |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
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 ServiceManager |
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/wiki/development Wiki |
28 | */ |
29 | |
30 | namespace VuFind\ServiceManager; |
31 | |
32 | use Laminas\ServiceManager\Initializer\InitializerInterface; |
33 | use Psr\Container\ContainerInterface; |
34 | |
35 | /** |
36 | * VuFind Service Initializer |
37 | * |
38 | * @category VuFind |
39 | * @package ServiceManager |
40 | * @author Demian Katz <demian.katz@villanova.edu> |
41 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
42 | * @link https://vufind.org/wiki/development Wiki |
43 | */ |
44 | class ServiceInitializer implements InitializerInterface |
45 | { |
46 | /** |
47 | * Check if the record cache is enabled within a service manager. |
48 | * |
49 | * @param ContainerInterface $sm Service manager |
50 | * |
51 | * @return bool |
52 | */ |
53 | protected function isCacheEnabled(ContainerInterface $sm) |
54 | { |
55 | // Use static cache to save time on repeated lookups: |
56 | static $enabled = null; |
57 | if (null === $enabled) { |
58 | // Return true if Record Cache is enabled for any data source |
59 | $cacheConfig = $sm->get(\VuFind\Config\PluginManager::class) |
60 | ->get('RecordCache'); |
61 | $enabled = false; |
62 | foreach ($cacheConfig as $section) { |
63 | foreach ($section as $setting) { |
64 | if ( |
65 | isset($setting['operatingMode']) |
66 | && $setting['operatingMode'] !== 'disabled' |
67 | ) { |
68 | $enabled = true; |
69 | break 2; // quit looping -- we know the answer! |
70 | } |
71 | } |
72 | } |
73 | } |
74 | return $enabled; |
75 | } |
76 | |
77 | /** |
78 | * Given an instance and a Service Manager, initialize the instance. |
79 | * |
80 | * @param ContainerInterface $sm Service manager |
81 | * @param object $instance Instance to initialize |
82 | * |
83 | * @return object |
84 | */ |
85 | public function __invoke(ContainerInterface $sm, $instance) |
86 | { |
87 | if ($instance instanceof \VuFind\Db\Table\DbTableAwareInterface) { |
88 | $instance->setDbTableManager( |
89 | $sm->get(\VuFind\Db\Table\PluginManager::class) |
90 | ); |
91 | } |
92 | if ($instance instanceof \VuFind\Db\Service\DbServiceAwareInterface) { |
93 | $instance->setDbServiceManager( |
94 | $sm->get(\VuFind\Db\Service\PluginManager::class) |
95 | ); |
96 | } |
97 | if ($instance instanceof \Laminas\Log\LoggerAwareInterface) { |
98 | $instance->setLogger($sm->get(\VuFind\Log\Logger::class)); |
99 | } |
100 | if ($instance instanceof \VuFind\I18n\Translator\TranslatorAwareInterface) { |
101 | $instance->setTranslator($sm->get(\Laminas\Mvc\I18n\Translator::class)); |
102 | } |
103 | if ($instance instanceof \VuFindHttp\HttpServiceAwareInterface) { |
104 | $instance->setHttpService($sm->get(\VuFindHttp\HttpService::class)); |
105 | } |
106 | // Only inject cache if configuration enabled (to save resources): |
107 | if ( |
108 | $instance instanceof \VuFind\Record\Cache\RecordCacheAwareInterface |
109 | && $this->isCacheEnabled($sm) |
110 | ) { |
111 | $instance->setRecordCache($sm->get(\VuFind\Record\Cache::class)); |
112 | } |
113 | return $instance; |
114 | } |
115 | } |