Skip to content

Conversation

Bluestreak2k5
Copy link

I've updated the checkedCompile method to allow for multiple files to be checked instead of just the input.

/*
* Checks 1 or more files modified time to see if they are newer then the output file.
* If a file is newer then the output file, it compiles the file into the output file.
*
* @param mixed $in: 1 filename or an array of filenames to be checked against.
* @param string filename $out: the output file to compare the time against.
* @param string filename $fileToCompile: if passing an array, the file to compile if
*       one of the files is newer then the output.
*/
public function checkedCompile($in, $out, $fileToCompile = NULL) {
    if( is_array($in) )
    {
        if( !isset( $fileToCompile ))
        {
            throw new Exception('The 3rd argument must be set as the file you want compiled if one fo the files is newer');
        }

        foreach( $in as $file )
        {
            if(!is_file($out) || filemtime($file) > filemtime($out))
            {
                $this->compileFile($fileToCompile, $out);
                return true;
            }
        }
    }else
    {
        if (!is_file($out) || filemtime($in) > filemtime($out)) 
        {
            $this->compileFile($in, $out);
            return true;
        }
    }
    return false;
}

And it can be used as such like this to check any of your less files for the modified time to be greater then the modified time on the output file
You can also still use the original way, with no problems as shown with the admin section.

require 'lessc.inc.php';

$less = new lessc;
$less->setFormatter("classic");
if (array_key_exists('HTTP_HOST', $_SERVER))
{
    if (stripos($_SERVER['REQUEST_URI'], 'admin') !== FALSE)
    {
        try
        {
            $less->checkedCompile('less/admin.less', 'css/admin.css');
        } catch (exception $ex)
        {
            exit('lessc fatal error:<br />'.$ex->getMessage());
        }
    }else
    {
        $file_array = array
        (
            'less/common.less', 
            'less/input.less',
            'less/mixins.less',
            'less/reset.less',
            'less/site.less'
        );
        try 
        {
            $less->checkedCompile($file_array, 'css/site.css', 'less/input.less');
        } catch (exception $ex) 
        {
            exit('lessc fatal error:<br />'.$ex->getMessage());
        }
    }
}

Where the file_array is the list of all your less files, input.less is the main less file that imports all the files, and site.css is the output file.

…ay of files to be checked against the output file
@pikilon
Copy link

pikilon commented Mar 8, 2013

I have wrote a function to solve this outside the class, I think it is better so you can update it and you can use the very same function in other files (like Js)...

Here you have the function/s (I have split it in 3 of them).
So you can use something like this when you include it

    $css = array (
    "normalize.css", 
    "main.css", 
    "style.css", 
);

$less->checkedCompile(
    join_updated_files($css,"precompiled_css.less"),
    "less_rendered.css"
);

So it's easy. Here is the code, I've included the "to_array" function because is very clear to me.
if one of the files is not found the function breaks and echoes "file not found"

function join_updated_files($files,$out,$siempre=false) {
    $files = to_array($files);
    $new = !file_exists($out) ? true : $siempre;
    if (!$new)  {
        foreach ($files as $file) {

            if (!file_exists($file)) {echo $file." file not found";return false;}
            if (filemtime($file) > filemtime($out)) {
                $new = true;
                break;
            }
        }
    }
    $result = $new? join_files($files,$out) : $out;
    return $result;
}

function join_files ($files,$out) {
    $files = to_array($files);
    $total = "";
    foreach ($files as $file) {
        if (!file_exists($file)) {echo $file." file not found";return false;}
        $total .= file_get_contents($file);
    }
    if (file_put_contents($out, $total)) {
        return $out;
    } else {
        return false;
    }

}

function to_array($variable) {
    $variable = is_array($variable)? $variable : array($variable);
    return $variable;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants