1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @subpackage System module |
5
|
|
|
* @category modules |
6
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2015-2016, Nazar Mokrynskyi |
8
|
|
|
* @license MIT License, see license.txt |
9
|
|
|
*/ |
10
|
|
|
namespace cs\modules\System\api\Controller\admin; |
11
|
|
|
use |
12
|
|
|
cs\Cache, |
13
|
|
|
cs\Config, |
14
|
|
|
cs\Event, |
15
|
|
|
cs\ExitException; |
16
|
|
|
|
17
|
|
|
trait optimization { |
18
|
|
|
protected static $optimization_options_keys = [ |
19
|
|
|
'cache_compress_js_css', |
20
|
|
|
'frontend_load_optimization', |
21
|
|
|
'vulcanization', |
22
|
|
|
'put_js_after_body', |
23
|
|
|
'disable_webcomponents', |
24
|
|
|
'inserts_limit', |
25
|
|
|
'update_ratio' |
26
|
|
|
]; |
27
|
|
|
/** |
28
|
|
|
* Get optimization settings |
29
|
|
|
*/ |
30
|
|
|
public static function admin_optimization_get_settings () { |
|
|
|
|
31
|
|
|
$Config = Config::instance(); |
32
|
|
|
return $Config->core(static::$optimization_options_keys) + [ |
33
|
|
|
'cache_state' => Cache::instance()->cache_state(), |
34
|
|
|
'applied' => $Config->cancel_available() |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
/** |
38
|
|
|
* Clean cache |
39
|
|
|
* |
40
|
|
|
* @param \cs\Request $Request |
41
|
|
|
* |
42
|
|
|
* @throws ExitException |
43
|
|
|
*/ |
44
|
|
|
public static function admin_optimization_clean_cache ($Request) { |
45
|
|
|
$Cache = Cache::instance(); |
46
|
|
|
time_limit_pause(); |
47
|
|
|
$path_prefix = $Request->data('path_prefix'); |
48
|
|
|
if ($path_prefix) { |
49
|
|
|
$result = $Cache->del($path_prefix); |
50
|
|
|
} else { |
51
|
|
|
$result = $Cache->clean(); |
52
|
|
|
clean_classes_cache(); |
53
|
|
|
} |
54
|
|
|
time_limit_pause(false); |
55
|
|
|
if (!$result) { |
56
|
|
|
throw new ExitException(500); |
57
|
|
|
} |
58
|
|
|
$Cache->disable(); |
59
|
|
|
} |
60
|
|
|
/** |
61
|
|
|
* Clean public cache (CSS/JS/HTML) |
62
|
|
|
* |
63
|
|
|
* @throws ExitException |
64
|
|
|
*/ |
65
|
|
|
public static function admin_optimization_clean_pcache () { |
66
|
|
|
if (!clean_pcache()) { |
67
|
|
|
throw new ExitException(500); |
68
|
|
|
} |
69
|
|
|
Event::instance()->fire('admin/System/general/optimization/clean_pcache'); |
70
|
|
|
} |
71
|
|
|
/** |
72
|
|
|
* Apply optimization settings |
73
|
|
|
* |
74
|
|
|
* @param \cs\Request $Request |
75
|
|
|
* |
76
|
|
|
* @throws ExitException |
77
|
|
|
*/ |
78
|
|
|
public static function admin_optimization_apply_settings ($Request) { |
79
|
|
|
static::admin_core_options_apply($Request, static::$optimization_options_keys); |
80
|
|
|
static::admin_optimization_clean_pcache(); |
81
|
|
|
} |
82
|
|
|
/** |
83
|
|
|
* Save optimization settings |
84
|
|
|
* |
85
|
|
|
* @param \cs\Request $Request |
86
|
|
|
* |
87
|
|
|
* @throws ExitException |
88
|
|
|
*/ |
89
|
|
|
public static function admin_optimization_save_settings ($Request) { |
90
|
|
|
static::admin_core_options_save($Request, static::$optimization_options_keys); |
91
|
|
|
static::admin_optimization_clean_pcache(); |
92
|
|
|
} |
93
|
|
|
/** |
94
|
|
|
* Cancel optimization settings |
95
|
|
|
* |
96
|
|
|
* @throws ExitException |
97
|
|
|
*/ |
98
|
|
|
public static function admin_optimization_cancel_settings () { |
99
|
|
|
static::admin_core_options_cancel(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.