Skip to content

Commit 4ba0e47

Browse files
committed
coding style tweaks
1 parent a24a0b6 commit 4ba0e47

File tree

6 files changed

+251
-175
lines changed

6 files changed

+251
-175
lines changed

src/Cache.php

Lines changed: 74 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
*
1919
* In short:
2020
*
21-
* allow to put in cache/get from fache a generic result from a known operation on a generic dataset,
21+
* allow to put in cache/get from cache a generic result from a known operation on a generic dataset,
2222
* taking in account options that affects the result
2323
*
24-
* The cache manager is agnostic about data format and only the operation is expected to be descripted by string
24+
* The cache manager is agnostic about data format and only the operation is expected to be described by string
2525
*
2626
*/
2727

@@ -32,38 +32,36 @@
3232
*/
3333
class Cache
3434
{
35-
3635
const CACHE_VERSION = 0;
3736

38-
3937
// directory used for storing data
40-
public static $cache_dir = false;
38+
public static $cacheDir = false;
4139

4240
// prefix for the storing data
4341
public static $prefix = 'scssphp_';
4442

4543
// force a refresh : 'once' for refreshing the first hit on a cache only, true to never use the cache in this hit
46-
public static $force_refresh = false;
44+
public static $forceFefresh = false;
4745

4846
// specifies the number of seconds after which data cached will be seen as 'garbage' and potentially cleaned up
49-
public static $gc_lifetime = 604800;
47+
public static $gcLifetime = 604800;
5048

51-
52-
// array of already refreshed cache if $force_refresh==='once'
49+
// array of already refreshed cache if $forceFefresh==='once'
5350
protected static $refreshed = [];
5451

55-
5652
/**
5753
* Constructor
54+
*
55+
* @param array $options
5856
*/
5957
public function __construct($options)
6058
{
61-
//check $cache_dir
59+
// check $cacheDir
6260
if (isset($options['cache_dir'])) {
63-
self::$cache_dir = $options['cache_dir'];
61+
self::$cacheDir = $options['cache_dir'];
6462
}
6563

66-
if (empty(self::$cache_dir)) {
64+
if (empty(self::$cacheDir)) {
6765
throw new Exception('cache_dir not set');
6866
}
6967

@@ -75,40 +73,42 @@ public function __construct($options)
7573
throw new Exception('prefix not set');
7674
}
7775

78-
if (isset($options['force_refresh'])) {
79-
self::$force_refresh = $options['force_refresh'];
76+
if (isset($options['forceRefresh'])) {
77+
self::$forceFefresh = $options['force_refresh'];
8078
}
8179

8280
self::checkCacheDir();
8381
}
8482

85-
8683
/**
87-
* Get the cached result of $operation on $what, which is known as dependant from the content of $options
84+
* Get the cached result of $operation on $what,
85+
* which is known as dependant from the content of $options
86+
*
87+
* @param string $operation parse, compile...
88+
* @param mixed $what content key (e.g., filename to be treated)
89+
* @param array $options any option that affect the operation result on the content
90+
* @param integer $lastModified last modified timestamp
8891
*
89-
* @param string $operation
90-
* parse, compile...
91-
* @param $what
92-
* content key (filename to be treated for instance)
93-
* @param array $options
94-
* any option that affect the operation result on the content
95-
* @param int $last_modified
9692
* @return mixed
97-
* @throws Exception
93+
*
94+
* @throws \Exception
9895
*/
99-
public function getCache($operation, $what, $options = array(), $last_modified = null)
96+
public function getCache($operation, $what, $options = [], $lastModified = null)
10097
{
98+
$fileCache = self::$cacheDir . self::cacheName($operation, $what, $options);
10199

102-
$fileCache = self::$cache_dir . self::cacheName($operation, $what, $options);
100+
if ((! self::$forceRefresh || (self::$forceRefresh === 'once' && isset(self::$refreshed[$fileCache])))
101+
&& file_exists($fileCache)
102+
) {
103+
$cacheTime = filemtime($fileCache);
103104

104-
if ((! self::$force_refresh || (self::$force_refresh === 'once' && isset(self::$refreshed[$fileCache])))
105-
and file_exists($fileCache)) {
106-
$cache_time = filemtime($fileCache);
107-
if ((is_null($last_modified) or $cache_time > $last_modified)
108-
and $cache_time + self::$gc_lifetime > time()) {
105+
if ((is_null($lastModified) || $cacheTime > $lastModified)
106+
&& $cacheTime + self::$gcLifetime > time()
107+
) {
109108
$c = file_get_contents($fileCache);
110109
$c = unserialize($c);
111-
if (is_array($c) and isset($c['value'])) {
110+
111+
if (is_array($c) && isset($c['value'])) {
112112
return $c['value'];
113113
}
114114
}
@@ -118,43 +118,45 @@ public function getCache($operation, $what, $options = array(), $last_modified =
118118
}
119119

120120
/**
121-
* Put in cache the result of $operation on $what, which is known as dependant from the content of $options
121+
* Put in cache the result of $operation on $what,
122+
* which is known as dependant from the content of $options
122123
*
123124
* @param string $operation
124-
* @param $what
125-
* @param $value
126-
* @param array $options
125+
* @param mixed $what
126+
* @param mixed $value
127+
* @param array $options
127128
*/
128-
public function setCache($operation, $what, $value, $options = array())
129+
public function setCache($operation, $what, $value, $options = [])
129130
{
130-
$fileCache = self::$cache_dir . self::cacheName($operation, $what, $options);
131+
$fileCache = self::$cacheDir . self::cacheName($operation, $what, $options);
131132

132-
$c = array('value' => $value);
133+
$c = ['value' => $value];
133134
$c = serialize($c);
134135
file_put_contents($fileCache, $c);
135136

136-
if (self::$force_refresh === 'once') {
137+
if (self::$forceRefresh === 'once') {
137138
self::$refreshed[$fileCache] = true;
138139
}
139140
}
140141

141-
142142
/**
143-
* get the cachename for the caching of $opetation on $what, which is known as dependant from the content of $options
143+
* Get the cache name for the caching of $operation on $what,
144+
* which is known as dependant from the content of $options
145+
*
144146
* @param string $operation
145-
* @param $what
146-
* @param array $options
147+
* @param mixed $what
148+
* @param array $options
149+
*
147150
* @return string
148151
*/
149-
private static function cacheName($operation, $what, $options = array())
152+
private static function cacheName($operation, $what, $options = [])
150153
{
151-
152-
$t = array(
154+
$t = [
153155
'version' => self::CACHE_VERSION,
154156
'operation' => $operation,
155157
'what' => $what,
156158
'options' => $options
157-
);
159+
];
158160

159161
$t = self::$prefix
160162
. sha1(json_encode($t))
@@ -164,53 +166,52 @@ private static function cacheName($operation, $what, $options = array())
164166
return $t;
165167
}
166168

167-
168169
/**
169-
* Check that the cache dir is existing and writeable
170-
* @throws Exception
170+
* Check that the cache dir exists and is writeable
171+
*
172+
* @throws \Exception
171173
*/
172174
public static function checkCacheDir()
173175
{
176+
self::$cacheDir = str_replace('\\', '/', self::$cacheDir);
177+
self::$cacheDir = rtrim(self::$cacheDir, '/') . '/';
174178

175-
self::$cache_dir = str_replace('\\', '/', self::$cache_dir);
176-
self::$cache_dir = rtrim(self::$cache_dir, '/') . '/';
177-
178-
if (! file_exists(self::$cache_dir)) {
179-
if (! mkdir(self::$cache_dir)) {
180-
throw new Exception('Cache directory couldn\'t be created: ' . self::$cache_dir);
179+
if (! file_exists(self::$cacheDir)) {
180+
if (! mkdir(self::$cacheDir)) {
181+
throw new Exception('Cache directory couldn\'t be created: ' . self::$cacheDir);
181182
}
182-
} elseif (! is_dir(self::$cache_dir)) {
183-
throw new Exception('Cache directory doesn\'t exist: ' . self::$cache_dir);
184-
} elseif (! is_writable(self::$cache_dir)) {
185-
throw new Exception('Cache directory isn\'t writable: ' . self::$cache_dir);
183+
} elseif (! is_dir(self::$cacheDir)) {
184+
throw new Exception('Cache directory doesn\'t exist: ' . self::$cacheDir);
185+
} elseif (! is_writable(self::$cacheDir)) {
186+
throw new Exception('Cache directory isn\'t writable: ' . self::$cacheDir);
186187
}
187188
}
188189

189190
/**
190191
* Delete unused cached files
191-
*
192192
*/
193193
public static function cleanCache()
194194
{
195195
static $clean = false;
196196

197-
198-
if ($clean || empty(self::$cache_dir)) {
197+
if ($clean || empty(self::$cacheDir)) {
199198
return;
200199
}
201200

202201
$clean = true;
203202

204203
// only remove files with extensions created by SCSSPHP Cache
205204
// css files removed based on the list files
206-
$remove_types = array('scsscache' => 1);
205+
$removeTypes = ['scsscache' => 1];
206+
207+
$files = scandir(self::$cacheDir);
207208

208-
$files = scandir(self::$cache_dir);
209209
if (! $files) {
210210
return;
211211
}
212212

213-
$check_time = time() - self::$gc_lifetime;
213+
$checkTime = time() - self::$gcLifetime;
214+
214215
foreach ($files as $file) {
215216
// don't delete if the file wasn't created with SCSSPHP Cache
216217
if (strpos($file, self::$prefix) !== 0) {
@@ -220,20 +221,19 @@ public static function cleanCache()
220221
$parts = explode('.', $file);
221222
$type = array_pop($parts);
222223

223-
224-
if (! isset($remove_types[$type])) {
224+
if (! isset($removeTypes[$type])) {
225225
continue;
226226
}
227227

228-
$full_path = self::$cache_dir . $file;
229-
$mtime = filemtime($full_path);
228+
$fullPath = self::$cacheDir . $file;
229+
$mtime = filemtime($fullPath);
230230

231231
// don't delete if it's a relatively new file
232-
if ($mtime > $check_time) {
232+
if ($mtime > $checkTime) {
233233
continue;
234234
}
235235

236-
unlink($full_path);
236+
unlink($fullPath);
237237
}
238238
}
239239
}

0 commit comments

Comments
 (0)