Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
LowerCaseServiceNameTrait | |
100.00% |
7 / 7 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getNormalizedServiceName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
5 |
1 | <?php |
2 | |
3 | /** |
4 | * Trait for plugin managers that allows service names to be normalized to lowercase |
5 | * (for backward compatibility with ServiceManager v2). |
6 | * |
7 | * PHP version 8 |
8 | * |
9 | * Copyright (C) Villanova University 2017. |
10 | * |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License version 2, |
13 | * as published by the Free Software Foundation. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License |
21 | * along with this program; if not, write to the Free Software |
22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 | * |
24 | * @category VuFind |
25 | * @package ServiceManager |
26 | * @author Demian Katz <demian.katz@villanova.edu> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link https://vufind.org/wiki/development Wiki |
29 | */ |
30 | |
31 | namespace VuFind\ServiceManager; |
32 | |
33 | /** |
34 | * Trait for plugin managers that allows service names to be normalized to lowercase |
35 | * (for backward compatibility with ServiceManager v2). |
36 | * |
37 | * @category VuFind |
38 | * @package ServiceManager |
39 | * @author Demian Katz <demian.katz@villanova.edu> |
40 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
41 | * @link https://vufind.org/wiki/development Wiki |
42 | */ |
43 | trait LowerCaseServiceNameTrait |
44 | { |
45 | /** |
46 | * Retrieve a plugin |
47 | * |
48 | * @param string $name Name of plugin |
49 | * @param null|array $options Options to use when creating the instance. |
50 | * |
51 | * @return mixed |
52 | */ |
53 | public function get($name, array $options = null) |
54 | { |
55 | return parent::get($this->getNormalizedServiceName($name), $options); |
56 | } |
57 | |
58 | /** |
59 | * Returns true if the container can return an entry for the given identifier. |
60 | * Returns false otherwise. |
61 | * |
62 | * @param string $id Identifier of the entry to look for. |
63 | * |
64 | * @return bool |
65 | */ |
66 | public function has($id) |
67 | { |
68 | return parent::has($this->getNormalizedServiceName($id)); |
69 | } |
70 | |
71 | /** |
72 | * Hack for backward compatibility with services defined under |
73 | * ServiceManager v2, when service names were case-insensitive. |
74 | * TODO: set up aliases and/or normalize code to eliminate the need for this. |
75 | * |
76 | * @param string $name Service name |
77 | * |
78 | * @return string |
79 | */ |
80 | protected function getNormalizedServiceName($name) |
81 | { |
82 | if ( |
83 | $name != ($lower = strtolower($name)) |
84 | && (isset($this->services[$lower]) || isset($this->factories[$lower]) |
85 | || isset($this->aliases[$lower])) |
86 | ) { |
87 | return $lower; |
88 | } |
89 | return $name; |
90 | } |
91 | } |