-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cache.php
57 lines (41 loc) · 1.41 KB
/
Cache.php
1
2
3
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
/**
* Convenient fragment caching methods
*
* @package ThemePlate
* @since 0.1.0
*/
namespace ThemePlate;
use Error;
use ThemePlate\Cache\CacheManager;
use ThemePlate\Process\Tasks;
/**
* @method static false|mixed remember( string $key, callable $callback, int $expiration = 0 )
* @method static mixed|null forget( string $key, $default = null )
* @method static false|string file( string $key, string $path )
* @method static CacheManager assign( $field )
* @method static CacheManager reset()
*/
class Cache {
private static ?CacheManager $manager = null;
private static ?Tasks $tasks = null;
public static function __callStatic( string $name, array $arguments ) {
if ( ! self::$manager instanceof CacheManager ) {
self::$manager = new CacheManager( self::$tasks );
}
if ( method_exists( self::$manager, $name ) ) {
return call_user_func_array( array( self::$manager, $name ), $arguments );
}
throw new Error( 'Call to undefined method ' . __CLASS__ . '::' . $name . '()' );
}
/**
* Support for soft-expiration; `Cache::remember`* and `Cache::file` updates in the background
* >\**Except for using anonymous function as callback (closure)*
*/
public static function processor( Tasks $tasks = null ): ?Tasks {
if ( ! self::$tasks instanceof Tasks && class_exists( Tasks::class ) ) {
self::$tasks = $tasks ?? new Tasks( __CLASS__ );
}
return self::$tasks;
}
}