Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch proxies to LazyGhostTrait #2700

Open
wants to merge 5 commits into
base: 2.10.x
Choose a base branch
from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Nov 23, 2024

Q A
Type improvement
BC Break no
Fixed issues Replace #2692

Summary

Use the LazyGhostTrait from symfony/var-exporter to generate lazy proxy classes.

@GromNaN GromNaN force-pushed the proxy-var-exporter-2 branch from 9bdb39c to a81c08c Compare November 23, 2024 21:36
@GromNaN GromNaN changed the title Switch proxies back to LazyGhostTrait Switch proxies to LazyGhostTrait Nov 23, 2024
use function substr;

/** @internal */
class LazyGhostProxyClassNameResolver implements ClassNameResolver, ProxyClassNameResolver
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

* @template T of object
* @template-extends Proxy<T>
*/
interface InternalProxy extends Proxy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This interface is copied from Doctrine\ORM\Proxy\InternalProxy.

Comment on lines +589 to +608
$proxyManagerConfiguration = new ProxyManagerConfiguration();
$proxyManagerConfiguration->setProxiesTargetDir($this->getProxyDir());
$proxyManagerConfiguration->setProxiesNamespace($this->getProxyNamespace());

switch ($this->getAutoGenerateProxyClasses()) {
case self::AUTOGENERATE_FILE_NOT_EXISTS:
$proxyManagerConfiguration->setGeneratorStrategy(new FileWriterGeneratorStrategy(
new FileLocator($proxyManagerConfiguration->getProxiesTargetDir()),
));

break;
case self::AUTOGENERATE_EVAL:
$proxyManagerConfiguration->setGeneratorStrategy(new EvaluatingGeneratorStrategy());

break;
default:
throw new InvalidArgumentException('Invalid proxy generation strategy given - only AUTOGENERATE_FILE_NOT_EXISTS and AUTOGENERATE_EVAL are supported.');
}

return $proxyManagerConfiguration;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating ProxyManagerConfiguration on-demand as the class is not required when LaryGhostObjects are used.

*
* @internal
*/
final class Autoloader
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is copied from Doctrine\ORM\Proxy\Autoloader.

Comment on lines +188 to +199
return static function (InternalProxy $proxy, mixed $identifier) use ($persister, $classMetadata, $factory): void {
$original = $persister->load([$classMetadata->identifier => $identifier], $proxy);

if (! $original && ! $factory->lifecycleEventManager->documentNotFound($proxy, $identifier)) {
throw DocumentNotFoundException::documentNotFound($classMetadata->getName(), $identifier);
}

// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
if ($proxy instanceof NotifyPropertyChanged) {
$proxy->addPropertyChangedListener($factory->uow);
}
};
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initialization code copied from StaticProxyFactory.

*
* @template T of object
*/
public function getProxy(ClassMetadata $metadata, $identifier): GhostObjectInterface;
public function getProxy(ClassMetadata $metadata, $identifier): object;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interface is modified, but child classes can be more restrictive. This is not a BC break for implementers of the interface, but it can be for users of the interface.

Comment on lines +120 to +123
public static function isLazyObject(object $document): bool
{
return $document instanceof InternalProxy || $document instanceof LazyLoadingInterface;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most test changes are related to check interfaces to assert if the class is a lazy proxy object. I wonder if this method should be in UOW.

Later, it will use ReflectionClass::getLazyInitializer() to detect native lazy object.

Comment on lines +620 to +645

/**
* Generate proxy classes using Symfony VarExporter's LazyGhostTrait if true.
* Otherwise, use ProxyManager's LazyLoadingGhostFactory (deprecated)
*/
public function setUseLazyGhostObject(bool $flag): void
{
if ($flag === false) {
if (! class_exists(ProxyManagerConfiguration::class)) {
throw new LogicException('Package "friendsofphp/proxy-manager-lts" is required to disable LazyGhostObject.');
}

trigger_deprecation(
'doctrine/mongodb-odm',
'2.6',
'Using "friendsofphp/proxy-manager-lts" is deprecated. Use "symfony/var-exporter" LazyGhostObjects instead.',
);
}

$this->useLazyGhostObject = $flag;
}

public function isLazyGhostObjectEnabled(): bool
{
return $this->useLazyGhostObject ?? true;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we use the same method names as the ORM: isLazyGhostObjectEnabled and setLazyGhostObjectEnabled?

@GromNaN GromNaN requested review from alcaeus and malarzm November 23, 2024 22:32
Comment on lines +270 to +284
switch ($this->autoGenerate) {
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED:
if (file_exists($fileName) && filemtime($fileName) >= filemtime($class->getReflectionClass()->getFileName())) {
break;
}
// no break
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS:
if (file_exists($fileName)) {
break;
}
// no break
case Configuration::AUTOGENERATE_ALWAYS:
$this->generateProxyClass($class, $fileName, $proxyClassName);
break;
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be more intuitive this way:

Suggested change
switch ($this->autoGenerate) {
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED:
if (file_exists($fileName) && filemtime($fileName) >= filemtime($class->getReflectionClass()->getFileName())) {
break;
}
// no break
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS:
if (file_exists($fileName)) {
break;
}
// no break
case Configuration::AUTOGENERATE_ALWAYS:
$this->generateProxyClass($class, $fileName, $proxyClassName);
break;
}
if (
match ($this->autoGenerate) {
Configuration::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED => ! file_exists($fileName) || filemtime($fileName) < filemtime($class->getReflectionClass()->getFileName()),
Configuration::AUTOGENERATE_FILE_NOT_EXISTS => ! file_exists($fileName),
Configuration::AUTOGENERATE_ALWAYS => true,
Configuration::AUTOGENERATE_NEVER => false,
}
) {
$this->generateProxyClass($class, $fileName, $proxyClassName);
}


trigger_deprecation(
'doctrine/mongodb-odm',
'2.6',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'2.6',
'2.10',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant