-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(loader): lock load class on file loader to avoid race condition
- Loading branch information
Showing
7 changed files
with
46 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,13 +11,14 @@ | |
use AutoMapper\Metadata\MetadataRegistry; | ||
use PhpParser\PrettyPrinter\Standard; | ||
use PhpParser\PrettyPrinterAbstract; | ||
use Symfony\Component\Lock\LockFactory; | ||
|
||
/** | ||
* Use file system to load mapper, and persist them using a registry. | ||
* | ||
* @author Joel Wurtz <[email protected]> | ||
* | ||
* @internal | ||
* @internal | ||
*/ | ||
final class FileLoader implements ClassLoaderInterface | ||
{ | ||
|
@@ -30,6 +31,7 @@ public function __construct( | |
private readonly MapperGenerator $generator, | ||
private readonly MetadataFactory $metadataFactory, | ||
private readonly string $directory, | ||
private readonly LockFactory $lockFactory, | ||
private readonly FileReloadStrategy $reloadStrategy = FileReloadStrategy::ON_CHANGE, | ||
) { | ||
$this->printer = new Standard(); | ||
|
@@ -40,25 +42,33 @@ public function loadClass(MapperMetadata $mapperMetadata): void | |
$className = $mapperMetadata->className; | ||
$classPath = $this->directory . \DIRECTORY_SEPARATOR . $className . '.php'; | ||
|
||
if ($this->reloadStrategy === FileReloadStrategy::NEVER && file_exists($classPath)) { | ||
require $classPath; | ||
// We lock the file here, because another process could be writing the file at the same time | ||
$lock = $this->lockFactory->createLock($className); | ||
$lock->acquire(true); | ||
|
||
return; | ||
} | ||
try { | ||
if ($this->reloadStrategy === FileReloadStrategy::NEVER && file_exists($classPath)) { | ||
require $classPath; | ||
|
||
$shouldBuildMapper = true; | ||
return; | ||
} | ||
|
||
if ($this->reloadStrategy === FileReloadStrategy::ON_CHANGE) { | ||
$registry = $this->getRegistry(); | ||
$hash = $mapperMetadata->getHash(); | ||
$shouldBuildMapper = !isset($registry[$className]) || $registry[$className] !== $hash || !file_exists($classPath); | ||
} | ||
$shouldBuildMapper = true; | ||
|
||
if ($shouldBuildMapper) { | ||
$this->createGeneratedMapper($mapperMetadata); | ||
} | ||
if ($this->reloadStrategy === FileReloadStrategy::ON_CHANGE) { | ||
$registry = $this->getRegistry(); | ||
$hash = $mapperMetadata->getHash(); | ||
$shouldBuildMapper = !isset($registry[$className]) || $registry[$className] !== $hash || !file_exists($classPath); | ||
} | ||
|
||
if ($shouldBuildMapper) { | ||
$this->createGeneratedMapper($mapperMetadata); | ||
} | ||
|
||
require $classPath; | ||
require $classPath; | ||
} finally { | ||
$lock->release(); | ||
} | ||
} | ||
|
||
public function buildMappers(MetadataRegistry $registry): bool | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters