xref: /dokuwiki/vendor/simplepie/simplepie/src/Cache/File.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\Cache;
9
10/**
11 * Caches data to the filesystem
12 *
13 * @deprecated since SimplePie 1.8.0, use implementation of "Psr\SimpleCache\CacheInterface" instead
14 */
15class File implements Base
16{
17    /**
18     * Location string
19     *
20     * @see SimplePie::$cache_location
21     * @var string
22     */
23    protected $location;
24
25    /**
26     * Filename
27     *
28     * @var string
29     */
30    protected $filename;
31
32    /**
33     * File extension
34     *
35     * @var string
36     */
37    protected $extension;
38
39    /**
40     * File path
41     *
42     * @var string
43     */
44    protected $name;
45
46    /**
47     * Create a new cache object
48     *
49     * @param string $location Location string (from SimplePie::$cache_location)
50     * @param string $name Unique ID for the cache
51     * @param Base::TYPE_FEED|Base::TYPE_IMAGE $type Either TYPE_FEED for SimplePie data, or TYPE_IMAGE for image data
52     */
53    public function __construct(string $location, string $name, $type)
54    {
55        $this->location = $location;
56        $this->filename = $name;
57        $this->extension = $type;
58        $this->name = "$this->location/$this->filename.$this->extension";
59    }
60
61    /**
62     * Save data to the cache
63     *
64     * @param array<mixed>|\SimplePie\SimplePie $data Data to store in the cache. If passed a SimplePie object, only cache the $data property
65     * @return bool Successfulness
66     */
67    public function save($data)
68    {
69        if (file_exists($this->name) && is_writable($this->name) || file_exists($this->location) && is_writable($this->location)) {
70            if ($data instanceof \SimplePie\SimplePie) {
71                $data = $data->data;
72            }
73
74            $data = serialize($data);
75            return (bool) file_put_contents($this->name, $data);
76        }
77        return false;
78    }
79
80    /**
81     * Retrieve the data saved to the cache
82     *
83     * @return array<mixed>|false Data for SimplePie::$data
84     */
85    public function load()
86    {
87        if (file_exists($this->name) && is_readable($this->name)) {
88            return unserialize((string) file_get_contents($this->name));
89        }
90        return false;
91    }
92
93    /**
94     * Retrieve the last modified time for the cache
95     *
96     * @return int|false Timestamp
97     */
98    public function mtime()
99    {
100        return @filemtime($this->name);
101    }
102
103    /**
104     * Set the last modified time to the current time
105     *
106     * @return bool Success status
107     */
108    public function touch()
109    {
110        return @touch($this->name);
111    }
112
113    /**
114     * Remove the cache
115     *
116     * @return bool Success status
117     */
118    public function unlink()
119    {
120        if (file_exists($this->name)) {
121            return unlink($this->name);
122        }
123        return false;
124    }
125}
126
127class_alias('SimplePie\Cache\File', 'SimplePie_Cache_File');
128