You are here

private function ProxyGenerator::generateMagicGet in Plug 7

Generates the magic getter invoked when lazy loaded public properties are requested.

Parameters

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

Return value

string

File

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

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 generateMagicGet(ClassMetadata $class) {
  $lazyPublicProperties = array_keys($this
    ->getLazyLoadedPublicProperties($class));
  $reflectionClass = $class
    ->getReflectionClass();
  $hasParentGet = false;
  $returnReference = '';
  $inheritDoc = '';
  if ($reflectionClass
    ->hasMethod('__get')) {
    $hasParentGet = true;
    $inheritDoc = '{@inheritDoc}';
    if ($reflectionClass
      ->getMethod('__get')
      ->returnsReference()) {
      $returnReference = '& ';
    }
  }
  if (empty($lazyPublicProperties) && !$hasParentGet) {
    return '';
  }
  $magicGet = <<<EOT
    /**
     * {<span class="php-variable">$inheritDoc</span>}
     * @param string \$name
     */
    public function {<span class="php-variable">$returnReference</span>}__get(\$name)
    {

EOT;
  if (!empty($lazyPublicProperties)) {
    $magicGet .= <<<'EOT'
        if (array_key_exists($name, $this->__getLazyProperties())) {
            $this->__initializer__ && $this->__initializer__->__invoke($this, '__get', array($name));

            return $this->$name;
        }


EOT;
  }
  if ($hasParentGet) {
    $magicGet .= <<<'EOT'
        $this->__initializer__ && $this->__initializer__->__invoke($this, '__get', array($name));

        return parent::__get($name);

EOT;
  }
  else {
    $magicGet .= <<<'EOT'
        trigger_error(sprintf('Undefined property: %s::$%s', __CLASS__, $name), E_USER_NOTICE);

EOT;
  }
  $magicGet .= "    }";
  return $magicGet;
}