Elgg  Version 6.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 
24  public function __construct(protected Config $config, protected EventsService $events) {
25  }
26 
35  public function compile($css, array $options = []): string {
36  $defaults = [
37  'minify' => false, // minify handled by \Elgg\Views\MinifyHandler::class
38  'formatter' => 'single-line', // shows lowest byte size
39  'versioning' => false, // versioning done by Elgg
40  'rewrite_import_urls' => false,
41  'boilerplate' => false,
42  ];
43 
44  $config = (array) $this->config->css_compiler_options;
45 
46  $options = array_merge($defaults, $config, $options);
47  $options['vars'] = $this->getCssVars($options);
48 
49  Crush::$process = new CssCrushProcess($options, ['type' => 'filter', 'data' => $css]);
50  return Crush::$process->compile()->__toString();
51  }
52 
61  public function getCssVars(array $compiler_options = [], bool $load_config_vars = true): array {
62  $default_vars = array_merge($this->getCoreVars(), $this->getPluginVars());
63  $custom_vars = (array) elgg_extract('vars', $compiler_options, []);
64  $vars = array_merge($default_vars, $custom_vars);
65 
66  $results = (array) $this->events->triggerResults('vars:compiler', 'css', $compiler_options, $vars);
67 
68  if (!$load_config_vars) {
69  return $results;
70  }
71 
72  return array_merge($results, (array) elgg_get_config('custom_theme_vars', []));
73  }
74 
80  protected function getCoreVars(): array {
81  $file = Paths::elgg() . 'engine/theme.php';
82  return Includer::includeFile($file);
83  }
84 
90  protected function getPluginVars(): array {
91  $return = [];
92 
93  $plugins = elgg_get_plugins('active');
94  foreach ($plugins as $plugin) {
95  $plugin_vars = $plugin->getStaticConfig('theme', []);
96  if (empty($plugin_vars)) {
97  continue;
98  }
99 
100  $return = array_merge($return, $plugin_vars);
101  }
102 
103  return $return;
104  }
105 }
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:39
$plugin
static elgg()
Get the Elgg codebase path with "/".
Definition: Paths.php:44
getCoreVars()
Default Elgg theme variables.
Definition: CssCompiler.php:80
elgg_get_config(string $name, $default=null)
Get an Elgg configuration value.
$defaults
Generic entity header upload helper.
Definition: header.php:6
Events service.
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:256
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
if($who_can_change_language=== 'nobody') elseif($who_can_change_language=== 'admin_only'&&!elgg_is_admin_logged_in()) $options
Definition: language.php:20
getPluginVars()
Plugin theme variables.
Definition: CssCompiler.php:90
compile($css, array $options=[])
Compile CSS.
Definition: CssCompiler.php:35
$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:61
__construct(protected Config $config, protected EventsService $events)
Constructor.
Definition: CssCompiler.php:24