Skip to content

Commit

Permalink
MDL-61394 Administration: Add experimental feature to use SassC
Browse files Browse the repository at this point in the history
  • Loading branch information
cameorn1730 committed Feb 19, 2018
1 parent d1b4ca9 commit 4658220
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions admin/settings/development.php
Expand Up @@ -14,6 +14,8 @@

$temp->add(new admin_setting_configcheckbox('dndallowtextandlinks', new lang_string('dndallowtextandlinks', 'admin'), new lang_string('configdndallowtextandlinks', 'admin'), 0));

$temp->add(new admin_setting_configexecutable('pathtosassc', new lang_string('pathtosassc', 'admin'), new lang_string('pathtosassc_help', 'admin'), null));

$ADMIN->add('experimental', $temp);

// "debugging" settingpage
Expand Down
2 changes: 2 additions & 0 deletions lang/en/admin.php
Expand Up @@ -837,6 +837,8 @@
$string['pathtopsqlinvalid'] = 'Invalid path to psql - either wrong path or not executable';
$string['pathtopython'] = 'Path to Python';
$string['pathtopythondesc'] = 'Path to your executable Python binary (both Python 2 and Python 3 are acceptable).';
$string['pathtosassc'] = 'Path to SassC';
$string['pathtosassc_help'] = 'Specifying the location of the SassC binary will switch the SASS compiler from Moodle\'s PHP implementation to SassC. See https://github.com/sass/sassc for more information.';
$string['pcreunicodewarning'] = 'It is strongly recommended to use PCRE PHP extension that is compatible with Unicode characters.';
$string['perfdebug'] = 'Performance info';
$string['performance'] = 'Performance';
Expand Down
48 changes: 48 additions & 0 deletions lib/classes/scss.php
Expand Up @@ -98,6 +98,54 @@ public function to_css() {
return $this->compile($content);
}

/**
* Compile scss.
*
* Overrides ScssPHP's implementation, using the SassC compiler if it is available.
*
* @param string $code SCSS to compile.
* @param string $path Path to SCSS to compile.
*
* @return string The compiled CSS.
*/
public function compile($code, $path = null) {
global $CFG;

$pathtosassc = trim($CFG->pathtosassc);

if (!empty($pathtosassc) && is_executable($pathtosassc) && !is_dir($pathtosassc)) {
$process = proc_open(
$pathtosassc . ' -I' . implode(':', $this->importPaths) . ' -s',
[
['pipe', 'r'], // Set the process stdin pipe to read mode.
['pipe', 'w'], // Set the process stdout pipe to write mode.
['pipe', 'w'] // Set the process stderr pipe to write mode.
],
$pipes // Pipes become available in $pipes (pass by reference).
);
if (is_resource($process)) {
fwrite($pipes[0], $code); // Write the raw scss to the sassc process stdin.
fclose($pipes[0]);

$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);

fclose($pipes[1]);
fclose($pipes[2]);

// The proc_close function returns the process exit status. Anything other than 0 is bad.
if (proc_close($process) !== 0) {
throw new coding_exception($stderr);
}

// Compiled CSS code will be available from stdout.
return $stdout;
}
}

return parent::compile($code, $path);
}

/**
* Compile child; returns a value to halt execution
*
Expand Down

0 comments on commit 4658220

Please sign in to comment.