Elgg  Version 5.1
CssCompiler.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Assets;
4 
6 use Elgg\Config;
8 use Elgg\Includer;
10 
16 class CssCompiler {
17 
18  protected Config $config;
19 
21 
28  public function __construct(Config $config, EventsService $events) {
29  $this->config = $config;
30  $this->events = $events;
31  }
32 
41  public function compile($css, array $options = []): string {
42  $defaults = [
43  'minify' => false, // minify handled by \Elgg\Views\MinifyHandler::class
44  'formatter' => 'single-line', // shows lowest byte size
45  'versioning' => false, // versioning done by Elgg
46  'rewrite_import_urls' => false,
47  'boilerplate' => false,
48  ];
49 
50  $config = (array) $this->config->css_compiler_options;
51 
52  $options = array_merge($defaults, $config, $options);
53  $options['vars'] = $this->getCssVars($options);
54 
55  Crush::$process = new CssCrushProcess($options, ['type' => 'filter', 'data' => $css]);
56  return Crush::$process->compile()->__toString();
57  }
58 
67  public function getCssVars(array $compiler_options = [], bool $load_config_vars = true): array {
68  $default_vars = array_merge($this->getCoreVars(), $this->getPluginVars());
69  $custom_vars = (array) elgg_extract('vars', $compiler_options, []);
70  $vars = array_merge($default_vars, $custom_vars);
71 
72  $results = (array) $this->events->triggerResults('vars:compiler', 'css', $compiler_options, $vars);
73 
74  if (!$load_config_vars) {
75  return $results;
76  }
77 
78  return array_merge($results, (array) elgg_get_config('custom_theme_vars', []));
79  }
80 
86  protected function getCoreVars(): array {
87  $file = Paths::elgg() . 'engine/theme.php';
88  return Includer::includeFile($file);
89  }
90 
96  protected function getPluginVars(): array {
97  $return = [];
98 
99  $plugins = elgg_get_plugins('active');
100  foreach ($plugins as $plugin) {
101  $plugin_vars = $plugin->getStaticConfig('theme', []);
102  if (empty($plugin_vars)) {
103  continue;
104  }
105 
106  $return = array_merge($return, $plugin_vars);
107  }
108 
109  return $return;
110  }
111 }
static includeFile($file)
Include a file with as little context as possible.
Definition: Includer.php:18
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:55
$plugin
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
getCoreVars()
Default Elgg theme variables.
Definition: CssCompiler.php:86
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
$defaults
Generic entity header upload helper.
Definition: header.php:6
__construct(Config $config, EventsService $events)
Constructor.
Definition: CssCompiler.php:28
EventsService $events
Definition: CssCompiler.php:20
Events service.
$plugins
Definition: categories.php:3
$options
Elgg admin footer.
Definition: footer.php:6
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
getPluginVars()
Plugin theme variables.
Definition: CssCompiler.php:96
compile($css, array $options=[])
Compile CSS.
Definition: CssCompiler.php:41
$css
Definition: install.css.php:5
$results
Definition: content.php:22
Compile CSS with CSSCrush.
Definition: CssCompiler.php:16
Css Crush Processor.
$vars
Definition: theme.php:5
getCssVars(array $compiler_options=[], bool $load_config_vars=true)
Fetches a combined set of CSS variables and their value.
Definition: CssCompiler.php:67