You are here

private function ProxyGenerator::generateWakeupImpl in Plug 7

Generates implementation for the `__wakeup` method of proxies.

Parameters

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

Return value

string

File

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

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;
}