Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a method to use the caching method of lessphp #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/vendor
composer.lock
.DS_Store
/nbproject/private/
*.properties
project.xml
48 changes: 48 additions & 0 deletions src/Zizaco/Lessy/Lessy.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,52 @@ public function compileSingleFile( $filename, $verbose = false )
);
}

/**
* complies and caches the less files into one css file
*/
public function cachedCompileLessFile()
{

$root = $this->_app['path'] . '/';
$inputFile = $this->_app['config']->get('lessy::originFile');
$outputFile = $this->_app['config']->get('lessy::destinationFile');

if (empty($inputFile))
$inputFile = 'less/main.less';

if (empty($outputFile))
$outputFile = '../public/assets/css/main.css';

$inputFile = $root . $inputFile;
$outputFile = $root . $outputFile;

// cachedCompile method takes an optional second argument, $force.
// Passing in true will cause the input to always be recompiled.
$force = false;

// lessjs (default) — Same style used in LESS for JavaScript
// compressed — Compresses all the unrequired whitespace
// classic — lessphp’s original formatter
$formatter = $this->_app['config']->get('lessy::formatter');

// load the cache
$cacheFile = $inputFile . ".cache";

if (file_exists($cacheFile)) {
$cache = unserialize(file_get_contents($cacheFile));
} else {
$cache = $inputFile;
}

$less = new lessc;
$less->setFormatter($formatter);

$newCache = $less->cachedCompile($cache, $force);

if (!is_array($cache) || $newCache["updated"] > $cache["updated"]) {
file_put_contents($cacheFile, serialize($newCache));
file_put_contents($outputFile, $newCache['compiled']);
}
}

}
7 changes: 6 additions & 1 deletion src/Zizaco/Lessy/LessyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ public function boot()
// Compiles less file if manual_compile_only is not enabled
if (! $this->app['config']->get('lessy::manual_compile_only'))
{
$lessy->compileLessFiles();
// compiles and caches the less files into a single css file
if($this->app['config']->get('lessy::single_file')) {
$lessy->cachedCompileLessFile();
} else {
$lessy->compileLessFiles();
}
}
}
}
Expand Down
26 changes: 23 additions & 3 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
|
*/

'origin' => 'less',

'destination' => '../public/assets/css',
'origin' => 'less',

'destination' => '../public/assets/css',

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -46,4 +45,25 @@

'manual_compile_only' => false,

/*
|--------------------------------------------------------------------------
| Singe File
|--------------------------------------------------------------------------
|
| This option will force all less output into a single file and cache it.
|
*/

'single_file' => 'false',

// source file responisble for glueing together the less files into a single
// css output file
'originFile' => 'less/main.less',

// file to be compiled from the less source files
'destinationFile' => '../public/assets/css/main.css',

// the formatting for the outputted css file
'formatter' => 'lessjs',

);