-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[VarExporter] Add trait to help implement lazy loading ghost objects #46751
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarExporter\Internal; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
enum EmptyScope | ||
{ | ||
} |
126 changes: 126 additions & 0 deletions
126
src/Symfony/Component/VarExporter/Internal/GhostObjectRegistry.php
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,126 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarExporter\Internal; | ||
|
||
/** | ||
* Stores the state of lazy ghost objects and caches related reflection information. | ||
* | ||
* As a micro-optimization, this class uses no type declarations. | ||
* | ||
* @internal | ||
*/ | ||
class GhostObjectRegistry | ||
{ | ||
/** | ||
* @var array<int, GhostObjectState> | ||
*/ | ||
public static $states = []; | ||
|
||
/** | ||
* @var array<class-string, \ReflectionClass> | ||
*/ | ||
public static $classReflectors = []; | ||
|
||
/** | ||
* @var array<class-string, array<string, mixed>> | ||
*/ | ||
public static $defaultProperties = []; | ||
|
||
/** | ||
* @var array<class-string, list<\Closure>> | ||
*/ | ||
public static $classResetters = []; | ||
|
||
/** | ||
* @var array<class-string, array{get: \Closure, set: \Closure, isset: \Closure, unset: \Closure}> | ||
*/ | ||
public static $classAccessors = []; | ||
|
||
/** | ||
* @var array<class-string, array{get: int, set: bool, isset: bool, unset: bool, clone: bool, serialize: bool, sleep: bool, destruct: bool}> | ||
*/ | ||
public static $parentMethods = []; | ||
|
||
public static function getClassResetters($class) | ||
{ | ||
$classProperties = []; | ||
$propertyScopes = Hydrator::$propertyScopes[$class] ??= Hydrator::getPropertyScopes($class); | ||
|
||
foreach ($propertyScopes as $key => [$scope, $name, $readonlyScope]) { | ||
if ('lazyGhostObjectId' !== $name && null !== ($propertyScopes["\0$scope\0$name"] ?? $propertyScopes["\0*\0$name"] ?? $readonlyScope)) { | ||
$classProperties[$readonlyScope ?? $scope][$name] = $key; | ||
} | ||
} | ||
|
||
$resetters = []; | ||
foreach ($classProperties as $scope => $properties) { | ||
$resetters[] = \Closure::bind(static function ($instance, $skippedProperties = []) use ($properties) { | ||
foreach ($properties as $name => $key) { | ||
if (!\array_key_exists($key, $skippedProperties)) { | ||
unset($instance->$name); | ||
} | ||
} | ||
}, null, $scope); | ||
} | ||
|
||
$resetters[] = static function ($instance, $skippedProperties = []) { | ||
foreach ((array) $instance as $name => $value) { | ||
if ("\0" !== ($name[0] ?? '') && !\array_key_exists($name, $skippedProperties)) { | ||
unset($instance->$name); | ||
} | ||
} | ||
}; | ||
|
||
return $resetters; | ||
} | ||
|
||
public static function getClassAccessors($class) | ||
{ | ||
return \Closure::bind(static function () { | ||
return [ | ||
'get' => static function &($instance, $name, $readonly) { | ||
if (!$readonly) { | ||
return $instance->$name; | ||
} | ||
$value = $instance->$name; | ||
|
||
return $value; | ||
}, | ||
'set' => static function ($instance, $name, $value) { | ||
$instance->$name = $value; | ||
}, | ||
'isset' => static function ($instance, $name) { | ||
return isset($instance->$name); | ||
}, | ||
'unset' => static function ($instance, $name) { | ||
unset($instance->$name); | ||
}, | ||
]; | ||
}, null, $class)(); | ||
} | ||
|
||
public static function getParentMethods($class) | ||
{ | ||
$parent = get_parent_class($class); | ||
|
||
return [ | ||
'get' => $parent && method_exists($parent, '__get') ? ((new \ReflectionMethod($parent, '__get'))->returnsReference() ? 2 : 1) : 0, | ||
'set' => $parent && method_exists($parent, '__set'), | ||
'isset' => $parent && method_exists($parent, '__isset'), | ||
'unset' => $parent && method_exists($parent, '__unset'), | ||
'clone' => $parent && method_exists($parent, '__clone'), | ||
'serialize' => $parent && method_exists($parent, '__serialize'), | ||
'sleep' => $parent && method_exists($parent, '__sleep'), | ||
'destruct' => $parent && method_exists($parent, '__destruct'), | ||
]; | ||
} | ||
} |
87 changes: 87 additions & 0 deletions
87
src/Symfony/Component/VarExporter/Internal/GhostObjectState.php
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarExporter\Internal; | ||
|
||
use Symfony\Component\VarExporter\Hydrator; | ||
|
||
/** | ||
* Keeps the state of lazy ghost objects. | ||
* | ||
* As a micro-optimization, this class uses no type declarations. | ||
* | ||
* @internal | ||
*/ | ||
class GhostObjectState | ||
{ | ||
public const STATUS_INITIALIZED_PARTIAL = 1; | ||
public const STATUS_UNINITIALIZED_FULL = 2; | ||
public const STATUS_INITIALIZED_FULL = 3; | ||
|
||
public \Closure $initializer; | ||
|
||
/** | ||
* @var array<class-string|'*', array<string, true>> | ||
*/ | ||
public $preInitUnsetProperties; | ||
|
||
/** | ||
* @var array<string, true> | ||
*/ | ||
public $preInitSetProperties = []; | ||
|
||
/** | ||
* @var array<class-string|'*', array<string, true>> | ||
*/ | ||
public $unsetProperties = []; | ||
|
||
/** | ||
* One of self::STATUS_*. | ||
* | ||
* @var int | ||
*/ | ||
public $status; | ||
|
||
/** | ||
* @return bool Returns true when fully-initializing, false when partial-initializing | ||
*/ | ||
public function initialize($instance, $propertyName, $propertyScope) | ||
{ | ||
if (!$this->status) { | ||
$this->status = 1 < (new \ReflectionFunction($this->initializer))->getNumberOfRequiredParameters() ? self::STATUS_INITIALIZED_PARTIAL : self::STATUS_UNINITIALIZED_FULL; | ||
$this->preInitUnsetProperties ??= $this->unsetProperties; | ||
} | ||
|
||
if (self::STATUS_INITIALIZED_FULL === $this->status) { | ||
return true; | ||
} | ||
|
||
if (self::STATUS_UNINITIALIZED_FULL === $this->status) { | ||
if ($defaultProperties = array_diff_key(GhostObjectRegistry::$defaultProperties[\get_class($instance)], (array) $instance)) { | ||
Hydrator::hydrate($instance, $defaultProperties); | ||
} | ||
|
||
$this->status = self::STATUS_INITIALIZED_FULL; | ||
($this->initializer)($instance); | ||
|
||
return true; | ||
} | ||
|
||
$value = ($this->initializer)(...[$instance, $propertyName, $propertyScope]); | ||
|
||
$propertyScope ??= \get_class($instance); | ||
$accessor = GhostObjectRegistry::$classAccessors[$propertyScope] ??= GhostObjectRegistry::getClassAccessors($propertyScope); | ||
|
||
$accessor['set']($instance, $propertyName, $value); | ||
|
||
return false; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/Symfony/Component/VarExporter/LazyGhostObjectInterface.php
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\VarExporter; | ||
|
||
interface LazyGhostObjectInterface | ||
{ | ||
/** | ||
* Forces initialization of a lazy ghost object. | ||
*/ | ||
public function initializeLazyGhostObject(): void; | ||
|
||
/** | ||
* @return bool Returns false when the object cannot be reset, ie when it's not a ghost object | ||
*/ | ||
public function resetLazyGhostObject(): bool; | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.