1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle CMS |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2014-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs\Page\Includes; |
9
|
|
|
use |
10
|
|
|
cs\Config, |
11
|
|
|
cs\Language, |
12
|
|
|
cs\Page\Includes_processing; |
13
|
|
|
|
14
|
|
|
trait Cache { |
15
|
|
|
/** |
16
|
|
|
* Creates cached version of given HTML, JS and CSS files. |
17
|
|
|
* Resulting files names consist of `$filename_prefix` and extension. |
18
|
|
|
* |
19
|
|
|
* @param string $target_file_path |
20
|
|
|
* @param string[][] $includes Array of paths to files, may have keys: `css` and/or `js` and/or `html` |
21
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
22
|
|
|
* |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
|
|
protected function cache_compressed_includes_files ($target_file_path, $includes, $vulcanization) { |
26
|
|
|
$local_includes = []; |
27
|
|
|
foreach ($includes as $extension => $files) { |
28
|
|
|
$content = $this->cache_compressed_includes_files_single($extension, $target_file_path, $files, $vulcanization); |
29
|
|
|
$file_path = "$target_file_path.$extension"; |
30
|
|
|
file_put_contents($file_path, gzencode($content, 9), LOCK_EX | FILE_BINARY); |
31
|
|
|
$local_includes[$extension] = 'storage/pcache/'.basename($file_path).'?'.substr(md5($content), 0, 5); |
32
|
|
|
} |
33
|
|
|
return $local_includes; |
34
|
|
|
} |
35
|
|
|
/** |
36
|
|
|
* @param string $extension |
37
|
|
|
* @param string $target_file_path |
38
|
|
|
* @param string[] $files |
39
|
|
|
* @param bool $vulcanization Whether to put combined files separately or to make includes built-in (vulcanization) |
40
|
|
|
* |
41
|
|
|
* @return mixed |
42
|
|
|
*/ |
43
|
|
|
protected function cache_compressed_includes_files_single ($extension, $target_file_path, $files, $vulcanization) { |
44
|
|
|
$content = ''; |
45
|
|
|
switch ($extension) { |
46
|
|
|
/** |
47
|
|
|
* Insert external elements into resulting css file. |
48
|
|
|
* It is needed, because those files will not be copied into new destination of resulting css file. |
49
|
|
|
*/ |
50
|
|
|
case 'css': |
51
|
|
|
/** |
52
|
|
|
* @param string $content |
53
|
|
|
* @param string $file |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
|
|
$callback = function ($content, $file) { |
58
|
|
|
return $content.Includes_processing::css(file_get_contents($file), $file); |
59
|
|
|
}; |
60
|
|
|
break; |
61
|
|
|
/** |
62
|
|
|
* Combine css and js files for Web Component into resulting files in order to optimize loading process |
63
|
|
|
*/ |
64
|
|
|
case 'html': |
65
|
|
|
/** |
66
|
|
|
* For CSP-compatible HTML files we need to know destination to put there additional JS/CSS files |
67
|
|
|
* |
68
|
|
|
* @param string $content |
69
|
|
|
* @param string $file |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
$callback = function ($content, $file) use ($target_file_path, $vulcanization) { |
74
|
|
|
$base_target_file_path = "$target_file_path-".basename($file).'+'.substr(md5($file), 0, 5); |
75
|
|
|
return $content.Includes_processing::html(file_get_contents($file), $file, $base_target_file_path, $vulcanization); |
76
|
|
|
}; |
77
|
|
|
break; |
78
|
|
|
case 'js': |
79
|
|
|
/** |
80
|
|
|
* @param string $content |
81
|
|
|
* @param string $file |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
$callback = function ($content, $file) { |
86
|
|
|
return $content.Includes_processing::js(file_get_contents($file)); |
87
|
|
|
}; |
88
|
|
|
if (substr($target_file_path, -7) == ':System') { |
89
|
|
|
$content = 'window.cs={Language:'._json_encode(Language::instance()).'};'; |
90
|
|
|
$content .= 'window.requirejs={paths:'._json_encode($this->get_requirejs_paths()).'};'; |
|
|
|
|
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
/** @noinspection PhpUndefinedVariableInspection */ |
94
|
|
|
return array_reduce($files, $callback, $content); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.