-
-
Notifications
You must be signed in to change notification settings - Fork 505
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
GromNaN
wants to merge
5
commits into
doctrine:2.10.x
Choose a base branch
from
GromNaN:proxy-var-exporter-2
base: 2.10.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2e48d18
Generate proxy classes using symfony/var-exporter
GromNaN 1ef6225
Leverage UOW::initializeObject in tests
GromNaN 8742c51
Trigger lifecycleEventManager from lazy ghost object
GromNaN a81c08c
Run the full test suite with proxy manager
GromNaN e69cd31
Fix assert not lazy object
GromNaN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -24,6 +24,7 @@ | |||||
use Doctrine\Persistence\Mapping\Driver\MappingDriver; | ||||||
use Doctrine\Persistence\ObjectRepository; | ||||||
use InvalidArgumentException; | ||||||
use LogicException; | ||||||
use MongoDB\Driver\WriteConcern; | ||||||
use ProxyManager\Configuration as ProxyManagerConfiguration; | ||||||
use ProxyManager\Factory\LazyLoadingGhostFactory; | ||||||
|
@@ -33,6 +34,7 @@ | |||||
use ReflectionClass; | ||||||
|
||||||
use function array_key_exists; | ||||||
use function class_exists; | ||||||
use function interface_exists; | ||||||
use function trigger_deprecation; | ||||||
use function trim; | ||||||
|
@@ -82,12 +84,23 @@ class Configuration | |||||
*/ | ||||||
public const AUTOGENERATE_EVAL = 3; | ||||||
|
||||||
/** | ||||||
* Autogenerate the proxy class when the proxy file does not exist or | ||||||
* when the proxied file changed. | ||||||
* | ||||||
* This strategy causes a file_exists() call whenever any proxy is used the | ||||||
* first time in a request. When the proxied file is changed, the proxy will | ||||||
* be updated. | ||||||
*/ | ||||||
public const AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED = 4; | ||||||
|
||||||
/** | ||||||
* Array of attributes for this configuration instance. | ||||||
* | ||||||
* @phpstan-var array{ | ||||||
* autoGenerateHydratorClasses?: self::AUTOGENERATE_*, | ||||||
* autoGeneratePersistentCollectionClasses?: self::AUTOGENERATE_*, | ||||||
* autoGenerateProxyClasses?: self::AUTOGENERATE_*, | ||||||
* classMetadataFactoryName?: class-string<ClassMetadataFactoryInterface>, | ||||||
* defaultCommitOptions?: CommitOptions, | ||||||
* defaultDocumentRepositoryClassName?: class-string<ObjectRepository<object>>, | ||||||
|
@@ -106,24 +119,18 @@ class Configuration | |||||
* persistentCollectionGenerator?: PersistentCollectionGenerator, | ||||||
* persistentCollectionDir?: string, | ||||||
* persistentCollectionNamespace?: string, | ||||||
* proxyDir?: string, | ||||||
* proxyNamespace?: string, | ||||||
* repositoryFactory?: RepositoryFactory | ||||||
* } | ||||||
*/ | ||||||
private array $attributes = []; | ||||||
|
||||||
private ?CacheItemPoolInterface $metadataCache = null; | ||||||
|
||||||
private ProxyManagerConfiguration $proxyManagerConfiguration; | ||||||
|
||||||
private int $autoGenerateProxyClasses = self::AUTOGENERATE_EVAL; | ||||||
|
||||||
private bool $useTransactionalFlush = false; | ||||||
|
||||||
public function __construct() | ||||||
{ | ||||||
$this->proxyManagerConfiguration = new ProxyManagerConfiguration(); | ||||||
$this->setAutoGenerateProxyClasses(self::AUTOGENERATE_FILE_NOT_EXISTS); | ||||||
} | ||||||
private bool $useLazyGhostObject = true; | ||||||
|
||||||
/** | ||||||
* Adds a namespace under a certain alias. | ||||||
|
@@ -248,68 +255,49 @@ public function setMetadataCache(CacheItemPoolInterface $cache): void | |||||
*/ | ||||||
public function setProxyDir(string $dir): void | ||||||
{ | ||||||
$this->getProxyManagerConfiguration()->setProxiesTargetDir($dir); | ||||||
|
||||||
// Recreate proxy generator to ensure its path was updated | ||||||
if ($this->autoGenerateProxyClasses !== self::AUTOGENERATE_FILE_NOT_EXISTS) { | ||||||
return; | ||||||
} | ||||||
|
||||||
$this->setAutoGenerateProxyClasses($this->autoGenerateProxyClasses); | ||||||
$this->attributes['proxyDir'] = $dir; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets the directory where Doctrine generates any necessary proxy class files. | ||||||
*/ | ||||||
public function getProxyDir(): ?string | ||||||
{ | ||||||
return $this->getProxyManagerConfiguration()->getProxiesTargetDir(); | ||||||
return $this->attributes['proxyDir'] ?? null; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Gets an int flag that indicates whether proxy classes should always be regenerated | ||||||
* during each script execution. | ||||||
* | ||||||
* @return self::AUTOGENERATE_* | ||||||
*/ | ||||||
public function getAutoGenerateProxyClasses(): int | ||||||
{ | ||||||
return $this->autoGenerateProxyClasses; | ||||||
return $this->attributes['autoGenerateProxyClasses'] ?? self::AUTOGENERATE_FILE_NOT_EXISTS; | ||||||
} | ||||||
|
||||||
/** | ||||||
* Sets an int flag that indicates whether proxy classes should always be regenerated | ||||||
* during each script execution. | ||||||
* | ||||||
* @param self::AUTOGENERATE_* $mode | ||||||
* | ||||||
* @throws InvalidArgumentException If an invalid mode was given. | ||||||
*/ | ||||||
public function setAutoGenerateProxyClasses(int $mode): void | ||||||
{ | ||||||
$this->autoGenerateProxyClasses = $mode; | ||||||
$proxyManagerConfig = $this->getProxyManagerConfiguration(); | ||||||
|
||||||
switch ($mode) { | ||||||
case self::AUTOGENERATE_FILE_NOT_EXISTS: | ||||||
$proxyManagerConfig->setGeneratorStrategy(new FileWriterGeneratorStrategy( | ||||||
new FileLocator($proxyManagerConfig->getProxiesTargetDir()), | ||||||
)); | ||||||
|
||||||
break; | ||||||
case self::AUTOGENERATE_EVAL: | ||||||
$proxyManagerConfig->setGeneratorStrategy(new EvaluatingGeneratorStrategy()); | ||||||
|
||||||
break; | ||||||
default: | ||||||
throw new InvalidArgumentException('Invalid proxy generation strategy given - only AUTOGENERATE_FILE_NOT_EXISTS and AUTOGENERATE_EVAL are supported.'); | ||||||
} | ||||||
$this->attributes['autoGenerateProxyClasses'] = $mode; | ||||||
} | ||||||
|
||||||
public function getProxyNamespace(): ?string | ||||||
{ | ||||||
return $this->getProxyManagerConfiguration()->getProxiesNamespace(); | ||||||
return $this->attributes['proxyNamespace'] ?? null; | ||||||
} | ||||||
|
||||||
public function setProxyNamespace(string $ns): void | ||||||
{ | ||||||
$this->getProxyManagerConfiguration()->setProxiesNamespace($ns); | ||||||
$this->attributes['proxyNamespace'] = $ns; | ||||||
} | ||||||
|
||||||
public function setHydratorDir(string $dir): void | ||||||
|
@@ -589,14 +577,35 @@ public function getPersistentCollectionGenerator(): PersistentCollectionGenerato | |||||
return $this->attributes['persistentCollectionGenerator']; | ||||||
} | ||||||
|
||||||
/** @deprecated */ | ||||||
public function buildGhostObjectFactory(): LazyLoadingGhostFactory | ||||||
{ | ||||||
return new LazyLoadingGhostFactory(clone $this->getProxyManagerConfiguration()); | ||||||
return new LazyLoadingGhostFactory($this->getProxyManagerConfiguration()); | ||||||
} | ||||||
|
||||||
/** @deprecated */ | ||||||
public function getProxyManagerConfiguration(): ProxyManagerConfiguration | ||||||
{ | ||||||
return $this->proxyManagerConfiguration; | ||||||
$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; | ||||||
} | ||||||
|
||||||
public function setUseTransactionalFlush(bool $useTransactionalFlush): void | ||||||
|
@@ -608,6 +617,32 @@ public function isTransactionalFlushEnabled(): bool | |||||
{ | ||||||
return $this->useTransactionalFlush; | ||||||
} | ||||||
|
||||||
/** | ||||||
* 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', | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'Using "friendsofphp/proxy-manager-lts" is deprecated. Use "symfony/var-exporter" LazyGhostObjects instead.', | ||||||
); | ||||||
} | ||||||
|
||||||
$this->useLazyGhostObject = $flag; | ||||||
} | ||||||
|
||||||
public function isLazyGhostObjectEnabled(): bool | ||||||
{ | ||||||
return $this->useLazyGhostObject ?? true; | ||||||
} | ||||||
Comment on lines
+620
to
+645
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use the same method names as the ORM: |
||||||
} | ||||||
|
||||||
interface_exists(MappingDriver::class); |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.