You are here

private function ProxyGenerator::generateWakeupImpl in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php \Doctrine\Common\Proxy\ProxyGenerator::generateWakeupImpl()

Generates implementation for the `__wakeup` method of proxies.

Parameters

\Doctrine\Common\Persistence\Mapping\ClassMetadata $class:

Return value

string

File

vendor/doctrine/common/lib/Doctrine/Common/Proxy/ProxyGenerator.php, line 663

Class

ProxyGenerator
This factory is used to generate proxy classes. It builds proxies from given parameters, a template and class metadata.

Namespace

Doctrine\Common\Proxy

Code

private function generateWakeupImpl(ClassMetadata $class) {
  $unsetPublicProperties = array();
  $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
    /**
     * {<span class="php-variable">$inheritDoc</span>}
     */
    public function __wakeup()
    {
        if ( ! \$this->__isInitialized__) {
            \$this->__initializer__ = function ({<span class="php-variable">$shortName</span>} \$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;
}