Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

Commit

Permalink
ProxyManager use locking when reading auto generated files
Browse files Browse the repository at this point in the history
fixes #122
  • Loading branch information
cebe committed Dec 11, 2017
1 parent a4e2b75 commit 040abb4
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,15 @@ public function __initializeProperty(\$propertyName)
PROXY;

$this->checkProxyDirectory();
file_put_contents($proxyFile, $content);
file_put_contents($proxyFile, $content, LOCK_EX);

if (!class_exists($proxyClass)) {
// using locks to avoid reading a partially written file
$fo = fopen($proxyFile, 'r');
flock($fo, LOCK_SH);
require $proxyFile;
flock($fo, LOCK_UN);
fclose($fo);
}

return $this->newProxyInstance($proxyClass);
Expand Down Expand Up @@ -197,7 +202,7 @@ private function newProxyInstance($proxyClass)
if (!array_key_exists($proxyClass, $prototypes)) {
$rc = @unserialize(sprintf('C:%d:"%s":0:{}', strlen($proxyClass), $proxyClass));

if (false === $rc) {
if (false === $rc || $rc instanceof \__PHP_Incomplete_Class) {
$rc = new \ReflectionClass($proxyClass);
return $rc->newInstanceWithoutConstructor();
}
Expand Down

0 comments on commit 040abb4

Please sign in to comment.