private function generateWakeupImpl(ClassMetadata $class)
{
$unsetPublicProperties = [];
$hasWakeup = $class->getReflectionClass()->hasMethod('__wakeup');
foreach (array_keys($this->getLazyLoadedPublicProperties($class)) as $lazyPublicProperty) {
$unsetPublicProperties[] = '$this->' . $lazyPublicProperty;
}
$shortName = $this->generateProxyShortClassName($class);
$inheritDoc = $hasWakeup ? '{@inheritDoc}' : '';
$wakeupImpl = <<<EOT
/**
* {$inheritDoc}
*/
public function __wakeup()
{
if ( ! \$this->__isInitialized__) {
\$this->__initializer__ = function ({$shortName} \$proxy) {
\$proxy->__setInitializer(null);
\$proxy->__setCloner(null);
\$existingProperties = get_object_vars(\$proxy);
foreach (\$proxy->__getLazyProperties() as \$property => \$defaultValue) {
if ( ! array_key_exists(\$property, \$existingProperties)) {
\$proxy->\$property = \$defaultValue;
}
}
};
EOT;
if (!empty($unsetPublicProperties)) {
$wakeupImpl .= "\n unset(" . implode(', ', $unsetPublicProperties) . ");";
}
$wakeupImpl .= "\n }";
if ($hasWakeup) {
$wakeupImpl .= "\n parent::__wakeup();";
}
$wakeupImpl .= "\n }";
return $wakeupImpl;
}