Skip to content
This repository has been archived by the owner on Jun 2, 2018. It is now read-only.

Commit

Permalink
Got rid of internal compression
Browse files Browse the repository at this point in the history
Newer upstream lessphp has it anyway.
  • Loading branch information
Oreolek committed Jun 14, 2014
1 parent d83891b commit 4d34fa5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 96 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ To Use
2. Include less module in your application's bootstrap: 'less' => MODPATH.'less'
3. Copy the less config file from /modules/less/config/less.php to your application's config directory
4. From your less.php config file, put the 'path' to where you want the CSS files compiled / compressed, the folder must be writable
5. You can set 'compress' to TRUE on your less.php config file if you want your CSS files to be combined in to one file and compressed (to lessen server calls)
5. You can set 'compress' to TRUE on your less.php config file if you want your CSS files to be minified

Sample Code
------------
Expand Down Expand Up @@ -100,4 +100,4 @@ Default less files extension is set in `Less::$extension` and is `.less`.

Issues
-------
Please report it to the [issues tracker](http://github.com/mongeslani/kohana-less/issues)..
Please report it to the [issues tracker](http://github.com/mongeslani/kohana-less/issues)..
102 changes: 8 additions & 94 deletions classes/Less/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@ public static function compile($array = '', $media = 'screen')
// get less config
$config = Kohana::$config->load('less');

// if compression is allowed
if ($config['compress'])
{
return HTML::style(self::_combine($stylesheets), array('media' => $media));
}

// if no compression
foreach ($stylesheets as $file)
{
Expand Down Expand Up @@ -113,92 +107,6 @@ protected static function _get_filename($file, $path)
return $filename;
}

/**
* Combine the files
*
* @param array array of asset files
* @return string path to the asset file
*/
protected static function _combine($files)
{
// get assets' css config
$config = Kohana::$config->load('less');

// get the most recent modified time of any of the files
$last_modified = self::_get_last_modified($files);

// compose the asset filename
$compiled = md5(implode('|', $files)).'-'.$last_modified.'.css';

// compose the path to the asset file
$filename = $config['path'].$compiled;

// if the file exists no need to generate
if ( ! file_exists($filename))
{
self::_generate_assets($filename, $files);
}

return $filename;
}

/**
* Generate an asset file
*
* @param string filename of the asset file
* @param array array of source files
*/
protected static function _generate_assets($filename, $files)
{
// create data holder
$data = '';

touch($filename);

ob_start();

foreach($files as $file)
{
$data .= file_get_contents($file);
}

echo $data;

file_put_contents($filename, ob_get_clean(), LOCK_EX);

self::_compile($filename);
}

/**
* Compiles the file from less to css format
*
* @param string path to the file to compile
*/
public static function _compile($filename)
{
if (Kohana::$config->load('less.vendor_internal') === TRUE)
{
require_once '../vendor/lessphp/lessc.inc.php';
$less = new lessc($filename);

try
{
$compiled = $less->parse();
$compressed = self::_compress($compiled);
file_put_contents($filename, $compressed);
}
catch (LessException $ex)
{
exit($ex->getMessage());
}
} else {
$compiled = shell_exec('lessc ' . $filename);
if (is_null($compiled)) exit($compiled);
$compressed = self::_compress($compiled);
file_put_contents($filename, $compressed);
}
}

/**
* compile to $in to $out if $in is newer than $out
* @param string $original path to original file
Expand All @@ -207,13 +115,19 @@ public static function _compile($filename)
**/
public static function _ccompile($original, $compiled)
{
if (Kohana::$config->load('less.vendor_internal') === TRUE)
$config = Kohana::$config->load('less');
if ($config['vendor_internal'])
{
require_once '../vendor/lessphp/lessc.inc.php';
return lessc::ccompile($original, $compiled);
} else {
if (!is_file($compiled) || filemtime($original) > filemtime($compiled)) {
return (int) shell_exec('lessc ' . $original . ' >' . $compiled);
$command = 'lessc';
if ($config['compress'])
{
$command .= ' --clean-css';
}
return (int) shell_exec($command.' '.$original.' >'.$compiled);
} else {
return true;
}
Expand Down

0 comments on commit 4d34fa5

Please sign in to comment.