Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Active compiled style files are being pruned away #20

Merged
merged 3 commits into from
Nov 24, 2012
Merged
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 .svnignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
*.idea
*.git
.git*
lib/vendor/lessphp/.git
13 changes: 13 additions & 0 deletions lib/Configuration.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,17 @@ public function getTtl()
{
return $this->ttl;
}

/**
* Sets the TTL fo a compiled CSS file
*
* @api
* @param $ttl
* @since 1.5.1
*/
public function setTtl($ttl)
{
$this->ttl = (int)$ttl;
}

}
53 changes: 52 additions & 1 deletion lib/Garbagecollector.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

class WPLessGarbagecollector
{
protected $configuration;
/**
* @static
* @var int Max number of compiled versions of a same file to keep
*/
public static $COMPILED_VERSIONS = 3;

/**
* @protected
* @var WPLessConfiguration
*/
protected $configuration;

public function __construct(WPLessConfiguration $configuration)
{
Expand All @@ -16,6 +26,7 @@ public function __construct(WPLessConfiguration $configuration)
public function clean()
{
$outdated_files = $this->getOutdatedFiles($this->configuration->getTtl());
$this->filterVersions($outdated_files);

if (!empty($outdated_files))
{
Expand Down Expand Up @@ -55,6 +66,46 @@ protected function getOutdatedFiles($ttl)
return $outdated;
}

protected function filterVersions(array &$outdated_files)
{
$groups = array();
$keep = array();

// Grouping and collecting data
foreach ($outdated_files as $index => &$file)
{
$m = array();
preg_match_all('#^(?P<group>.+)-(?P<token>[^\-\.]+).css$#sU', $file, $m, PREG_SET_ORDER);

if (empty($m))
{
continue;
}

if (!isset($groups[ $m['group'] ]))
{
$groups[ $m['group'] ] = array();
}

$groups[ $m['group'] ][ $file ] = filemtime($file);
}

// Capping groups
foreach ($groups as &$versions)
{
arsort($versions);
$chunks = array_chunk($versions, self::$COMPILED_VERSIONS);

if (!empty($chunks[0]))
{
$keep = array_merge($keep, $chunks[0]);
}
}

// Returning the diff
return array_diff($outdated_files, $keep);
}

/**
* Remove a bunch of files
*
Expand Down