Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/classes/traits/trait-memoize.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
namespace WP_Rocket\Traits;
3
+
4
+
/**
5
+
* Statically store values.
6
+
*
7
+
* @since 3.3
8
+
*/
9
+
trait Memoize {
10
+
11
+
/**
12
+
* Store the values.
13
+
*
14
+
* @var array
15
+
* @since 3.3
16
+
*/
17
+
private static $memoized = [];
18
+
19
+
/**
20
+
* Tell if a value is memoized.
21
+
*
22
+
* @since 3.3
23
+
*
24
+
* @param string $method Name of the method.
25
+
* @param array $args Arguments passed to the parent method. It is used to build a hash.
26
+
* @return bool
27
+
*/
28
+
final public static function is_memoized( $method, $args = [] ) {
29
+
$hash = self::get_memoize_args_hash( $args );
30
+
return isset( self::$memoized[ $method ][ $hash ] );
31
+
}
32
+
33
+
/**
34
+
* Get a stored value.
35
+
*
36
+
* @since 3.3
37
+
*
38
+
* @param string $method Name of the method.
39
+
* @param array $args Arguments passed to the parent method. It is used to build a hash.
40
+
* @return mixed
41
+
*/
42
+
final public static function get_memoized( $method, $args = [] ) {
43
+
$hash = self::get_memoize_args_hash( $args );
44
+
return isset( self::$memoized[ $method ][ $hash ] ) ? self::$memoized[ $method ][ $hash ] : null;
45
+
}
46
+
47
+
/**
48
+
* Cache a value.
49
+
*
50
+
* @since 3.3
51
+
*
52
+
* @param string $method Name of the method.
53
+
* @param array $args Arguments passed to the parent method. It is used to build a hash.
54
+
* @param mixed $value Value to store.
55
+
* @return mixed The stored value.
56
+
*/
57
+
final public static function memoize( $method, $args = [], $value = null ) {
58
+
$hash = self::get_memoize_args_hash( $args );
59
+
60
+
if ( ! isset( self::$memoized[ $method ] ) ) {
61
+
self::$memoized[ $method ] = [];
62
+
}
63
+
64
+
self::$memoized[ $method ][ $hash ] = $value;
65
+
return self::$memoized[ $method ][ $hash ]; //phpcs:ignore Universal.CodeAnalysis.ConstructorDestructorReturn.ReturnValueFound
66
+
}
67
+
68
+
/**
69
+
* Create a hash based on an array of arguments.
70
+
*
71
+
* @since 3.3
72
+
*
73
+
* @param array $args An array of arguments.
74
+
* @return string
75
+
*/
76
+
private static function get_memoize_args_hash( $args ) {
77
+
if ( [] === $args ) {
78
+
return 'd751713988987e9331980363e24189ce'; // `md5( json_encode( [] ) )`
79
+
}
80
+
81
+
return md5( call_user_func( 'json_encode', $args ) );
82
+
}
83
+
}
84
+