Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 121 additions & 27 deletions Classes/Hooks/RenderPreProcessorHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use TYPO3\CMS\Core\Utility\DebugUtility;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
Expand All @@ -44,12 +45,40 @@ class RenderPreProcessorHook
private static $visitedFiles = [];

private $variables = [];
private $importPaths = [];
private $setup = false;
private $parser = false;

/**
* @var \TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer
*/
private $contentObjectRenderer;

/**
* watch import path files for changed files
* @param integer $lastBuildTime
* @return array
*/
public function watch($lastBuildTime){
$has_changes = false;
if( filter_var($this->setup['watchImportPath'],FILTER_VALIDATE_BOOLEAN) ){
$filelist = [];
foreach ( $this->importPaths as $addPath ) {
$filelist=array_merge($filelist, glob( $addPath . "/*.scss" ));
$filelist=array_merge($filelist, glob( $addPath . "/**/*.scss" ));
}
foreach ($filelist as $i => &$watchfile){
if (! realpath($watchfile) or filemtime($watchfile) > $lastBuildTime) {
$has_changes = true;
break;
}
// For Debugging
//$watchfile = $watchfile.' - '.filemtime($watchfile);
}
}
//debug($filelist);exit;
return $has_changes;
}
/**
* Main hook function
*
Expand All @@ -70,18 +99,24 @@ public function renderPreProcessorProc(&$params, PageRenderer $pagerenderer)
if (VersionNumberUtility::convertVersionNumberToInteger(VersionNumberUtility::getCurrentTypo3Version()) < VersionNumberUtility::convertVersionNumberToInteger('8.0.0')) {
$defaultOutputDir = 'typo3temp/';
}
if ($this->contentObjectRenderer === null) {
$this->contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}

$this->setup = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_wsscss.'];
if (\is_array($this->setup['importPaths.'])) {
foreach ($this->setup['importPaths.'] as $index => $importPath) {
$this->importPaths[] = $this->getStreamlinedPath( $importPath );
}
}

$setup = $GLOBALS['TSFE']->tmpl->setup;
if (\is_array($setup['plugin.']['tx_wsscss.']['variables.'])) {
if (\is_array($this->setup['variables.'])) {

$variables = $setup['plugin.']['tx_wsscss.']['variables.'];
$variables = $this->setup['variables.'];

$parsedTypoScriptVariables = [];
foreach ($variables as $variable => $key) {
if (array_key_exists($variable . '.', $variables)) {
if ($this->contentObjectRenderer === null) {
$this->contentObjectRenderer = GeneralUtility::makeInstance(ContentObjectRenderer::class);
}
$content = $this->contentObjectRenderer->cObjGetSingle($variables[$variable], $variables[$variable . '.']);
$parsedTypoScriptVariables[$variable] = $content;

Expand Down Expand Up @@ -179,7 +214,7 @@ public function renderPreProcessorProc(&$params, PageRenderer $pagerenderer)
$css = '';

try {
if ($contentHashCache === '' || $contentHashCache !== $contentHash) {
if ($contentHashCache === '' || $contentHashCache !== $contentHash || $this->watch(filemtime($cssFilename)) ) {
$css = $this->compileScss($scssFilename, $cssFilename, $this->variables, $showLineNumber, $formatter, $cssRelativeFilename, $useSourceMap);

$cache->set($cacheKey, $contentHash, ['scss'], 0);
Expand Down Expand Up @@ -231,33 +266,60 @@ protected function compileScss($scssFilename, $cssFilename, $vars = [], $showLin
$extPath = ExtensionManagementUtility::extPath('ws_scss');
require_once $extPath . 'Resources/Private/scssphp/scss.inc.php';

$parser = new \Leafo\ScssPhp\Compiler();
if (file_exists($scssFilename)) {
$scssCacheOptions = [
'cache_dir' => $this->getStreamlinedPath('typo3temp/ws_scss/'),
'prefix' => 'scssphp_',
'forceRefresh' => false
];

$parser->setVariables($vars);
try {
$this->parser = new \ScssPhp\ScssPhp\Compiler($scssCacheOptions);
}catch(Exception $e){
DebugUtility::printArray($e->getMessage());
}

if ($showLineNumber) {
$parser->setLineNumberStyle(\Leafo\ScssPhp\Compiler::LINE_COMMENTS);
}
if ($formatter !== null) {
$parser->setFormatter($formatter);
}

if ($useSourceMap) {
$parser->setSourceMap(\Leafo\ScssPhp\Compiler::SOURCE_MAP_INLINE);

$parser->setSourceMapOptions([
'sourceMapWriteTo' => $cssFilename . '.map',
'sourceMapURL' => $cssRelativeFilename . '.map',
'sourceMapBasepath' => PATH_site,
'sourceMapRootpath' => '/',
]);
}
if (file_exists($scssFilename)) {
// set import paths
try {
if( $this->setup['importPathMode'] === 'replace'){
$this->parser->setImportPaths( $this->importPaths );
} else {
foreach ( $this->importPaths as $addPath ) {
$this->parser->addImportPath($addPath);
}
}
$this->parser->setVariables($vars);
if ($showLineNumber) {
$this->parser->setLineNumberStyle(\ScssPhp\ScssPhp\Compiler::LINE_COMMENTS);
}
if ($formatter !== null) {
$this->parser->setFormatter($formatter);
}

$css = $parser->compile('@import "' . $scssFilename . '";');
if ($useSourceMap) {
$this->parser->setSourceMap(\ScssPhp\ScssPhp\Compiler::SOURCE_MAP_INLINE);

$this->parser->setSourceMapOptions([
'sourceMapWriteTo' => $cssFilename . '.map',
'sourceMapURL' => $cssRelativeFilename . '.map',
'sourceMapBasepath' => PATH_site,
'sourceMapRootpath' => '/',
]);
}
$css = $this->parser->compile(file_get_contents( $scssFilename ));
}catch(\ScssPhp\ScssPhp\Exception\CompilerException $e){
DebugUtility::printArray($e->getMessage());
}

GeneralUtility::writeFile($cssFilename, $css);

if( filter_var($this->setup['debug'],FILTER_VALIDATE_BOOLEAN) ){
debug( $this->parser );
debug( $this->parser->getParsedFiles() );
debug( $this->setup );
}

return $css;
}

Expand Down Expand Up @@ -311,5 +373,37 @@ protected function calculateContentHash($scssFilename, $vars = ''): string
return $hash;
}

/**
*
* @param string $path
* @param boolean $relPath
* @return string parsed path
*/
protected function getStreamlinedPath($path, $relPath=false)
{
if ( strpos( $path, 'EXT:' ) === 0) {
$pathParts = explode( '/', substr( $path, 4 ) );
$extKey = array_shift( $pathParts );

if ( (string) $extKey !== '' && ExtensionManagementUtility::isLoaded( $extKey ) ) {
array_unshift( $pathParts, rtrim( ExtensionManagementUtility::extPath( $extKey ), '/') );
}
$path = implode( '/', $pathParts );
} elseif ( strpos( $path, 'DIR:' ) === 0 ) {
$pathParts = explode( '/', substr( $path, 4 ) );
array_unshift( $pathParts, rtrim(PATH_site, '/') );
$path = implode( '/', $pathParts );
} elseif ( strpos( $path, '..' ) === 0) {
$path = realpath( $path );
$path = str_replace(DIRECTORY_SEPARATOR,'/', $path);
} else {
$path = GeneralUtility::getFileAbsFileName( $path );
}

if($relPath){
$path = PathUtility::stripPathSitePrefix($path);
}
return $path;
}

}
2 changes: 1 addition & 1 deletion Resources/Private/scssphp/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015 Leaf Corcoran, http://leafo.github.io/scssphp
Copyright (c) 2015 Leaf Corcoran, http://scssphp.github.io/scssphp

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
8 changes: 4 additions & 4 deletions Resources/Private/scssphp/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# scssphp
### <http://leafo.github.io/scssphp>
### <http://scssphp.github.io/scssphp>

