Skip to content
Permalink
Browse files

single file WIP

  • Loading branch information
dg committed Feb 12, 2020
1 parent 6cb7c71 commit 0e16a88ca6de7874d80422a39ab094349aa8985b
Showing with 18 additions and 15 deletions.
  1. +18 −15 src/RobotLoader/RobotLoader.php
@@ -411,45 +411,48 @@ public function setTempDirectory(string $dir): self
private function loadCache(): void
{
$file = $this->getCacheFile();
$lock = fopen("$file.lock", 'cb+');
if (!$lock || !flock($lock, LOCK_SH)) {
throw new \RuntimeException("Unable to create or acquire shared lock on file '$file.lock'.");
$handle = fopen($file, 'cb+');
if (!$handle || !flock($handle, LOCK_SH)) {
throw new \RuntimeException("Unable to create or acquire shared lock on file '$file'.");
}

[$this->classes, $this->missing] = @include $file; // @ file may not exist
[$this->classes, $this->missing] = include $file;
if (is_array($this->classes)) {
return;
}

if (!flock($lock, LOCK_EX)) {
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file.lock'.");
if (!flock($handle, LOCK_EX)) {
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file'.");
}

// while waiting for the lock, someone might have already created the cache
[$this->classes, $this->missing] = @include $file; // @ file may not exist
if (is_array($this->classes)) {
return;
if (fstat($handle)['size']) {
flock($handle, LOCK_SH);
[$this->classes, $this->missing] = include $file;
if (is_array($this->classes)) {
return;
}
}

$this->classes = $this->missing = [];
$this->refreshClasses();
$this->saveCache($lock);
$this->saveCache($handle);
}


/**
* Writes class list to cache.
*/
private function saveCache($lock = null): void
private function saveCache($handle = null): void
{
$file = $this->getCacheFile();
$lock = $lock ?: fopen("$file.lock", 'cb+');
if (!$lock || !flock($lock, LOCK_EX)) {
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file.lock'.");
$handle = $handle ?: fopen($file, 'cb+');
if (!$handle || !flock($handle, LOCK_EX)) {
throw new \RuntimeException("Unable to create or acquire exclusive lock on file '$file'.");
}

$code = "<?php\nreturn " . var_export([$this->classes, $this->missing], true) . ";\n";
if (file_put_contents($file, $code) !== strlen($code)) {
if (!ftruncate($handle, 0) || fwrite($handle, $code) !== strlen($code)) {
@unlink($file); // @ - file may not exist
throw new \RuntimeException("Unable to create '$file'.");
}

0 comments on commit 0e16a88

Please sign in to comment.
You can’t perform that action at this time.