Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
CacheDecorator | |
100.00% |
17 / 17 |
|
100.00% |
4 / 4 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
fromFile | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
fromString | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
generateKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * Cache decorator. |
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 Config |
25 | * @author David Maus <maus@hab.de> |
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\Config\Reader; |
31 | |
32 | use Laminas\Cache\Storage\StorageInterface; |
33 | use Laminas\Config\Reader\ReaderInterface; |
34 | |
35 | /** |
36 | * This class decorates a configuration file reader with caching support. |
37 | * |
38 | * @category VuFind |
39 | * @package Config |
40 | * @author David Maus <maus@hab.de> |
41 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
42 | * @link https://vufind.org Main Site |
43 | */ |
44 | class CacheDecorator implements ReaderInterface |
45 | { |
46 | /** |
47 | * The decorated reader. |
48 | * |
49 | * @var ReaderInterface |
50 | */ |
51 | protected $reader; |
52 | |
53 | /** |
54 | * Cache storage. |
55 | * |
56 | * @var StorageInterface |
57 | */ |
58 | protected $storage; |
59 | |
60 | /** |
61 | * Constructor. |
62 | * |
63 | * @param ReaderInterface $reader Config reader |
64 | * @param StorageInterface $storage Cache storage |
65 | * |
66 | * @return void |
67 | */ |
68 | public function __construct(ReaderInterface $reader, StorageInterface $storage) |
69 | { |
70 | $this->reader = $reader; |
71 | $this->storage = $storage; |
72 | } |
73 | |
74 | /** |
75 | * Read from a file and create an array |
76 | * |
77 | * @param string $filename Filename |
78 | * |
79 | * @return array |
80 | */ |
81 | public function fromFile($filename) |
82 | { |
83 | $absFilename = realpath($filename); |
84 | $mtime = @filemtime($absFilename); |
85 | $key = $this->generateKey($mtime . $absFilename); |
86 | if ($this->storage->hasItem($key)) { |
87 | return $this->storage->getItem($key); |
88 | } |
89 | $config = $this->reader->fromFile($filename); |
90 | $this->storage->setItem($key, $config); |
91 | return $config; |
92 | } |
93 | |
94 | /** |
95 | * Read from a string and create an array |
96 | * |
97 | * @param string $string String |
98 | * |
99 | * @return array|bool |
100 | */ |
101 | public function fromString($string) |
102 | { |
103 | $key = $this->generateKey($string); |
104 | if ($this->storage->hasItem($key)) { |
105 | return $this->storage->getItem($key); |
106 | } |
107 | $config = $this->reader->fromString($string); |
108 | $this->storage->setItem($key, $config); |
109 | return $config; |
110 | } |
111 | |
112 | /// Internal API |
113 | |
114 | /** |
115 | * Return a cache key. |
116 | * |
117 | * @param string $string String |
118 | * |
119 | * @return string |
120 | */ |
121 | protected function generateKey($string) |
122 | { |
123 | return md5($string); |
124 | } |
125 | } |