diff --git a/README.md b/README.md index c3b17e2d..c17e4430 100644 --- a/README.md +++ b/README.md @@ -1,64 +1,104 @@ -# lessphp v0.3.4-2 -### +# A note about Laravel 4 -`lessphp` is a compiler for LESS written in PHP. The documentation is great, -so check it out: . +With Laravel 4, this bundle is so easy to replicate, I'm not going to support it anymore. I'll give you a quick how-to here though, just in case. -Here's a quick tutorial: +1. Require [lessphp](https://packagist.org/packages/leafo/lessphp) in composer.json +2. Write some code like this (maybe in your global start file): -### How to use in your PHP project +```php -Copy `lessc.inc.php` to your include directory and include it into your project. +$less = new lessc; -There are a few ways to interface with the compiler. The easiest is to have it -compile a LESS file when the page is requested. The static function -`lessc::ccompile`, checked compile, will compile the input LESS file only when it -is newer than the output file. +// Compile a file +$less->compileFile('app/less/input.less', 'public/css/output.css'); - try { - lessc::ccompile('input.less', 'output.css'); - } catch (exception $ex) { - exit($ex->getMessage()); - } +// Compile based on modified time +$less->checkedCompile('app/less/input.less', 'public/css/output.css'); -`lessc::ccompile` is not aware of imported files that change. Read [about -`lessc::cexecute`](http://leafo.net/lessphp/docs/#compiling_automatically). +// Compile a 'snippet' +$css = $less->compile('#myElem { background-color: #f00; }'); +// And then write to file? -Note that all failures with lessc are reported through exceptions. -If you need more control you can make your own instance of lessc. +``` - $input = 'mystyle.less'; +# laraveless - Automated LESS compilation for Laravel - $lc = new lessc($input); +Thanks to [leafo](https://github.com/leafo) for [lessphp](https://github.com/leafo/lessphp). - try { - file_put_contents('mystyle.css', $lc->parse()); - } catch (exception $ex) { ... } +### Quick Start -In addition to loading from file, you can also parse from a string like so: +1. Install the bundle - $lc = new lessc(); - $lesscode = 'body { ... }'; - $out = $lc->parse($lesscode); + php artisan bundle:install laraveless -### How to use from the command line +1. Add it to your application's `bundles.php` -An additional script has been included to use the compiler from the command -line. In the simplest invocation, you specify an input file and the compiled -css is written to standard out: + return array( + 'laraveless' => array( + 'auto' => true, + ), + ); - $ plessc input.less > output.css +1. Create `application/config/less.php` -Using the -r flag, you can specify LESS code directly as an argument or, if -the argument is left off, from standard in: + return array( + 'directories' => array( + ... + ), + 'files' => array( + ... + ), + 'snippets' => array( + ... + ), + ); - $ plessc -r "my less code here" +1. That's it, really. -Finally, by using the -w flag you can watch a specified input file and have it -compile as needed to the output file +### More about `directories`, `files`, and `snippets` - $ plessc -w input-file output-file +`laraveless` uses the `directories`, `files`, and `snippets` arrays to create CSS from your LESS. Here's how. -Errors from watch mode are written to standard out. +#### Directories +The `directories` array is optional. If you don't have any directories that you want compiled to LESS, you can leave it out; otherwise, you want to specify your directories in `application/config/less.php` as so: + return array( + 'directories' => array( + path('app') . 'less' => path('public') . 'css', + ), + ); + +That will get all files matching `application/less/*.less` (case-insensitive, non-recursive) and compile them to CSS in the `public/css` directory. For example, the file `application/less/test.less` becomes `public/css/test.css`. + +If you want to specify single LESS files or want to specify the output filename, use `files`. + +#### Files + +The `files` array is nearly identical to the `directories` array. In `application/config/less.php`: + + return array( + 'files' => array( + path('app') . 'less/source.less' => path('public') . 'css/destination.css', + ), + ); + +That will... you get it, right?... compile `application/less/source.less` to `public/css/destination.css`. + +#### Snippets + +I added snippets just because. They're probably not great practice, but maybe you'll find a use for them. Once again, in `application/config/less.php`: + + return array( + 'snippets' => array( + '#content { background: #f00; h1 { color: #0f0; } }' => path('public') . 'css/file.css', + ), + ); + +That should be pretty self-explanatory. + +### One final note + +`laraveless` uses `lessphp`'s `ccompile` method, which means that only source less files modified more recently than the destination css file are compiled, so you shouldn't take a performance hit. + +... except for on snippets, which are compiled and written to disk every time the bundle is started. diff --git a/start.php b/start.php new file mode 100644 index 00000000..3604c14a --- /dev/null +++ b/start.php @@ -0,0 +1,76 @@ +[^";]+)"/ism', File::get($file), $matches); + foreach ($matches['imports'] as $import) + { + $path = dirname($file) . '/' . $import; + if (File::exists($path) || File::exists($path .= '.less')) + { + $paths[] = $path; + $paths = array_merge($paths, $imports($path)); + } + } + + return $paths; +}; + +$compile = function($input_file, $output_file) use ($imports) +{ + try + { + $latest = File::modified($input_file); + foreach ($imports($input_file) as $import) + { + $import_modified = File::modified($import); + $latest = $import_modified > $latest ? $import_modified : $latest; + } + + if (! File::exists($output_file) || $latest > File::modified($output_file)) + { + $cache = lessc::cexecute($input_file); + File::put($output_file, $cache['compiled']); + } + } + catch (Exception $ex) + { + exit('lessc fatal error:
' . $ex->getMessage()); + } +}; + +if (isset($config['directories'])) +{ + foreach ($config['directories'] as $less_dir => $css_dir) + { + $less_dir = rtrim($less_dir, '/') . '/'; + foreach (glob($less_dir . '*.[Ll][Ee][Ss][Ss]') as $less) + { + $css = rtrim($css_dir, '/') . '/' . basename($less, '.less') . '.css'; + $compile($less, $css); + } + } +} + +if (isset($config['files'])) +{ + foreach ($config['files'] as $less => $css) + { + $compile($less, $css); + } +} + +if (isset($config['snippets'])) +{ + $less = new lessc(); + foreach ($config['snippets'] as $snippet => $css) + { + File::put($css, $less->parse($snippet)); + } +} \ No newline at end of file