FileCache.php 5.13 KB
Newer Older
speedprogs_de's avatar
speedprogs_de committed
1
<?php
2
3
namespace T3o\TerFe2\Cache;

4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*******************************************************************
 *  Copyright notice
 *
 *  (c) 2011 Kai Vogel <kai.vogel@speedprogs.de>, Speedprogs.de
 *
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
 *  free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *
 *  The GNU General Public License can be found at
 *  http://www.gnu.org/copyleft/gpl.html.
 *
 *  This script is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  This copyright notice MUST APPEAR in all copies of the script!
 ******************************************************************/

/**
 * Cache for extension files
 */
31
class FileCache implements \TYPO3\CMS\Core\SingletonInterface
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{

    /**
     * @var string
     */
    protected $cacheDirectory;


    /**
     * Set cache directory path
     *
     * @param string $cacheDirectory Path to cache directory
     * @return void
     */
    public function setCacheDirectory($cacheDirectory)
    {
        if (empty($cacheDirectory)) {
49
            throw new \Exception('An empty cache directory is not allowed');
50
        }
51
        $this->cacheDirectory = \T3o\TerFe2\Utility\File::getAbsoluteDirectory($cacheDirectory);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    }


    /**
     * Get cache directory path
     *
     * @return string Path of the cache directory
     */
    public function getCacheDirectory()
    {
        return $this->cacheDirectory;
    }


    /**
     * Get filename
     *
     * @param string $filename Name of the file
     * @return string Local filename
     */
    public function getFile($filename)
    {
        $filename = $this->getFilePath($filename);
75
        if (\T3o\TerFe2\Utility\File::fileExists($filename)) {
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
            return $filename;
        }
        return '';
    }


    /**
     * Returns an extension related filename
     *
     * @param string $extensionKey Extension key
     * @param string $filename Name of the file
     * @return string Local filename
     */
    public function getExtensionFile($extensionKey, $filename)
    {
        $filename = $this->getExtensionFilename($extensionKey, $filename);
        return $this->getFile($filename);
    }


    /**
     * Copy a file to local cache
     *
     * @param string $fileUrl Url to the file
     * @param string $filename Name of the file
     * @return string Local filename
     */
    public function addFile($fileUrl, $filename)
    {
        if (empty($fileUrl)) {
            return '';
        }
        $filename = $this->getFilePath($filename);
109
        if (\T3o\TerFe2\Utility\File::copyFile($fileUrl, $filename)) {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
            return $filename;
        }
        return '';
    }


    /**
     * Copy an extension file to local cache
     *
     * @param string $extensionKey Extension key
     * @param string $fileUrl Url to the file
     * @param string $filename Name of the file
     * @return string Local filename
     */
    public function addExtensionFile($extensionKey, $fileUrl, $filename)
    {
        $filename = $this->getExtensionFilename($extensionKey, $filename);
        return $this->addFile($fileUrl, $filename);
    }


    /**
     * Remove a file from local cache
     *
     * @param string $filename Name of the file
     * @return boolean TRUE if success
     */
    public function removeFile($filename)
    {
        $filename = $this->getFilePath($filename);
140
        if (!\T3o\TerFe2\Utility\File::fileExists($fileUrl)) {
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
            return FALSE;
        }
        return unlink($filename);
    }


    /**
     * Remove an extension file from local cache
     *
     * @param string $extensionKey Extension key
     * @param string $filename Name of the file
     * @return string Local filename
     */
    public function removeExtensionFile($extensionKey, $filename)
    {
        $filename = $this->getExtensionFilename($extensionKey, $filename);
        return $this->removeFile($filename);
    }


    /**
     * Returns the filename of a file
     *
     * @param string $filename Name of the file
     * @return string Absolute path to file
     */
    protected function getFilePath($filename)
    {
        if (empty($filename)) {
            return '';
        }
        $cacheDirectory = $this->getCacheDirectory();
        if (empty($cacheDirectory)) {
174
            throw new \Exception('No cache directory defined');
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
        }
        return $cacheDirectory . $filename;
    }


    /**
     * Returns the filename of an extension file
     *
     * @param string $extensionKey Extension key
     * @param string $filename Name of the file
     * @return string Extension filename
     */
    protected function getExtensionFilename($extensionKey, $filename)
    {
        if (empty($extensionKey) || empty($filename)) {
            return '';
        }
        return strtolower($extensionKey) . '/' . basename($filename);
    }

}