[![Build](https://travis-ci.org/leafo/scssphp.svg?branch=master)](http://travis-ci.org/leafo/scssphp)
[![License](https://poser.pugx.org/leafo/scssphp/license.svg)](https://packagist.org/packages/leafo/scssphp)
[![Build](https://travis-ci.org/scssphp/scssphp.svg?branch=master)](http://travis-ci.org/scssphp/scssphp)
[![License](https://poser.pugx.org/scssphp/scssphp/license)](https://packagist.org/packages/scssphp/scssphp)

`scssphp` is a compiler for SCSS written in PHP.

Checkout the homepage, <http://leafo.github.io/scssphp>, for directions on how to use.
Checkout the homepage, <http://scssphp.github.io/scssphp>, for directions on how to use.

## Running Tests

Expand Down
3 changes: 2 additions & 1 deletion Resources/Private/scssphp/scss.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
throw new \Exception('scssphp requires PHP 5.6 or above');
}

if (! class_exists('Leafo\ScssPhp\Version', false)) {
if (! class_exists('ScssPhp\ScssPhp\Version', false)) {
include_once __DIR__ . '/src/Base/Range.php';
include_once __DIR__ . '/src/Block.php';
include_once __DIR__ . '/src/Cache.php';
include_once __DIR__ . '/src/Colors.php';
include_once __DIR__ . '/src/Compiler.php';
include_once __DIR__ . '/src/Compiler/Environment.php';
Expand Down
14 changes: 14 additions & 0 deletions Resources/Private/scssphp/shim.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace

{
foreach ([
'PHPUnit_Framework_TestCase' => 'PHPUnit\Framework\TestCase',
] as $class => $namespacedClass) {
if (! class_exists($class) && class_exists($namespacedClass)) {
class_alias($namespacedClass, $class);
}
}

include_once __DIR__ . '/vendor/composer/autoload_real.php';
}
6 changes: 3 additions & 3 deletions Resources/Private/scssphp/src/Base/Range.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* SCSSPHP
*
* @copyright 2015-2018 Leaf Corcoran
* @copyright 2015-2019 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://leafo.github.io/scssphp
* @link http://scssphp.github.io/scssphp
*/

namespace Leafo\ScssPhp\Base;
namespace ScssPhp\ScssPhp\Base;

/**
* Range
Expand Down
10 changes: 5 additions & 5 deletions Resources/Private/scssphp/src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
/**
* SCSSPHP
*
* @copyright 2012-2018 Leaf Corcoran
* @copyright 2012-2019 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://leafo.github.io/scssphp
* @link http://scssphp.github.io/scssphp
*/

namespace Leafo\ScssPhp;
namespace ScssPhp\ScssPhp;

/**
* Block
Expand All @@ -24,7 +24,7 @@ class Block
public $type;

/**
* @var \Leafo\ScssPhp\Block
* @var \ScssPhp\ScssPhp\Block
*/
public $parent;

Expand Down Expand Up @@ -64,7 +64,7 @@ class Block
public $children;

/**
* @var \Leafo\ScssPhp\Block
* @var \ScssPhp\ScssPhp\Block
*/
public $selfParent;
}
Loading