cypresslab/less-elephant
PHP wrapper around the Less (lessc) binary to manage and compile LESS projects. Uses Symfony Finder and Process, supports Composer/PEAR installation, includes PHPUnit tests, and follows Symfony2 coding standards. Requires PHP 5.3+ and a *nix system with lessc installed.
Installation:
composer require cypresslab/less-elephant:^1.0.0
Ensure lessc (LESS compiler binary) is installed on your *nix system.
First Use Case:
Compile a single .less file to .css:
use Cypresslab\LessElephant\LessProject;
$project = new LessProject(
__DIR__ . '/path/to/less/files',
'styles.less',
__DIR__ . '/path/to/output/styles.css'
);
if (!$project->isClean()) {
$project->compile();
}
Key Files:
vendor/cypresslab/less-elephant/src/ – Core classes.vendor/cypresslab/less-elephant/tests/ – Usage examples in tests.Project Initialization:
.less file, and output .css path.LessBinary path:
$project = new LessProject(
'/less/src',
'main.less',
'/css/output.css',
'my-project',
new LessBinary('/custom/lessc')
);
Compile on Demand:
if (!$project->isClean()) { // Checks file timestamps via Symfony Finder
$project->compile(); // Uses Symfony Process to run `lessc`
}
Batch Processing:
$projects = [
new LessProject('/src', 'file1.less', '/output/file1.css'),
new LessProject('/src', 'file2.less', '/output/file2.css'),
];
foreach ($projects as $project) {
if (!$project->isClean()) $project->compile();
}
Symfony Integration:
LessElephantBundle for dependency injection:
# config.yml
less_elephant:
projects:
default:
src: '%kernel.project_dir%/assets/less'
input: 'main.less'
output: '%kernel.project_dir%/web/css/main.css'
$this->get('less_elephant.project_manager')->compile('default');
assets:compile or view.compiled events.inotifywait (Linux) or nodemon (cross-platform) for live reloading.$outputPath = $project->getOutputPath() . '?v=' . filemtime($project->getInputPath());
Binary Path Assumptions:
lessc in $PATH. Explicitly set path if missing:
$project = new LessProject(..., ..., ..., ..., new LessBinary('/usr/local/bin/lessc'));
File Permissions:
www-data) has write access to output directories:
chmod -R 775 /path/to/output
chown -R www-data:www-data /path/to/output
Symfony Finder Quirks:
isClean() may return false if LESS files are symlinked (timestamp mismatches).$project->setFinder(function (Finder $finder) {
$finder->exclude('vendor');
});
LESS Compiler Errors:
compile() throws RuntimeException on failure. Catch and log:
try {
$project->compile();
} catch (\RuntimeException $e) {
Log::error($e->getMessage());
}
Verbose Output: Enable Symfony Process logging:
$project->setVerbose(true); // Shows `lessc` command output
Dry Runs: Test without writing files:
$project->compile(true); // Dry run (no file writes)
Custom Compilation Logic:
Override LessProject::compile() to add pre/post-processing:
class CustomLessProject extends LessProject {
protected function compile($dryRun = false) {
// Pre-compile steps (e.g., minification)
parent::compile($dryRun);
// Post-compile steps (e.g., CSS optimization)
}
}
Plugin System:
Extend LessBinary to support custom LESS plugins:
class PluginLessBinary extends LessBinary {
public function __construct($binaryPath) {
parent::__construct($binaryPath);
$this->addPlugin('--plugin=path/to/plugin.less');
}
}
Event Dispatching: Use Laravel’s events to trigger actions on compile:
event(new LessCompiled($project->getOutputPath()));
Disable Staleness Checks:
For static sites, bypass isClean() and force recompilation:
$project->setClean(false); // Force recompile
Parallel Compilation:
Use parallel package to compile multiple projects concurrently:
\Parallel::run(function () use ($projects) {
foreach ($projects as $project) {
if (!$project->isClean()) $project->compile();
}
});
How can I help you explore Laravel packages today?