diff options
author | Aurélien Tamisier <virtualtam+github@flibidi.net> | 2019-01-18 21:26:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-18 21:26:03 +0100 |
commit | ff3b5dc5542ec150f0d9b447394364a15e9156d0 (patch) | |
tree | 5e926e36816d510e3b3a10e20b94c23f43b55092 /application/feed/CachedPage.php | |
parent | 1826e383ecf501302974132fd443cf1ca06e10f6 (diff) | |
parent | dea72c711ff740b3b829d238fcf85648465143a0 (diff) | |
download | Shaarli-ff3b5dc5542ec150f0d9b447394364a15e9156d0.tar.gz Shaarli-ff3b5dc5542ec150f0d9b447394364a15e9156d0.tar.zst Shaarli-ff3b5dc5542ec150f0d9b447394364a15e9156d0.zip |
Merge pull request #1248 from virtualtam/refactor/namespacing
Ensure all PHP classes are properly namespaced
Diffstat (limited to 'application/feed/CachedPage.php')
-rw-r--r-- | application/feed/CachedPage.php | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/application/feed/CachedPage.php b/application/feed/CachedPage.php new file mode 100644 index 00000000..d809bdd9 --- /dev/null +++ b/application/feed/CachedPage.php | |||
@@ -0,0 +1,62 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Feed; | ||
4 | |||
5 | /** | ||
6 | * Simple cache system, mainly for the RSS/ATOM feeds | ||
7 | */ | ||
8 | class CachedPage | ||
9 | { | ||
10 | // Directory containing page caches | ||
11 | private $cacheDir; | ||
12 | |||
13 | // Should this URL be cached (boolean)? | ||
14 | private $shouldBeCached; | ||
15 | |||
16 | // Name of the cache file for this URL | ||
17 | private $filename; | ||
18 | |||
19 | /** | ||
20 | * Creates a new CachedPage | ||
21 | * | ||
22 | * @param string $cacheDir page cache directory | ||
23 | * @param string $url page URL | ||
24 | * @param bool $shouldBeCached whether this page needs to be cached | ||
25 | */ | ||
26 | public function __construct($cacheDir, $url, $shouldBeCached) | ||
27 | { | ||
28 | // TODO: check write access to the cache directory | ||
29 | $this->cacheDir = $cacheDir; | ||
30 | $this->filename = $this->cacheDir . '/' . sha1($url) . '.cache'; | ||
31 | $this->shouldBeCached = $shouldBeCached; | ||
32 | } | ||
33 | |||
34 | /** | ||
35 | * Returns the cached version of a page, if it exists and should be cached | ||
36 | * | ||
37 | * @return string a cached version of the page if it exists, null otherwise | ||
38 | */ | ||
39 | public function cachedVersion() | ||
40 | { | ||
41 | if (!$this->shouldBeCached) { | ||
42 | return null; | ||
43 | } | ||
44 | if (is_file($this->filename)) { | ||
45 | return file_get_contents($this->filename); | ||
46 | } | ||
47 | return null; | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * Puts a page in the cache | ||
52 | * | ||
53 | * @param string $pageContent XML content to cache | ||
54 | */ | ||
55 | public function cache($pageContent) | ||
56 | { | ||
57 | if (!$this->shouldBeCached) { | ||
58 | return; | ||
59 | } | ||
60 | file_put_contents($this->filename, $pageContent); | ||
61 | } | ||
62 | } | ||