Doctrine\Common\Proxy\ProxyGenerator::generateProxyClass PHP Method

generateProxyClass() public method

Generates a proxy class file.
public generateProxyClass ( Doctrine\Common\Persistence\Mapping\ClassMetadata $class, string | boolean $fileName = false )
$class Doctrine\Common\Persistence\Mapping\ClassMetadata Metadata for the original class.
$fileName string | boolean Filename (full path) for the generated class. If none is given, eval() is used.
    public function generateProxyClass(ClassMetadata $class, $fileName = false)
    {
        preg_match_all('(<([a-zA-Z]+)>)', $this->proxyClassTemplate, $placeholderMatches);
        $placeholderMatches = array_combine($placeholderMatches[0], $placeholderMatches[1]);
        $placeholders = [];
        foreach ($placeholderMatches as $placeholder => $name) {
            $placeholders[$placeholder] = isset($this->placeholders[$name]) ? $this->placeholders[$name] : [$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);
        @chmod($tmpFileName, 0664);
        rename($tmpFileName, $fileName);
    }

Usage Example

 /**
  * @param $className
  *
  * @return string
  */
 private function generateProxyClass($className)
 {
     $proxyClassName = 'Doctrine\\Tests\\Common\\Proxy\\MagicMethodProxy\\__CG__\\' . $className;
     if (class_exists($proxyClassName, false)) {
         return $proxyClassName;
     }
     $metadata = $this->getMock('Doctrine\\Common\\Persistence\\Mapping\\ClassMetadata');
     $metadata->expects($this->any())->method('getName')->will($this->returnValue($className));
     $metadata->expects($this->any())->method('getIdentifier')->will($this->returnValue(array('id')));
     $metadata->expects($this->any())->method('getReflectionClass')->will($this->returnValue(new ReflectionClass($className)));
     $metadata->expects($this->any())->method('isIdentifier')->will($this->returnCallback(function ($fieldName) {
         return 'id' === $fieldName;
     }));
     $metadata->expects($this->any())->method('hasField')->will($this->returnCallback(function ($fieldName) {
         return in_array($fieldName, array('id', 'publicField'));
     }));
     $metadata->expects($this->any())->method('hasAssociation')->will($this->returnValue(false));
     $metadata->expects($this->any())->method('getFieldNames')->will($this->returnValue(array('id', 'publicField')));
     $metadata->expects($this->any())->method('getIdentifierFieldNames')->will($this->returnValue(array('id')));
     $metadata->expects($this->any())->method('getAssociationNames')->will($this->returnValue(array()));
     $metadata->expects($this->any())->method('getTypeOfField')->will($this->returnValue('string'));
     $this->proxyGenerator->generateProxyClass($metadata, $this->proxyGenerator->getProxyFileName($className));
     require_once $this->proxyGenerator->getProxyFileName($className);
     return $proxyClassName;
 }
All Usage Examples Of Doctrine\Common\Proxy\ProxyGenerator::generateProxyClass