1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Elgg\Assets; |
4
|
|
|
|
5
|
|
|
use Elgg\Config; |
6
|
|
|
use Elgg\Includer; |
7
|
|
|
use Elgg\PluginHooksService; |
8
|
|
|
use Elgg\Project\Paths; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Compile CSS with CSSCrush |
12
|
|
|
* |
13
|
|
|
* @internal |
14
|
|
|
*/ |
15
|
|
|
class CssCompiler { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Config |
19
|
|
|
*/ |
20
|
|
|
protected $config; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var PluginHooksService |
24
|
|
|
*/ |
25
|
|
|
protected $hooks; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Constructor |
29
|
|
|
* |
30
|
|
|
* @param Config $config Config |
31
|
|
|
* @param PluginHooksService $hooks Hooks service |
32
|
|
|
*/ |
33
|
6 |
|
public function __construct(Config $config, PluginHooksService $hooks) { |
34
|
6 |
|
$this->config = $config; |
35
|
6 |
|
$this->hooks = $hooks; |
36
|
6 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Compile CSS |
40
|
|
|
* |
41
|
|
|
* @param string $css CSS string |
42
|
|
|
* @param array $options CssCrush options |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
5 |
|
public function compile($css, array $options = []) { |
47
|
|
|
$defaults = [ |
48
|
5 |
|
'minify' => false, // minify handled by _elgg_views_minify |
49
|
|
|
'formatter' => 'single-line', // shows lowest byte size |
50
|
|
|
'versioning' => false, // versioning done by Elgg |
51
|
|
|
'rewrite_import_urls' => false, |
52
|
|
|
]; |
53
|
|
|
|
54
|
5 |
|
$config = (array) $this->config->css_compiler_options; |
55
|
|
|
|
56
|
5 |
|
$options = array_merge($defaults, $config, $options); |
57
|
|
|
|
58
|
5 |
|
$default_vars = $this->getDefaultVars(); |
59
|
5 |
|
$custom_vars = (array) elgg_extract('vars', $options, []); |
60
|
5 |
|
$vars = array_merge($default_vars, $custom_vars); |
61
|
|
|
|
62
|
5 |
|
$options['vars'] = $this->hooks->trigger('vars:compiler', 'css', $options, $vars); |
63
|
|
|
|
64
|
5 |
|
return csscrush_string($css, $options); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Default Elgg theme variables |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
5 |
|
protected function getDefaultVars() { |
72
|
5 |
|
$file = Paths::elgg() . 'engine/theme.php'; |
73
|
5 |
|
return Includer::includeFile($file); |
74
|
|
|
} |
75
|
|
|
} |