Skip to content

Commit

Permalink
Cache files are now stored in the system temp dir (ref #530)
Browse files Browse the repository at this point in the history
The paths specified for checking and used to locate a common path, which is used to generate the cache file name. When checking a new set of code, if a cache file already exists in a common location, that cache file will be reused and added to instead of a new one being created. So it is possible for a cache file to store the results of multiple projects if you first run over all the projects at once. Running over each individually will still end up finding that first cache file.
  • Loading branch information
gsherwood committed May 18, 2015
1 parent c587b27 commit 747bd82
Showing 1 changed file with 76 additions and 9 deletions.
85 changes: 76 additions & 9 deletions src/Util/Cache.php
Expand Up @@ -44,6 +44,10 @@ public static function load(Config $config, Ruleset $ruleset)
// to generate a hash for the code used during the run.
// At this point, the loaded class list contains the core PHPCS code
// and all sniffs that have been loaded as part of the run.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL."\tGenerating loaded file list for code hash".PHP_EOL;
}

$codeHash = '';
$classes = array_keys(Autoload::getLoadedClasses());
sort($classes);
Expand All @@ -60,10 +64,10 @@ public static function load(Config $config, Ruleset $ruleset)
}

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL."\t=> including external file in code hash: ".$file;
echo "\t\t=> external file: $file".PHP_EOL;
}
} else if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL."\t=> including internal sniff file in code hash: ".$file;
echo "\t\t=> internal sniff: $file".PHP_EOL;
}

$codeHash .= md5_file($file);
Expand Down Expand Up @@ -109,7 +113,7 @@ public static function load(Config $config, Ruleset $ruleset)
}

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL."\t=> including core file in code hash: ".$file;
echo "\t\t=> core file: $file".PHP_EOL;
}

$codeHash .= md5_file($file);
Expand All @@ -127,25 +131,88 @@ public static function load(Config $config, Ruleset $ruleset)
);

$configString = implode(',', $configData);
$hash = substr(sha1($configString), 0, 12);
$cacheHash = substr(sha1($configString), 0, 12);

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo PHP_EOL."\t** Cache key data generated **".PHP_EOL;
echo "\tGenerating cache key data".PHP_EOL;
echo "\t\t=> tabWidth: ".$configData['tabWidth'].PHP_EOL;
echo "\t\t=> encoding: ".$configData['encoding'].PHP_EOL;
echo "\t\t=> codeHash: ".$configData['codeHash'].PHP_EOL;
echo "\t\t=> cacheHash: $hash".PHP_EOL;
echo "\t\t=> cacheHash: $cacheHash".PHP_EOL;
}

// Determine the common paths for all files being checked.
// We can use this to locate an existing cache file, or to
// determine where to create a new one.
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\tChecking possible cache file paths".PHP_EOL;
}

$paths = array();
foreach ($config->files as $file) {
$file = Common::realpath($file);
while ($file !== DIRECTORY_SEPARATOR) {
if (isset($paths[$file]) === false) {
$paths[$file] = 1;
} else {
$paths[$file]++;
}

$lastFile = $file;
$file = dirname($file);
if ($file === $lastFile) {
// Just in case something went wrong,
// we don't want to end up in an inifite loop.
break;
}
}
}

ksort($paths);
$paths = array_reverse($paths);

$numFiles = count($config->files);
$tmpDir = sys_get_temp_dir();
$cacheFile = null;
foreach ($paths as $file => $count) {
if ($count !== $numFiles) {
unset($paths[$file]);
continue;
}

$fileHash = substr(sha1($file), 0, 12);
$testFile = $tmpDir.DIRECTORY_SEPARATOR."phpcs.$fileHash.$cacheHash.cache";
if ($cacheFile === null) {
// This will be our default location if we can't find
// an existing file.
$cacheFile = $testFile;
}

if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t\t=> $testFile".PHP_EOL;
echo "\t\t\t * based on shared location: $file *".PHP_EOL;
}

if (file_exists($testFile) === true) {
$cacheFile = $testFile;
break;
}
}//end foreach

if ($cacheFile === null) {
// Unlikely, but just in case $paths is empty for some reason.
$cacheFile = $tmpDir.DIRECTORY_SEPARATOR."phpcs.$cacheHash.cache";
}

self::$path = getcwd().DIRECTORY_SEPARATOR.".phpcs.$hash.cache";
self::$path = $cacheFile;
if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t** Using cache file \"".self::$path.'" **'.PHP_EOL;
echo "\t=> Using cache file: ".self::$path.' **'.PHP_EOL;
}

if (file_exists(self::$path) === true) {
self::$cache = json_decode(file_get_contents(self::$path), true);
} else if (PHP_CODESNIFFER_VERBOSITY > 1) {
echo "\t** Cache file does not exist **".PHP_EOL;
echo "\t* cache file does not exist *".PHP_EOL;
}

self::$cache['config'] = $configData;
Expand Down

0 comments on commit 747bd82

Please sign in to comment.