1 | <?php |
||
13 | class Cache { |
||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | protected $public_cache; |
||
18 | /** |
||
19 | * @param string $public_cache |
||
20 | */ |
||
21 | 4 | public function __construct ($public_cache = PUBLIC_CACHE) { |
|
22 | 4 | $this->public_cache = $public_cache; |
|
23 | 4 | } |
|
24 | /** |
||
25 | * @param \cs\Config $Config |
||
26 | * @param \cs\Language $L |
||
27 | * @param string $theme |
||
28 | */ |
||
29 | 4 | public function rebuild ($Config, $L, $theme) { |
|
30 | 4 | if (!file_exists("$this->public_cache/$theme.json")) { |
|
31 | 4 | $this->rebuild_normal($Config, $theme); |
|
32 | 4 | Event::instance()->fire('System/Page/rebuild_cache'); |
|
33 | 4 | $this->rebuild_optimized($theme); |
|
34 | 4 | $this->rebuild_webcomponents_polyfill(); |
|
35 | } |
||
36 | /** |
||
37 | * We take hash of languages in order to take into account when list of active languages has changed (and generate cache for all acive languages) |
||
38 | */ |
||
39 | 4 | $languages_hash = md5(implode('', $Config->core['active_languages'])); |
|
40 | 4 | if (!file_exists("$this->public_cache/languages-$languages_hash.json")) { |
|
41 | 4 | $this->rebuild_languages($Config, $L, $languages_hash); |
|
42 | } |
||
43 | 4 | } |
|
44 | /** |
||
45 | * @param \cs\Config $Config |
||
46 | * @param string $theme |
||
47 | */ |
||
48 | 4 | protected function rebuild_normal ($Config, $theme) { |
|
49 | 4 | list($dependencies, $assets_map) = Collecting::get_assets_dependencies_and_map($Config, $theme); |
|
50 | 4 | $compressed_assets_map = []; |
|
51 | 4 | $not_embedded_resources_map = []; |
|
52 | /** @noinspection ForeachSourceInspection */ |
||
53 | 4 | foreach ($assets_map as $filename_prefix => $local_assets) { |
|
54 | 4 | $compressed_assets_map[$filename_prefix] = $this->cache_compressed_assets_files( |
|
55 | 4 | $filename_prefix, |
|
56 | 4 | $local_assets, |
|
57 | 4 | $Config->core['vulcanization'], |
|
58 | 4 | $not_embedded_resources_map |
|
59 | ); |
||
60 | } |
||
61 | 4 | unset($assets_map, $filename_prefix, $local_assets); |
|
62 | 4 | file_put_json("$this->public_cache/$theme.json", [$dependencies, $compressed_assets_map, array_filter($not_embedded_resources_map)]); |
|
63 | 4 | } |
|
64 | /** |
||
65 | * @param string $theme |
||
66 | */ |
||
67 | 4 | protected function rebuild_optimized ($theme) { |
|
81 | 4 | protected function rebuild_webcomponents_polyfill () { |
|
82 | 4 | $webcomponents_js = file_get_contents(DIR.'/assets/js/WebComponents-polyfill/webcomponents-hi-sd-ce.min.js'); |
|
83 | 4 | $hash = md5($webcomponents_js); |
|
87 | /** |
||
88 | * @param \cs\Config $Config |
||
89 | * @param \cs\Language $L |
||
90 | * @param string $languages_hash |
||
91 | */ |
||
92 | 4 | protected function rebuild_languages ($Config, $L, $languages_hash) { |
|
106 | /** |
||
107 | * Creates cached version of given HTML, JS and CSS files. |
||
108 | * Resulting files names consist of `$filename_prefix` and extension. |
||
109 | * |
||
110 | * @param string $filename_prefix |
||
111 | * @param string[][] $assets Array of paths to files, may have keys: `css` and/or `js` and/or `html` |
||
112 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
113 | * @param string[][] $not_embedded_resources_map Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because |
||
114 | * of CSP |
||
115 | * |
||
116 | * @return array |
||
117 | */ |
||
118 | 4 | protected function cache_compressed_assets_files ($filename_prefix, $assets, $vulcanization, &$not_embedded_resources_map) { |
|
142 | /** |
||
143 | * @param string $extension |
||
144 | * @param string $filename_prefix |
||
145 | * @param string[] $files |
||
146 | * @param bool $vulcanization Whether to put combined files separately or to make included assets built-in (vulcanization) |
||
147 | * @param string[] $not_embedded_resources Resources like images/fonts might not be embedded into resulting CSS because of big size or CSS/JS because of CSP |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | 4 | protected function cache_compressed_assets_files_single ($extension, $filename_prefix, $files, $vulcanization, &$not_embedded_resources) { |
|
214 | } |
||
215 |