1
|
|
|
<?php |
2
|
|
|
namespace HtImgModule\Service; |
3
|
|
|
|
4
|
|
|
use HtImgModule\Options\CacheOptionsInterface; |
5
|
|
|
use Imagine\Image\ImageInterface; |
6
|
|
|
|
7
|
|
|
class CacheManager implements CacheManagerInterface |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var CacheOptionsInterface |
11
|
|
|
*/ |
12
|
|
|
protected $cacheOptions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructor |
16
|
|
|
* |
17
|
|
|
* @param CacheOptionsInterface $cacheOptions |
18
|
|
|
*/ |
19
|
2 |
|
public function __construct(CacheOptionsInterface $cacheOptions) |
20
|
|
|
{ |
21
|
2 |
|
$this->cacheOptions = $cacheOptions; |
22
|
2 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
1 |
|
public function cacheExists($relativeName, $filter, $formatOrImage = null) |
28
|
|
|
{ |
29
|
1 |
|
$cachePath = $this->getCachePath($relativeName, $filter, $formatOrImage); |
30
|
1 |
|
if (is_file($cachePath) && is_readable($cachePath)) { |
31
|
1 |
|
if ((time() - filemtime($cachePath)) < $this->cacheOptions->getCacheExpiry()) { |
32
|
1 |
|
return true; |
33
|
|
|
} |
34
|
|
|
unlink($cachePath); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function getCacheUrl($relativeName, $filter, $formatOrImage = null) |
44
|
|
|
{ |
45
|
1 |
|
if (is_readable($formatOrImage)) { |
46
|
1 |
|
$format = pathinfo($formatOrImage, PATHINFO_EXTENSION); |
47
|
1 |
|
} else { |
48
|
1 |
|
$format = $formatOrImage; |
49
|
|
|
} |
50
|
1 |
|
if (!$format) { |
51
|
1 |
|
return $this->cacheOptions->getCachePath() . '/' . $filter . '/'. $relativeName; |
52
|
|
|
} else { |
53
|
1 |
|
return $this->getCacheUrl($relativeName, $filter) . '.' . $format; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
1 |
|
public function getCachePath($relativeName, $filter, $formatOrImage = null) |
61
|
|
|
{ |
62
|
1 |
|
return $this->cacheOptions->getWebRoot() . '/' . $this->getCacheUrl($relativeName, $filter, $formatOrImage); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
1 |
|
public function createCache($relativeName, $filter, ImageInterface $image, $formatOrImage = null, array $saveOptions = []) |
69
|
|
|
{ |
70
|
1 |
|
$cachePath = $this->getCachePath($relativeName, $filter, $formatOrImage); |
71
|
1 |
|
if (!is_dir(dirname($cachePath))) { |
72
|
1 |
|
mkdir(dirname($cachePath), 0755, true); |
73
|
1 |
|
} |
74
|
1 |
|
$image->save($cachePath, $saveOptions); |
75
|
1 |
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* {@inheritDoc} |
79
|
|
|
*/ |
80
|
1 |
|
public function deleteCache($relativeName, $filter, $formatOrImage = null) |
81
|
|
|
{ |
82
|
1 |
|
$cachePath = $this->getCachePath($relativeName, $filter, $formatOrImage); |
83
|
1 |
|
if (is_readable($cachePath)) { |
84
|
1 |
|
unlink($cachePath); |
85
|
1 |
|
} |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritDoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function isCachingEnabled($filter, array $filterOptions) |
92
|
|
|
{ |
93
|
1 |
|
if (isset($filterOptions['enable_cache'])) { |
94
|
1 |
|
return (bool) $filterOptions['enable_cache']; |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return $this->cacheOptions->getEnableCache(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|