Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
71.03% |
76 / 107 |
|
33.33% |
3 / 9 |
CRAP | |
0.00% |
0 / 1 |
Less_Cache | |
71.03% |
76 / 107 |
|
33.33% |
3 / 9 |
94.24 | |
0.00% |
0 / 1 |
Get | |
74.42% |
32 / 43 |
|
0.00% |
0 / 1 |
21.84 | |||
Regen | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
Cache | |
80.00% |
8 / 10 |
|
0.00% |
0 / 1 |
3.07 | |||
OutputFile | |
40.00% |
2 / 5 |
|
0.00% |
0 / 1 |
4.94 | |||
CompiledName | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
SetCacheDir | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
CheckCacheDir | |
55.56% |
5 / 9 |
|
0.00% |
0 / 1 |
7.19 | |||
CleanCache | |
66.67% |
18 / 27 |
|
0.00% |
0 / 1 |
15.48 | |||
ListFiles | |
60.00% |
3 / 5 |
|
0.00% |
0 / 1 |
2.26 |
1 | <?php |
2 | |
3 | /** |
4 | * Utility for handling the generation and caching of css files |
5 | */ |
6 | class Less_Cache { |
7 | |
8 | /** @var string|false Directory less.php can use for storing data */ |
9 | public static $cache_dir = false; |
10 | |
11 | /** @var string Prefix for the storing data */ |
12 | public static $prefix = 'lessphp_'; |
13 | |
14 | /** @var string Prefix for the storing vars */ |
15 | public static $prefix_vars = 'lessphpvars_'; |
16 | |
17 | /** |
18 | * @var int Specifies the number of seconds after which data created by less.php will be seen |
19 | * as 'garbage' and potentially cleaned up |
20 | */ |
21 | public static $gc_lifetime = 604800; |
22 | |
23 | /** |
24 | * Save and reuse the results of compiled less files. |
25 | * The first call to Get() will generate css and save it. |
26 | * Subsequent calls to Get() with the same arguments will return the same css filename |
27 | * |
28 | * @param array $less_files Array of .less files to compile |
29 | * @param array $parser_options Array of compiler options |
30 | * @param array $modify_vars Array of variables |
31 | * @return string|false Name of the css file |
32 | */ |
33 | public static function Get( $less_files, $parser_options = [], $modify_vars = [] ) { |
34 | // check $cache_dir |
35 | if ( isset( $parser_options['cache_dir'] ) ) { |
36 | self::$cache_dir = $parser_options['cache_dir']; |
37 | } |
38 | |
39 | if ( empty( self::$cache_dir ) ) { |
40 | throw new Exception( 'cache_dir not set' ); |
41 | } |
42 | |
43 | if ( isset( $parser_options['prefix'] ) ) { |
44 | self::$prefix = $parser_options['prefix']; |
45 | } |
46 | |
47 | if ( empty( self::$prefix ) ) { |
48 | throw new Exception( 'prefix not set' ); |
49 | } |
50 | |
51 | if ( isset( $parser_options['prefix_vars'] ) ) { |
52 | self::$prefix_vars = $parser_options['prefix_vars']; |
53 | } |
54 | |
55 | if ( empty( self::$prefix_vars ) ) { |
56 | throw new Exception( 'prefix_vars not set' ); |
57 | } |
58 | |
59 | self::CheckCacheDir(); |
60 | $less_files = (array)$less_files; |
61 | |
62 | // create a file for variables |
63 | if ( !empty( $modify_vars ) ) { |
64 | $lessvars = Less_Parser::serializeVars( $modify_vars ); |
65 | $vars_file = self::$cache_dir . self::$prefix_vars . sha1( $lessvars ) . '.less'; |
66 | |
67 | if ( !file_exists( $vars_file ) ) { |
68 | file_put_contents( $vars_file, $lessvars ); |
69 | } |
70 | |
71 | $less_files += [ $vars_file => '/' ]; |
72 | } |
73 | |
74 | // generate name for compiled css file |
75 | $hash = md5( json_encode( $less_files ) ); |
76 | $list_file = self::$cache_dir . self::$prefix . $hash . '.list'; |
77 | |
78 | // check cached content |
79 | if ( !isset( $parser_options['use_cache'] ) || $parser_options['use_cache'] === true ) { |
80 | if ( file_exists( $list_file ) ) { |
81 | |
82 | self::ListFiles( $list_file, $list, $cached_name ); |
83 | $compiled_name = self::CompiledName( $list, $hash ); |
84 | |
85 | // if $cached_name is the same as the $compiled name, don't regenerate |
86 | if ( !$cached_name || $cached_name === $compiled_name ) { |
87 | |
88 | $output_file = self::OutputFile( $compiled_name, $parser_options ); |
89 | |
90 | if ( $output_file && file_exists( $output_file ) ) { |
91 | @touch( $list_file ); |
92 | return basename( $output_file ); // for backwards compatibility, we just return the name of the file |
93 | } |
94 | } |
95 | } |
96 | } |
97 | |
98 | $compiled = self::Cache( $less_files, $parser_options ); |
99 | if ( !$compiled ) { |
100 | return false; |
101 | } |
102 | |
103 | $compiled_name = self::CompiledName( $less_files, $hash ); |
104 | $output_file = self::OutputFile( $compiled_name, $parser_options ); |
105 | |
106 | // save the file list |
107 | $list = $less_files; |
108 | $list[] = $compiled_name; |
109 | $cache = implode( "\n", $list ); |
110 | file_put_contents( $list_file, $cache ); |
111 | |
112 | // save the css |
113 | file_put_contents( $output_file, $compiled ); |
114 | |
115 | // clean up |
116 | self::CleanCache(); |
117 | |
118 | return basename( $output_file ); |
119 | } |
120 | |
121 | /** |
122 | * Force the compiler to regenerate the cached css file |
123 | * |
124 | * @param array $less_files Array of .less files to compile |
125 | * @param array $parser_options Array of compiler options |
126 | * @param array $modify_vars Array of variables |
127 | * @return string Name of the css file |
128 | */ |
129 | public static function Regen( $less_files, $parser_options = [], $modify_vars = [] ) { |
130 | $parser_options['use_cache'] = false; |
131 | return self::Get( $less_files, $parser_options, $modify_vars ); |
132 | } |
133 | |
134 | public static function Cache( &$less_files, $parser_options = [] ) { |
135 | $parser_options['cache_dir'] = self::$cache_dir; |
136 | $parser = new Less_Parser( $parser_options ); |
137 | |
138 | // combine files |
139 | foreach ( $less_files as $file_path => $uri_or_less ) { |
140 | |
141 | // treat as less markup if there are newline characters |
142 | if ( strpos( $uri_or_less, "\n" ) !== false ) { |
143 | $parser->Parse( $uri_or_less ); |
144 | continue; |
145 | } |
146 | |
147 | $parser->ParseFile( $file_path, $uri_or_less ); |
148 | } |
149 | |