You are here

private function ProxyGenerator::generateSleepImpl in Plug 7

Generates implementation for the `__sleep` method of proxies.

Parameters

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

Return value

string

File

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

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 generateSleepImpl(ClassMetadata $class) {
  $hasParentSleep = $class
    ->getReflectionClass()
    ->hasMethod('__sleep');
  $inheritDoc = $hasParentSleep ? '{@inheritDoc}' : '';
  $sleepImpl = <<<EOT
    /**
     * {<span class="php-variable">$inheritDoc</span>}
     * @return array
     */
    public function __sleep()
    {

EOT;
  if ($hasParentSleep) {
    return $sleepImpl . <<<'EOT'
        $properties = array_merge(array('__isInitialized__'), parent::__sleep());

        if ($this->__isInitialized__) {
            $properties = array_diff($properties, array_keys($this->__getLazyProperties()));
        }

        return $properties;
    }
EOT;
  }
  $allProperties = array(
    '__isInitialized__',
  );

  /* @var $prop \ReflectionProperty */
  foreach ($class
    ->getReflectionClass()
    ->getProperties() as $prop) {
    if ($prop
      ->isStatic()) {
      continue;
    }
    $allProperties[] = $prop
      ->isPrivate() ? "\0" . $prop
      ->getDeclaringClass()
      ->getName() . "\0" . $prop
      ->getName() : $prop
      ->getName();
  }
  $lazyPublicProperties = array_keys($this
    ->getLazyLoadedPublicProperties($class));
  $protectedProperties = array_diff($allProperties, $lazyPublicProperties);
  foreach ($allProperties as &$property) {
    $property = var_export($property, true);
  }
  foreach ($protectedProperties as &$property) {
    $property = var_export($property, true);
  }
  $allProperties = implode(', ', $allProperties);
  $protectedProperties = implode(', ', $protectedProperties);
  return $sleepImpl . <<<EOT
        if (\$this->__isInitialized__) {
            return array({<span class="php-variable">$allProperties</span>});
        }

        return array({<span class="php-variable">$protectedProperties</span>});
    }
EOT;
}