1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Odan\PlatesAsset; |
4
|
|
|
|
5
|
|
|
use DirectoryIterator; |
6
|
|
|
use FilesystemIterator; |
7
|
|
|
use RuntimeException; |
8
|
|
|
use SplFileInfo; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Asset Cache for the internal JS ans CSS files. |
12
|
|
|
*/ |
13
|
|
|
class PlatesAssetsCache |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Cache. |
17
|
|
|
* |
18
|
|
|
* @var string Path |
19
|
|
|
*/ |
20
|
|
|
protected $directory; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int File mode |
24
|
|
|
*/ |
25
|
|
|
protected $chmod = -1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create new instance. |
29
|
|
|
* |
30
|
|
|
* @param string $publicDir Public directory |
31
|
|
|
* @param int $chmod Changes file mode (optional) |
32
|
|
|
*/ |
33
|
9 |
|
public function __construct(string $publicDir, int $chmod = -1) |
34
|
|
|
{ |
35
|
9 |
|
$this->directory = $publicDir; |
36
|
9 |
|
$this->chmod = $chmod; |
37
|
|
|
|
38
|
9 |
|
if (!file_exists($this->directory)) { |
39
|
1 |
|
throw new RuntimeException("Path {$this->directory} not found"); |
40
|
|
|
} |
41
|
9 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Clear the existing cache. |
45
|
|
|
* |
46
|
|
|
* @return bool Success |
47
|
|
|
*/ |
48
|
|
|
public function clearCache(): bool |
49
|
|
|
{ |
50
|
|
|
return $this->removeDirectory($this->directory); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Remove directory recursively. |
55
|
|
|
* This function is compatible with vfsStream. |
56
|
|
|
* |
57
|
|
|
* @param string $path Path |
58
|
|
|
* |
59
|
|
|
* @return bool true on success or false on failure |
60
|
|
|
*/ |
61
|
|
|
private function removeDirectory(string $path): bool |
62
|
|
|
{ |
63
|
|
|
$iterator = new DirectoryIterator($path); |
64
|
|
|
foreach ($iterator as $fileInfo) { |
65
|
|
|
if ($fileInfo->isDot() || !$fileInfo->isDir()) { |
66
|
|
|
continue; |
67
|
|
|
} |
68
|
|
|
$dirName = $fileInfo->getPathname(); |
69
|
|
|
$this->removeDirectory($dirName); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$files = new FilesystemIterator($path); |
73
|
|
|
|
74
|
|
|
/** @var SplFileInfo $file */ |
75
|
|
|
foreach ($files as $file) { |
76
|
|
|
$fileName = $file->getPathname(); |
77
|
|
|
unlink($fileName); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return rmdir($path); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns url for filename. |
85
|
|
|
* |
86
|
|
|
* @param string $fileName The filename |
87
|
|
|
* @param string $content The content |
88
|
|
|
* @param string $urlBasePath The url base path |
89
|
|
|
* |
90
|
|
|
* @return string The url |
91
|
|
|
*/ |
92
|
7 |
|
public function createCacheBustedUrl(string $fileName, string $content, string $urlBasePath): string |
93
|
|
|
{ |
94
|
7 |
|
$cacheFile = $this->createPublicCacheFile($fileName, $content); |
95
|
|
|
|
96
|
7 |
|
return $urlBasePath . pathinfo($cacheFile, PATHINFO_BASENAME); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create cache file from fileName. |
101
|
|
|
* |
102
|
|
|
* @param string $fileName The filename |
103
|
|
|
* @param string $content The content |
104
|
|
|
* |
105
|
|
|
* @return string The cache filename |
106
|
|
|
*/ |
107
|
7 |
|
private function createPublicCacheFile(string $fileName, string $content): string |
108
|
|
|
{ |
109
|
7 |
|
$extension = strtolower(pathinfo($fileName, PATHINFO_EXTENSION)); |
110
|
7 |
|
if (empty($extension)) { |
111
|
1 |
|
$extension = 'cache'; |
112
|
|
|
} |
113
|
|
|
|
114
|
7 |
|
$name = pathinfo($fileName, PATHINFO_FILENAME); |
115
|
7 |
|
$checksum = sha1($fileName . $content); |
116
|
7 |
|
$cacheFile = $this->directory . '/' . $name . '.' . $checksum . '.' . $extension; |
117
|
|
|
|
118
|
7 |
|
file_put_contents($cacheFile, $content); |
119
|
|
|
|
120
|
7 |
|
if ($this->chmod > -1) { |
121
|
7 |
|
chmod($cacheFile, $this->chmod); |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
return $cacheFile; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|