Skip to content

Commit

Permalink
Sass native compiler support
Browse files Browse the repository at this point in the history
  • Loading branch information
scaccogatto committed Nov 8, 2017
1 parent 8f678e9 commit a619a92
Show file tree
Hide file tree
Showing 8 changed files with 527 additions and 180 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hce/microframework-core",
"version": "1.4.5",
"version": "1.5.0",
"description": "microframework core files",
"authors": [
{
Expand Down
501 changes: 366 additions & 135 deletions composer.lock

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
# it.hce.microframework.core
### a lightweight framework designed for prototyping

> a lightweight framework designed for prototyping
## Install

* `php composer.phar create-project hce/microframework projectName`

or

* `composer.phar create-project hce/microframework projectName`

then

* `cd projectName`
* `npm install`
* Point your webserver to `projectName/public` folder
* Set `public/css` and `public/js` as writable folders (create them if necessary)
* Set `cache` as writable folder

## Documentation
Please refer to [it.hce.microframework](https://github.com/HCESrl/it.hce.microframework) repository documentation.

Please refer to [it.hce.microframework](https://github.com/HCESrl/it.hce.microframework) repository documentation.
6 changes: 0 additions & 6 deletions src/MicroFramework.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,22 @@ public static function printProject($folder = 'static/', $exclude = ['css', 'js'
// clean the destination folder
self::cleanupDirectory($destinationFolder);


echo "\033[34m finished writing resources \033[0m\n";

// creates the static folders and files
self::createDirIfNotExists($destinationFolder . '/css/');
self::createDirIfNotExists($destinationFolder . '/js/');


// write the whole resource pack
ResourcesFactory::writeResources(true, $destinationFolder);
ResourcesFactory::writeResources(false, $destinationFolder);


$directory = new RecursiveDirectoryIterator(PathHelper::getPublicPath());
$iterator = new RecursiveIteratorIterator($directory);
$result = new RegexIterator($iterator, '/^.+\.json$/i', RecursiveRegexIterator::GET_MATCH);



echo "\033[34m starting iterator \033[0m\n";


foreach ($result as $key => $value) {
echo "\033[34m processing $key \033[0m\n";
// write each controller in a sub directory
Expand Down
112 changes: 112 additions & 0 deletions src/factories/FallbackSassFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace it\hce\microframework\core\factories;


use CSSJanus;
use it\hce\microframework\core\exceptions\MicroFrameworkException;
use it\hce\microframework\core\helpers\PathHelper;
use Leafo\ScssPhp\Compiler;
use MatthiasMullie\Minify\CSS;

class FallbackSassFactory
{
const mainScssPath = 'css/main.scss';
const mainRtlScssPath = 'css/main.rtl.scss';
const scssPath = 'css/';

private $compiler;
private $main;
private $mainRtl;
private $minifier;
private $compiledCss;

/**
* SassFactory constructor.
* @param bool $rtl
* @throws MicroFrameworkException
*/
public function __construct($rtl = false)
{
// load the compiler
$this->compiler = new Compiler();

//load the minifier
$this->minifier = new CSS();

// set rtl
$this->rtl = $rtl;

if ($this->rtl) {
// load main.rtl.scss
if (!file_exists(PathHelper::getResourcesPath(self::mainRtlScssPath))) {
throw new MicroFrameworkException('main.rtl.scss not found');
}

$this->mainRtl = file_get_contents(PathHelper::getResourcesPath(self::mainRtlScssPath));
}
// load main.scss
if (!file_exists(PathHelper::getResourcesPath(self::mainScssPath))) {
throw new MicroFrameworkException('main.scss not found');
}

$this->main = file_get_contents(PathHelper::getResourcesPath(self::mainScssPath));
}

/**
* Collects the needed files
*/
public function collect()
{
$this->compiler->setImportPaths(PathHelper::getResourcesPath(self::scssPath));
}

/**
* Compiles SCSS
* @return string the compiled css
*/
public function compile()
{
$compiledSass = $this->compiler->compile($this->main);

if ($this->rtl) {
$compiledSass = $this->rightToLeft($compiledSass);
$rtlSass = $this->compiler->compile($this->mainRtl);
$compiledSass = $compiledSass . $rtlSass;
}

// minify the result
return $this->compiledCss = $this->minify($compiledSass);
}

private function rightToLeft($compiledSass)
{
return CSSJanus::transform($compiledSass);
}

private function minify($compiledSass)
{
$this->minifier->add($compiledSass);
return $this->minifier->minify();
}

/**
* Writes the loaded SCSS in a given css file (overwrite)
* @param $file
* @throws MicroFrameworkException
*/
public function write($file)
{
if ((!file_exists($file) && is_writable(dirname($file))) || is_writable($file)) {
file_put_contents($file, $this->compiledCss);
} else {
if((!file_exists($file) && !is_writable(dirname($file))) ){
throw new MicroFrameworkException($file . ' does not exist and directory is not writable');
} else if(!is_writable($file)){
throw new MicroFrameworkException($file . ' is not writable');
} else {
throw new MicroFrameworkException("Unknown error trying to write $file ");
}
}
}
}
12 changes: 6 additions & 6 deletions src/factories/JavascriptFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public function getStaticJSLastEditDate() {
$files = $this->getStaticJSFileList();
$time = 0;

foreach($files as $key => $value){
foreach($files as $key => $value) {
$modTime = filemtime($value);
if($modTime > $time){
if ($modTime > $time) {
$time = $modTime;
}
}
Expand Down Expand Up @@ -119,12 +119,12 @@ public function write($file)
if ((!file_exists($file) && is_writable(dirname($file))) || is_writable($file)) {
file_put_contents($file, $this->output());
} else {
if((!file_exists($file) && !is_writable(dirname($file))) ){
if((!file_exists($file) && !is_writable(dirname($file)))) {
throw new MicroFrameworkException($file . ' does not exist and directory is not writable');
} else if(!is_writable($file)){
throw new MicroFrameworkException($file . ' is not writable');
} else if (!is_writable($file)) {
throw new MicroFrameworkException($file . ' is not writable');
} else {
throw new MicroFrameworkException("Unknown error trying to write $file ");
throw new MicroFrameworkException("Unknown error trying to write $file");
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions src/factories/ResourcesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function writeResources($rtl = false, $destinationFolder = false)
*/
public static function writeCSS($rtl = false, $destinationFolder = false)
{
if($destinationFolder){
if ($destinationFolder) {
$targetCssPath = $destinationFolder . ($rtl ? self::rtlFilePath : self::cssFilePath);
} else {
$targetCssPath = PathHelper::getPublicPath($rtl ? self::rtlFilePath : self::cssFilePath);
Expand All @@ -39,7 +39,7 @@ public static function writeCSS($rtl = false, $destinationFolder = false)

if (!PathHelper::isResourceLocked($targetCssPath)) {
// Write minified CSS to main.css
self::$sassFactory = new SassFactory($rtl);
self::$sassFactory = self::getWorkingSassFactory($rtl);
self::$sassFactory->collect();
self::$sassFactory->compile();
self::$sassFactory->write($targetCssPath);
Expand All @@ -51,27 +51,33 @@ public static function writeCSS($rtl = false, $destinationFolder = false)
*/
public static function writeJS($destinationFolder)
{
if($destinationFolder){

if ($destinationFolder) {
$targetJsPath = $destinationFolder. (self::jsFilePath);
} else {
$targetJsPath = PathHelper::getPublicPath(self::jsFilePath);
}

self::$jsFactory = new JavascriptFactory();

$modTimeTarget = filemtime($targetJsPath);

$staticFileModTime = self::$jsFactory->getStaticJSLastEditDate();
$compontentsFileModTime = PathHelper::getLastEditDate(PathHelper::getComponentsPath(), '/^.+\.js$/i') ;


if (!PathHelper::isResourceLocked($targetJsPath)
&& ($staticFileModTime > $modTimeTarget || $compontentsFileModTime > $modTimeTarget)

) {
&& ($staticFileModTime > $modTimeTarget
|| $compontentsFileModTime > $modTimeTarget)) {
// Write minified JS to main.js
self::$jsFactory->collectJS();
self::$jsFactory->write($targetJsPath);
}
}

private static function getWorkingSassFactory ($rtl = false) {
if (extension_loaded('sass')) {
return new SassFactory($rtl);
}

return new FallbackSassFactory($rtl);
}
}
41 changes: 19 additions & 22 deletions src/factories/SassFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@


use CSSJanus;
use Sass;
use it\hce\microframework\core\exceptions\MicroFrameworkException;
use it\hce\microframework\core\helpers\PathHelper;
use Leafo\ScssPhp\Compiler;
use MatthiasMullie\Minify\CSS;

class SassFactory
{
Expand All @@ -18,7 +17,6 @@ class SassFactory
private $compiler;
private $main;
private $mainRtl;
private $minifier;
private $compiledCss;

/**
Expand All @@ -29,10 +27,10 @@ class SassFactory
public function __construct($rtl = false)
{
// load the compiler
$this->compiler = new Compiler();
$this->compiler = new Sass();

//load the minifier
$this->minifier = new CSS();
// set minification
$this->compiler->setStyle(Sass::STYLE_COMPRESSED);

// set rtl
$this->rtl = $rtl;
Expand All @@ -45,7 +43,8 @@ public function __construct($rtl = false)

$this->mainRtl = file_get_contents(PathHelper::getResourcesPath(self::mainRtlScssPath));
}
// load main.scss

// load main.scss
if (!file_exists(PathHelper::getResourcesPath(self::mainScssPath))) {
throw new MicroFrameworkException('main.scss not found');
}
Expand All @@ -58,38 +57,36 @@ public function __construct($rtl = false)
*/
public function collect()
{
$this->compiler->setImportPaths(PathHelper::getResourcesPath(self::scssPath));
$this->compiler->setIncludePath(PathHelper::getResourcesPath(self::scssPath));
}

/**
* Compiles SCSS
* @throws MicroFrameworkException
* @return string the compiled css
*/
public function compile()
{
$compiledSass = $this->compiler->compile($this->main);
try {
$compiledSass = $this->compiler->compile($this->main);

if ($this->rtl) {
$compiledSass = $this->rightToLeft($compiledSass);
$rtlSass = $this->compiler->compile($this->mainRtl);
$compiledSass = $compiledSass . $rtlSass;
}

if ($this->rtl) {
$compiledSass = $this->rightToLeft($compiledSass);
$rtlSass = $this->compiler->compile($this->mainRtl);
$compiledSass = $compiledSass . $rtlSass;
return $this->compiledCss = $compiledSass;
} catch (Exception $e) {
throw new MicroFrameworkException($e->getMessage());
}

// minify the result
return $this->compiledCss = $this->minify($compiledSass);
}

private function rightToLeft($compiledSass)
{
return CSSJanus::transform($compiledSass);
}

private function minify($compiledSass)
{
$this->minifier->add($compiledSass);
return $this->minifier->minify();
}

/**
* Writes the loaded SCSS in a given css file (overwrite)
* @param $file
Expand Down

0 comments on commit a619a92

Please sign in to comment.