Skip to content

Commit 40e1b20

Browse files
committed
improve lazy instantiator
1 parent e07041f commit 40e1b20

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

src/Symfony/Component/SerDes/Instantiator/LazyInstantiator.php

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,25 @@ public function __construct(
3939

4040
public function __invoke(\ReflectionClass $class, array $propertiesValues, array $context): object
4141
{
42-
if (!isset(self::$cache['lazy_class_name'][$className = $class->getName()])) {
43-
/** @var class-string $lazyClassName */
44-
$lazyClassName = sprintf('%sGhost', preg_replace('/\\\\/', '', $className));
45-
self::$cache['lazy_class_name'][$className] = $lazyClassName;
46-
}
42+
self::$cache['lazy_class_name'][$className = $class->getName()] ??= sprintf('%sGhost', preg_replace('/\\\\/', '', $className));
4743

48-
if (!isset(self::$lazyClassesLoaded[$className]) && !class_exists(self::$cache['lazy_class_name'][$className])) {
49-
if (!file_exists($path = sprintf('%s%s%s.php', $this->cacheDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className)))) {
50-
if (!file_exists($this->cacheDir)) {
51-
mkdir($this->cacheDir, recursive: true);
52-
}
44+
if (isset(self::$lazyClassesLoaded[$className]) && class_exists(self::$cache['lazy_class_name'][$className])) {
45+
return self::$cache['lazy_class_name'][$className]::createLazyGhost($propertiesValues);
46+
}
5347

54-
$lazyClassName = sprintf('%sGhost', preg_replace('/\\\\/', '', $className));
55-
file_put_contents($path, sprintf('class %s%s', $lazyClassName, ProxyHelper::generateLazyGhost($class)));
48+
if (!file_exists($path = sprintf('%s%s%s.php', $this->cacheDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className)))) {
49+
if (!file_exists($this->cacheDir)) {
50+
mkdir($this->cacheDir, recursive: true);
5651
}
5752

58-
eval(file_get_contents(sprintf('%s%s%s.php', $this->cacheDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className))));
59-
60-
self::$lazyClassesLoaded[$className] = true;
53+
$lazyClassName = sprintf('%sGhost', preg_replace('/\\\\/', '', $className));
54+
file_put_contents($path, sprintf('class %s%s', $lazyClassName, ProxyHelper::generateLazyGhost($class)));
6155
}
6256

63-
$lazyGhost = self::$cache['lazy_class_name'][$className]::createLazyGhost($propertiesValues);
57+
eval(file_get_contents(sprintf('%s%s%s.php', $this->cacheDir, \DIRECTORY_SEPARATOR, hash('xxh128', $className))));
58+
59+
self::$lazyClassesLoaded[$className] = true;
6460

65-
return $lazyGhost;
61+
return self::$cache['lazy_class_name'][$className]::createLazyGhost($propertiesValues);
6662
}
6763
}

0 commit comments

Comments
 (0)