Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
KeyGeneratorTrait
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
13.78
0.00% covered (danger)
0.00%
0 / 1
 getCacheKey
40.00% covered (danger)
40.00%
4 / 10
0.00% covered (danger)
0.00%
0 / 1
13.78
1<?php
2
3/**
4 * VuFind Cache Key Generator Trait
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Leipzig University Library 2016.
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  Cache
25 * @author   AndrĂ© Lahmann <lahmann@ub.uni-leipzig.de>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org/wiki/development:architecture:caching
28 */
29
30namespace VuFind\Cache;
31
32use function get_class;
33
34/**
35 * VuFind Cache Key Generator Trait
36 *
37 * Provides functions for generating uniform cache keys.
38 *
39 * @category VuFind
40 * @package  Cache
41 * @author   AndrĂ© Lahmann <lahmann@ub.uni-leipzig.de>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org/wiki/development:architecture:caching
44 */
45trait KeyGeneratorTrait
46{
47    /**
48     * Method to ensure uniform cache keys for cached VuFind objects.
49     *
50     * @param string|null $suffix Optional suffix that will get appended to the
51     * object class name calling getCacheKey()
52     *
53     * @return string
54     */
55    protected function getCacheKey($suffix = null)
56    {
57        // Build the raw key combining the calling classname with an optional suffix
58        $key = get_class($this) . (!empty($suffix) ? '_' . $suffix : '');
59
60        // Test the build key
61        if (
62            $this->cache
63            && ($keyPattern = $this->cache->getOptions()->getKeyPattern())
64            && !preg_match($keyPattern, $key)
65        ) {
66            // The key violates the currently set StorageAdapter key_pattern. Our
67            // best guess is to remove any characters that do not match the only
68            // default key_pattern for Laminas\Cache\StorageAdapters: the filesystem
69            // adapter (default key_pattern "/^[a-z0-9_\+\-]*$/Di").
70            // Any other custom pattern is assumed as less restrictive, thus the
71            // transformed key should match the custom pattern.
72            $key = preg_replace(
73                "/([^a-z0-9_\+\-])+/Di",
74                '',
75                $key
76            );
77        }
78
79        // If we are in production mode reduce the rawkey to md5 hash which will
80        // keep the key length at handy 32hex
81        return APPLICATION_ENV == 'production' ? md5($key) : $key;
82    }
83}