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
64 changes: 48 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# LESS support for Laravel 5.x without Node.js
Use LESS with your Laravel installation. Uses [oyejorge/less.php](http://lessphp.gpeasy.com/) instead of [leafo/lessphp](http://leafo.net/lessphp/) which is a more actively developed port of the official LESS processor.
# LESS support (with minifying) for Laravel 5.x without Node.js
Use LESS with your Laravel installation. Uses [oyejorge/less.php](http://lessphp.gpeasy.com/) instead of [leafo/lessphp](http://leafo.net/lessphp/) which is a more actively developed port of the official LESS processor.

Use [matthiasmullie/minify](https://github.com/matthiasmullie/minify) for minifying.

## Features
- Can modify LESS variables on-the-fly
- Can parse custom CSS/LESS and append it to the resulting file
- Works with Twitter Bootstrap v3.3.5 (thanks to oyejorge/less.php)
- Caching support
- Minifying support

## Installation

Expand All @@ -17,20 +20,32 @@ First, pull in the package through Composer.
}
```

And then, if using Laravel 5, include the service provider within `config/app.php`.
And then, include the service provider within `config/app.php`.

Laravel >= 5.1

```php
'providers' => [
'Langemike\Laravel5Less\LessServiceProvider'
Langemike\Laravel5Less\LessServiceProvider::class
];

'aliases' => [
'Less' => Langemike\Laravel5Less\LessFacade::class
];

```

In the aliases section, add:
Laravel 5.0 :

```php
'providers' => [
'Langemike\Laravel5Less\LessServiceProvider'
];

'aliases' => [
'Less' => 'Langemike\Laravel5Less\LessFacade'
];

```

## Configuration
Expand All @@ -40,46 +55,63 @@ with Artisan:
````
$ php artisan vendor:publish
````
This will create a config file 'less.php' in your config directory.
This will create a config file `less.php` in your config directory.

### Settings

You can specify your configuration through 3 options: `.env`, `config.php` file and through `$options` parameter.
You can specify your configuration through 3 options: `.env`, `config/less.php` file or through `$options` argument.

Your .env configuration will be used by default, it will be overridden by it's config.php settings, but the $options parameter will have the highest preference.
Your `.env` configuration will be used by default, it will be overridden by the settings in `config/less.php`, but the `$options` argument will have the highest preference.

### Recompilation
Additionally you can (and probably should) have different configurations for development

Additionally you can (and probably should) have different configurations for development
and production. Specifically, you probably don't want to be generating css files on
your production server, since it will slow down your site.

- change -- Check if LESS file(s) are modified. If it does, recompile CSS
- never -- Don't check, don't recompile.
- always -- Always rewrite CSS
- `change` -- Check if LESS file(s) are modified. If it does, recompile CSS
- `never` -- Don't check, don't recompile (best choice on a production server).
- `always` -- Always rewrite CSS

Set `LESS_RECOMPILE` in `.env`

````
LESS_RECOMPILE=change
````

## Minify

By default, the generated css is not minified. To minify it, just set `LESS_MINIFY` to `true` in `.env`

````
LESS_MINIFY=true
````

## Usage

By default, you must create your `.less` files into `resources/assets/less`

Within your models or controllers, you can perform modification to the outputted CSS. Here are some examples:
before you perform a redirect...

```php
public function recompileCSS()
{
Less::modifyVars(['@body-bg' => 'pink'])->recompile('filename');

Less::modifyVars(['@body-bg' => 'pink'])->recompile('filename_wo_ext');
return Redirect::back();
}
```

Within your view you can use the `Less::url()` function to link to your generated CSS

```html
<link href="{!! Less::url('filename') !!}" rel="stylesheet" />
<link href="{!! Less::url('filename_wo_ext') !!}" rel="stylesheet" />
```

Passing `true` as the second parameter to `Less::url()` will auto-detect, based on your configuration, if recompilation is needed and will do so accordingly.
If necessary, passing `false` as second parameter to `Less::url()` will avoid all recompilation (already if you set `LESS_RECOMPILE` to `always`).

## Credits
This project is inspired by [Less4Laravel](https://github.com/jtgrimes/less4laravel).
Without the hard work of [oyejorge/less.php](http://lessphp.gpeasy.com/) this project wouldn't be possible.

Minification added by [Sebastien HEYD](http://sheyd.fr)
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
],
"require": {
"php": ">=5.5.0",
"oyejorge/less.php": "~1.5"
"oyejorge/less.php": "~1.5",
"matthiasmullie/minify": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
14 changes: 12 additions & 2 deletions src/Less.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use lessc;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Contracts\Cache\Repository as Cache;
use MatthiasMullie\Minify;

class Less {

Expand Down Expand Up @@ -45,6 +46,11 @@ public function compile($filename, $options = array()) {
if ($written === true) {
$this->cleanCache();
}
// Minifying CSS
if(env('LESS_MINIFY', false)) {
$minifier = new Minify\CSS($output_path);
$minifier->minify($output_path);
}
return $written;
}

Expand All @@ -68,6 +74,9 @@ public function fresh() {
*/
protected function writeCss($output_path, $css) {
$css = str_replace('{relative_path_fix}', '..', $css);
if(!is_dir(dirname($output_path))) {
mkdir(dirname($output_path), 0777, true);
}
return file_put_contents($output_path, $css) !== false;
}

Expand Down Expand Up @@ -99,6 +108,7 @@ protected function cleanCache() {
* @return bool true on recompiled, false when not
*/
public function recompile($filename, $recompile = null, $options = array()) {

if ($this->recompiled === true) {
return false; // This instance is already recompiled. Recompile a new or the same instance using Less::fresh()
}
Expand Down Expand Up @@ -208,9 +218,9 @@ public function parseVariables($less) {
* @param bool $auto_recompile Automaticly recompile
* @return string CSS url
*/
public function url($filename, $auto_recompile = false) {
public function url($filename, $auto_recompile = true) {
if ($auto_recompile) {
$recompiled = $this->recompile($filename);
$this->recompile($filename);
}
$css_path = $this->config->get('less.link_path', '/css') . '/' . $filename . '.css';
return asset($css_path);
Expand Down