/** * Initializes a new instance of the <tt>ProxyFactory</tt> class that is * connected to the given <tt>EntityManager</tt>. * * @param EntityManager $em The EntityManager the new factory works for. * @param string $proxyDir The directory to use for the proxy classes. It must exist. * @param string $proxyNs The namespace to use for the proxy classes. * @param boolean $autoGenerate Whether to automatically generate proxy classes. */ public function __construct(EntityManager $em, $proxyDir, $proxyNs, $autoGenerate = false) { if (!$proxyDir) { throw ProxyException::proxyDirectoryRequired(); } if (!$proxyNs) { throw ProxyException::proxyNamespaceRequired(); } $this->_em = $em; $this->_proxyDir = $proxyDir; $this->_autoGenerate = $autoGenerate; $this->_proxyNamespace = $proxyNs; }
/** * Generates a proxy class file. * * @param ClassMetadata $class Metadata for the original class * @param string $fileName Filename (full path) for the generated class * @param string $file The proxy class template data */ private function _generateProxyClass(ClassMetadata $class, $fileName, $file) { $methods = $this->_generateMethods($class); $sleepImpl = $this->_generateSleep($class); $cloneImpl = $class->reflClass->hasMethod('__clone') ? 'parent::__clone();' : ''; // hasMethod() checks case-insensitive $placeholders = array('<namespace>', '<proxyClassName>', '<className>', '<methods>', '<sleepImpl>', '<cloneImpl>'); $className = ltrim($class->name, '\\'); $proxyClassName = ClassUtils::generateProxyClassName($class->name, $this->_proxyNamespace); $parts = explode('\\', strrev($proxyClassName), 2); $proxyClassNamespace = strrev($parts[1]); $proxyClassName = strrev($parts[0]); $replacements = array($proxyClassNamespace, $proxyClassName, $className, $methods, $sleepImpl, $cloneImpl); $file = str_replace($placeholders, $replacements, $file); $parentDirectory = dirname($fileName); if (!is_dir($parentDirectory)) { if (false === @mkdir($parentDirectory, 0775, true)) { throw ProxyException::proxyDirectoryNotWritable(); } } else { if (!is_writable($parentDirectory)) { throw ProxyException::proxyDirectoryNotWritable(); } } $tmpFileName = $fileName . '.' . uniqid("", true); file_put_contents($tmpFileName, $file); rename($tmpFileName, $fileName); }