Sass and Less plugins for Kirby v2

@bartvandebiezen:
Thanks for your plugin for Sass, awesome work!

I have added some extensions to the snippet file scss.php:

<?php

/**
 * SCSS Snippet
 * @author    Bart van de Biezen <bart@bartvandebiezen.com>
 * @link      https://github.com/bartvandebiezen/kirby-v2-scssphp
 * @return    CSS and HTML
 * @version   1.0.1
 * @extension 2016-06-03 by HeinerEF ("DS" by reason of OS Windows, $IsDeveloper, NOT minified CSS file only for Developers)
 */

use Leafo\ScssPhp\Compiler;

// Using realpath seems to work best in different situations.
$root = realpath(__DIR__ . DS . '..' . DS . '..');

// Set file paths. Used for checking and updating CSS file for current template.
$template        = $page->template();
$SCSS            = $root . DS . 'assets' . DS . 'scss' . DS . $template . '.scss';
$minCSS          = $root . DS . 'assets' . DS . 'css'  . DS . $template . '.min.css';
$orgCSS          = $root . DS . 'assets' . DS . 'css'  . DS . $template . '.css';
$minCSSKirbyPath = 'assets/css/' . $template . '.min.css';
$orgCSSKirbyPath = 'assets/css/' . $template . '.css';

// Set default SCSS if there is no SCSS for current template. If template is default, skip check.
if ($template == 'default' or !file_exists($SCSS)) {
  $SCSS            = $root . DS . 'assets' . DS . 'scss' . DS . 'default.scss';
  $minCSS          = $root . DS . 'assets' . DS . 'css'  . DS . 'default.min.css';
  $orgCSS          = $root . DS . 'assets' . DS . 'css'  . DS . 'default.css';
  $minCSSKirbyPath = 'assets/css/default.min.css';
  $orgCSSKirbyPath = 'assets/css/default.css';
}

$IsDeveloper = FALSE; // default => CSS is minified.
$username = '<not logged in user>';

if($user = site()->user()) {
  $IsDeveloper = (in_array($user->role()->id(), c::get('scssDeveloperRoles', array('admin')))); // these roles use the NOT minified CSS.
  $username = $user->username();
};
# echo '<!-- $user = "' . $user . '", $IsDeveloper = "' . r(($IsDeveloper == TRUE), 'TRUE', 'FALSE') . '"' . " -->\n  "; // activate this line only for debuging of 'site/config/config.php'.

// For when the plugin should check if partials are changed. If any partial is newer than the main SCSS file, the main SCSS file will be 'touched'. This will trigger the compiler later on, on this server and also an another environment when synced.
if (c::get('scssNestedCheck')) {
  $SCSSDirectory = $root . DS . 'assets' . DS . 'scss' . DS;
  $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($SCSSDirectory));
  foreach ($files as $file) {
    if (pathinfo($file, PATHINFO_EXTENSION) == "scss" && filemtime($file) > filemtime($SCSS)) {
      touch ($SCSS);
      clearstatcache();
      break;
    }
  }
}

// Get file modification times. Used for checking if update is required and as version number for caching.
$SCSSFileTime = filemtime($SCSS);
$CSSFileTime  = filemtime($minCSS);

// Update CSS when needed.
if (!file_exists($minCSS) or $SCSSFileTime > $CSSFileTime ) {

  // time stamp
  $stamp = '/* Last update ' . date("Y-m-d H:i:s P") . ' by SCSSPHP Plugin and ' . $username . ' */' . "\n";

  // Activate library.
  require_once $root . DS . 'site' . DS . 'plugins' . DS . 'scssphp' . DS . 'scss.inc.php';
  $parser = new Compiler();

  // Setting compression provided by library.
# $parser->setFormatter('Leafo\ScssPhp\Formatter\Compressed'); // ORG, but compression not required here, if CSS is minified.
  $parser->setFormatter('Leafo\ScssPhp\Formatter\Expanded');   // for easier debuging of the CSS, if CSS is NOT minified.

  // Setting relative @import paths.
  $importPath = $root . DS . 'assets' . DS . 'scss';
  $parser->addImportPath($importPath);

  // Place SCSS file in buffer.
  $buffer = file_get_contents($SCSS);

  // Compile content in buffer.
  $buffer = $parser->compile($buffer);

  // Update NOT minified CSS file.
  file_put_contents($orgCSS, $stamp . $buffer);

  // Minify the CSS even further.
  require_once $root . DS . 'site' . DS . 'plugins' . DS . 'scssphp' . DS . 'minify.php';
  $buffer = minifyCSS($buffer);

  // Update minified CSS file.
  file_put_contents($minCSS, $buffer);
}

?>
<link rel="stylesheet" href="<?php echo url(r($IsDeveloper, $orgCSSKirbyPath, $minCSSKirbyPath)); ?>?version=<?php echo $CSSFileTime; ?>">

Good luck!