Skip to content

Commit

Permalink
Refactor caching for small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Riimu committed Mar 15, 2015
1 parent ae78e6f commit 050e7fa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
27 changes: 21 additions & 6 deletions src/CacheListClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,36 @@ public function setCacheHandler(callable $callback)
*/
public function loadClass($class)
{
$result = $this->loadCachedClass($class);

if ($result === false) {
$result = parent::loadClass($class);
}

if ($this->verbose) {
return $result !== false;
}
}

/**
* Attempts loading class from the known class cache.
* @param string $class Full name of the class
* @return boolean True if the class was loaded, false if not
*/
private function loadCachedClass($class)
{
$result = false;

if (isset($this->cache[$class])) {
$result = include $this->cache[$class];

if ($result === false) {
unset($this->cache[$class]);
$this->saveCache();
$result = parent::loadClass($class);
}
} else {
$result = parent::loadClass($class);
}

if ($this->verbose) {
return $result !== false;
}
return $result !== false;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/FileCacheClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ private function createCache(array $cache)
{
ksort($cache);

$string = '<?php return [' . PHP_EOL;
$format = "\t%s => %s," . PHP_EOL;
$rows = [];

foreach ($cache as $key => $value) {
$string .= sprintf($format, var_export($key, true), var_export($value, true));
$rows[] = sprintf($format, var_export($key, true), var_export($value, true));
}

return $string . '];' . PHP_EOL;
return sprintf('<?php return [%s];' . PHP_EOL, PHP_EOL . implode('', $rows));
}
}

0 comments on commit 050e7fa

Please sign in to comment.