xref: /dokuwiki/vendor/simplepie/simplepie/src/Cache.php (revision 8e88a29b81301f78509349ab1152bb09c229123e)
1<?php
2
3// SPDX-FileCopyrightText: 2004-2023 Ryan Parman, Sam Sneddon, Ryan McCue
4// SPDX-License-Identifier: BSD-3-Clause
5
6declare(strict_types=1);
7
8namespace SimplePie;
9
10use SimplePie\Cache\Base;
11
12/**
13 * Used to create cache objects
14 *
15 * This class can be overloaded with {@see SimplePie::set_cache_class()},
16 * although the preferred way is to create your own handler
17 * via {@see register()}
18 *
19 * @deprecated since SimplePie 1.8.0, use "SimplePie\SimplePie::set_cache()" instead
20 */
21class Cache
22{
23    /**
24     * Cache handler classes
25     *
26     * These receive 3 parameters to their constructor, as documented in
27     * {@see register()}
28     * @var array<string, class-string<Base>>
29     */
30    protected static $handlers = [
31        'mysql'     => Cache\MySQL::class,
32        'memcache'  => Cache\Memcache::class,
33        'memcached' => Cache\Memcached::class,
34        'redis'     => Cache\Redis::class,
35    ];
36
37    /**
38     * Don't call the constructor. Please.
39     */
40    private function __construct()
41    {
42    }
43
44    /**
45     * Create a new SimplePie\Cache object
46     *
47     * @param string $location URL location (scheme is used to determine handler)
48     * @param string $filename Unique identifier for cache object
49     * @param Base::TYPE_FEED|Base::TYPE_IMAGE $extension 'spi' or 'spc'
50     * @return Base Type of object depends on scheme of `$location`
51     */
52    public static function get_handler(string $location, string $filename, $extension)
53    {
54        $type = explode(':', $location, 2);
55        $type = $type[0];
56        if (!empty(self::$handlers[$type])) {
57            $class = self::$handlers[$type];
58            return new $class($location, $filename, $extension);
59        }
60
61        return new \SimplePie\Cache\File($location, $filename, $extension);
62    }
63
64    /**
65     * Create a new SimplePie\Cache object
66     *
67     * @deprecated since SimplePie 1.3.1, use {@see get_handler()} instead
68     * @param string $location
69     * @param string $filename
70     * @param Base::TYPE_FEED|Base::TYPE_IMAGE $extension
71     * @return Base
72     */
73    public function create(string $location, string $filename, $extension)
74    {
75        trigger_error('Cache::create() has been replaced with Cache::get_handler() since SimplePie 1.3.1, use the registry system instead.', \E_USER_DEPRECATED);
76
77        return self::get_handler($location, $filename, $extension);
78    }
79
80    /**
81     * Register a handler
82     *
83     * @param string $type DSN type to register for
84     * @param class-string<Base> $class Name of handler class. Must implement Base
85     * @return void
86     */
87    public static function register(string $type, $class)
88    {
89        self::$handlers[$type] = $class;
90    }
91
92    /**
93     * Parse a URL into an array
94     *
95     * @param string $url
96     * @return array<string, mixed>
97     */
98    public static function parse_URL(string $url)
99    {
100        $parsedUrl = parse_url($url);
101
102        if ($parsedUrl === false) {
103            return [];
104        }
105
106        $params = array_merge($parsedUrl, ['extras' => []]);
107        if (isset($params['query'])) {
108            parse_str($params['query'], $params['extras']);
109        }
110        return $params;
111    }
112}
113
114class_alias('SimplePie\Cache', 'SimplePie_Cache');
115