You are here

private function ProxyGenerator::generateMethods in Plug 7

Generates decorated methods by picking those available in the parent class.

Parameters

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

Return value

string

File

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

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 generateMethods(ClassMetadata $class) {
  $methods = '';
  $methodNames = array();
  $reflectionMethods = $class
    ->getReflectionClass()
    ->getMethods(\ReflectionMethod::IS_PUBLIC);
  $skippedMethods = array(
    '__sleep' => true,
    '__clone' => true,
    '__wakeup' => true,
    '__get' => true,
    '__set' => true,
    '__isset' => true,
  );
  foreach ($reflectionMethods as $method) {
    $name = $method
      ->getName();
    if ($method
      ->isConstructor() || isset($skippedMethods[strtolower($name)]) || isset($methodNames[$name]) || $method
      ->isFinal() || $method
      ->isStatic() || !$method
      ->isPublic()) {
      continue;
    }
    $methodNames[$name] = true;
    $methods .= "\n    /**\n" . "     * {@inheritDoc}\n" . "     */\n" . '    public function ';
    if ($method
      ->returnsReference()) {
      $methods .= '&';
    }
    $methods .= $name . '(' . $this
      ->buildParametersString($class, $method, $method
      ->getParameters()) . ')';
    $methods .= "\n" . '    {' . "\n";
    if ($this
      ->isShortIdentifierGetter($method, $class)) {
      $identifier = lcfirst(substr($name, 3));
      $fieldType = $class
        ->getTypeOfField($identifier);
      $cast = in_array($fieldType, array(
        'integer',
        'smallint',
      )) ? '(int) ' : '';
      $methods .= '        if ($this->__isInitialized__ === false) {' . "\n";
      $methods .= '            return ' . $cast . ' parent::' . $method
        ->getName() . "();\n";
      $methods .= '        }' . "\n\n";
    }
    $invokeParamsString = implode(', ', $this
      ->getParameterNamesForInvoke($method
      ->getParameters()));
    $callParamsString = implode(', ', $this
      ->getParameterNamesForParentCall($method
      ->getParameters()));
    $methods .= "\n        \$this->__initializer__ " . "&& \$this->__initializer__->__invoke(\$this, " . var_export($name, true) . ", array(" . $invokeParamsString . "));" . "\n\n        return parent::" . $name . '(' . $callParamsString . ');' . "\n" . '    }' . "\n";
  }
  return $methods;
}