3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Proxy;
22 use Doctrine\Common\Persistence\Mapping\ClassMetadata;
23 use Doctrine\Common\Persistence\Mapping\ClassMetadataFactory;
24 use Doctrine\Common\Proxy\Exception\InvalidArgumentException;
25 use Doctrine\Common\Proxy\Exception\OutOfBoundsException;
26 use Doctrine\Common\Util\ClassUtils;
29 * Abstract factory for proxy objects.
31 * @author Benjamin Eberlei <kontakt@beberlei.de>
33 abstract class AbstractProxyFactory
36 * Never autogenerate a proxy and rely that it was generated by some
37 * process before deployment.
41 const AUTOGENERATE_NEVER = 0;
44 * Always generates a new proxy in every request.
46 * This is only sane during development.
50 const AUTOGENERATE_ALWAYS = 1;
53 * Autogenerate the proxy class when the proxy file does not exist.
55 * This strategy causes a file exists call whenever any proxy is used the
56 * first time in a request.
60 const AUTOGENERATE_FILE_NOT_EXISTS = 2;
63 * Generate the proxy classes using eval().
65 * This strategy is only sane for development, and even then it gives me
66 * the creeps a little.
70 const AUTOGENERATE_EVAL = 3;
72 private const AUTOGENERATE_MODES = [
73 self::AUTOGENERATE_NEVER,
74 self::AUTOGENERATE_ALWAYS,
75 self::AUTOGENERATE_FILE_NOT_EXISTS,
76 self::AUTOGENERATE_EVAL,
80 * @var \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
82 private $metadataFactory;
85 * @var \Doctrine\Common\Proxy\ProxyGenerator the proxy generator responsible for creating the proxy classes/files.
87 private $proxyGenerator;
90 * @var int Whether to automatically (re)generate proxy classes.
92 private $autoGenerate;
95 * @var \Doctrine\Common\Proxy\ProxyDefinition[]
97 private $definitions = [];
100 * @param \Doctrine\Common\Proxy\ProxyGenerator $proxyGenerator
101 * @param \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory $metadataFactory
102 * @param bool|int $autoGenerate
104 * @throws \Doctrine\Common\Proxy\Exception\InvalidArgumentException When auto generate mode is not valid.
106 public function __construct(ProxyGenerator $proxyGenerator, ClassMetadataFactory $metadataFactory, $autoGenerate)
108 $this->proxyGenerator = $proxyGenerator;
109 $this->metadataFactory = $metadataFactory;
111 $this->autoGenerate = (int)$autoGenerate;
113 if ( ! in_array($this->autoGenerate, self::AUTOGENERATE_MODES, true)) {
114 throw InvalidArgumentException::invalidAutoGenerateMode($autoGenerate);
119 * Gets a reference proxy instance for the entity of the given type and identified by
120 * the given identifier.
122 * @param string $className
123 * @param array $identifier
125 * @return \Doctrine\Common\Proxy\Proxy
127 * @throws \Doctrine\Common\Proxy\Exception\OutOfBoundsException
129 public function getProxy($className, array $identifier)
131 $definition = isset($this->definitions[$className])
132 ? $this->definitions[$className]
133 : $this->getProxyDefinition($className);
134 $fqcn = $definition->proxyClassName;
135 $proxy = new $fqcn($definition->initializer, $definition->cloner);
137 foreach ($definition->identifierFields as $idField) {
138 if (! isset($identifier[$idField])) {
139 throw OutOfBoundsException::missingPrimaryKeyValue($className, $idField);
142 $definition->reflectionFields[$idField]->setValue($proxy, $identifier[$idField]);
149 * Generates proxy classes for all given classes.
151 * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata[] $classes The classes (ClassMetadata instances)
152 * for which to generate proxies.
153 * @param string $proxyDir The target directory of the proxy classes. If not specified, the
154 * directory configured on the Configuration of the EntityManager used
155 * by this factory is used.
156 * @return int Number of generated proxies.
158 public function generateProxyClasses(array $classes, $proxyDir = null)
162 foreach ($classes as $class) {
163 if ($this->skipClass($class)) {
167 $proxyFileName = $this->proxyGenerator->getProxyFileName($class->getName(), $proxyDir);
169 $this->proxyGenerator->generateProxyClass($class, $proxyFileName);
178 * Reset initialization/cloning logic for an un-initialized proxy
180 * @param \Doctrine\Common\Proxy\Proxy $proxy
182 * @return \Doctrine\Common\Proxy\Proxy
184 * @throws \Doctrine\Common\Proxy\Exception\InvalidArgumentException
186 public function resetUninitializedProxy(Proxy $proxy)
188 if ($proxy->__isInitialized()) {
189 throw InvalidArgumentException::unitializedProxyExpected($proxy);
192 $className = ClassUtils::getClass($proxy);
193 $definition = isset($this->definitions[$className])
194 ? $this->definitions[$className]
195 : $this->getProxyDefinition($className);
197 $proxy->__setInitializer($definition->initializer);
198 $proxy->__setCloner($definition->cloner);
204 * Get a proxy definition for the given class name.
206 * @param string $className
208 * @return ProxyDefinition
210 private function getProxyDefinition($className)
212 $classMetadata = $this->metadataFactory->getMetadataFor($className);
213 $className = $classMetadata->getName(); // aliases and case sensitivity
215 $this->definitions[$className] = $this->createProxyDefinition($className);
216 $proxyClassName = $this->definitions[$className]->proxyClassName;
218 if ( ! class_exists($proxyClassName, false)) {
219 $fileName = $this->proxyGenerator->getProxyFileName($className);
221 switch ($this->autoGenerate) {
222 case self::AUTOGENERATE_NEVER:
226 case self::AUTOGENERATE_FILE_NOT_EXISTS:
227 if ( ! file_exists($fileName)) {
228 $this->proxyGenerator->generateProxyClass($classMetadata, $fileName);
233 case self::AUTOGENERATE_ALWAYS:
234 $this->proxyGenerator->generateProxyClass($classMetadata, $fileName);
238 case self::AUTOGENERATE_EVAL:
239 $this->proxyGenerator->generateProxyClass($classMetadata, false);
244 return $this->definitions[$className];
248 * Determine if this class should be skipped during proxy generation.
250 * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $metadata
254 abstract protected function skipClass(ClassMetadata $metadata);
257 * @param string $className
259 * @return ProxyDefinition
261 abstract protected function createProxyDefinition($className);