Skip to content
Closed
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ result or write it to the path specified by an optional second argument.
echo $less->compileFile("input.less");
```

The `compileChecked` method is like `compileFile`, but it only compiles if the output
The `checkedCompile` method is like `compileFile`, but it only compiles if the output
file doesn't exist or it's older than the input file:

```php
Expand Down
2 changes: 1 addition & 1 deletion docs/docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1023,7 +1023,7 @@ result or write it to the path specified by an optional second argument.
echo $less->compileFile("input.less");
```

The `compileChecked` method is like `compileFile`, but it only compiles if the output
The `checkedCompile` method is like `compileFile`, but it only compiles if the output
file doesn't exist or it's older than the input file:

```php
Expand Down
37 changes: 37 additions & 0 deletions lessc.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1930,6 +1930,43 @@ public function compileFile($fname, $outFname = null) {
return $out;
}

/**
* Based on explicit input/output files does a full change check on cache before compiling.
*
* @param string $in
* @param string $out
* @param boolean $force
* @return string Compiled CSS results
* @throws Exception
*/
public function checkedCachedCompile($in, $out, $force = false) {
if (!is_file($in) || !is_readable($in)) {
throw new Exception('Invalid or unreadable input file specified.');
}
if (is_dir($out) || !is_writable(file_exists($out) ? $out : dirname($out))) {
throw new Exception('Invalid or unwritable output file specified.');
}

$outMeta = $out . '.meta';
$metadata = null;
if (!$force && is_file($outMeta)) {
$metadata = unserialize(file_get_contents($outMeta));
}

$output = $this->cachedCompile($metadata ? $metadata : $in);

if (!$metadata || $metadata['updated'] != $output['updated']) {
$css = $output['compiled'];
unset($output['compiled']);
file_put_contents($out, $css);
file_put_contents($outMeta, serialize($output));
} else {
$css = file_get_contents($out);
}

return $css;
}

// compile only if changed input has changed or output doesn't exist
public function checkedCompile($in, $out) {
if (!is_file($out) || filemtime($in) > filemtime($out)) {
Expand Down
16 changes: 16 additions & 0 deletions tests/ServerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

class ServerTest extends \PHPUnit_Framework_TestCase
{
public function testCheckedCachedCompile()
{
$server = new lessc();
$server->setImportDir(__DIR__ . '/inputs/test-imports/');
$css = $server->checkedCachedCompile(__DIR__ . '/inputs/import.less', '/tmp/less.css');

$this->assertFileExists('/tmp/less.css');
$this->assertFileExists('/tmp/less.css.meta');
$this->assertEquals($css, file_get_contents('/tmp/less.css'));
$this->assertNotNull(unserialize(file_get_contents('/tmp/less.css.meta')));
}
}