You are here

public function ProxyGenerator::generateProxyClass in Plug 7

Generates a proxy class file.

Parameters

\Doctrine\Common\Persistence\Mapping\ClassMetadata $class Metadata for the original class.:

string|bool $fileName Filename (full path) for the generated class. If none is given, eval() is used.:

Throws

UnexpectedValueException

File

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

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

public function generateProxyClass(ClassMetadata $class, $fileName = false) {
  preg_match_all('(<([a-zA-Z]+)>)', $this->proxyClassTemplate, $placeholderMatches);
  $placeholderMatches = array_combine($placeholderMatches[0], $placeholderMatches[1]);
  $placeholders = array();
  foreach ($placeholderMatches as $placeholder => $name) {
    $placeholders[$placeholder] = isset($this->placeholders[$name]) ? $this->placeholders[$name] : array(
      $this,
      'generate' . $name,
    );
  }
  foreach ($placeholders as &$placeholder) {
    if (is_callable($placeholder)) {
      $placeholder = call_user_func($placeholder, $class);
    }
  }
  $proxyCode = strtr($this->proxyClassTemplate, $placeholders);
  if (!$fileName) {
    $proxyClassName = $this
      ->generateNamespace($class) . '\\' . $this
      ->generateProxyShortClassName($class);
    if (!class_exists($proxyClassName)) {
      eval(substr($proxyCode, 5));
    }
    return;
  }
  $parentDirectory = dirname($fileName);
  if (!is_dir($parentDirectory) && false === @mkdir($parentDirectory, 0775, true)) {
    throw UnexpectedValueException::proxyDirectoryNotWritable($this->proxyDirectory);
  }
  if (!is_writable($parentDirectory)) {
    throw UnexpectedValueException::proxyDirectoryNotWritable($this->proxyDirectory);
  }
  $tmpFileName = $fileName . '.' . uniqid('', true);
  file_put_contents($tmpFileName, $proxyCode);
  rename($tmpFileName, $fileName);
}