Exception
2 years ago
AbstractProxyFactory.php
2 years ago
Autoloader.php
2 years ago
Proxy.php
2 years ago
ProxyDefinition.php
2 years ago
ProxyGenerator.php
9 months ago
index.php
2 years ago
AbstractProxyFactory.php
110 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Doctrine\Common\Proxy; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Doctrine\Common\Proxy\Exception\InvalidArgumentException; |
| 5 | use MailPoetVendor\Doctrine\Common\Proxy\Exception\OutOfBoundsException; |
| 6 | use MailPoetVendor\Doctrine\Common\Util\ClassUtils; |
| 7 | use MailPoetVendor\Doctrine\Persistence\Mapping\ClassMetadata; |
| 8 | use MailPoetVendor\Doctrine\Persistence\Mapping\ClassMetadataFactory; |
| 9 | use function class_exists; |
| 10 | use function file_exists; |
| 11 | use function filemtime; |
| 12 | use function in_array; |
| 13 | abstract class AbstractProxyFactory |
| 14 | { |
| 15 | public const AUTOGENERATE_NEVER = 0; |
| 16 | public const AUTOGENERATE_ALWAYS = 1; |
| 17 | public const AUTOGENERATE_FILE_NOT_EXISTS = 2; |
| 18 | public const AUTOGENERATE_EVAL = 3; |
| 19 | public const AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED = 4; |
| 20 | private const AUTOGENERATE_MODES = [self::AUTOGENERATE_NEVER, self::AUTOGENERATE_ALWAYS, self::AUTOGENERATE_FILE_NOT_EXISTS, self::AUTOGENERATE_EVAL, self::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED]; |
| 21 | private $metadataFactory; |
| 22 | private $proxyGenerator; |
| 23 | private $autoGenerate; |
| 24 | private $definitions = []; |
| 25 | public function __construct(ProxyGenerator $proxyGenerator, ClassMetadataFactory $metadataFactory, $autoGenerate) |
| 26 | { |
| 27 | $this->proxyGenerator = $proxyGenerator; |
| 28 | $this->metadataFactory = $metadataFactory; |
| 29 | $this->autoGenerate = (int) $autoGenerate; |
| 30 | if (!in_array($this->autoGenerate, self::AUTOGENERATE_MODES, \true)) { |
| 31 | throw InvalidArgumentException::invalidAutoGenerateMode($autoGenerate); |
| 32 | } |
| 33 | } |
| 34 | public function getProxy($className, array $identifier) |
| 35 | { |
| 36 | $definition = $this->definitions[$className] ?? $this->getProxyDefinition($className); |
| 37 | $fqcn = $definition->proxyClassName; |
| 38 | $proxy = new $fqcn($definition->initializer, $definition->cloner); |
| 39 | foreach ($definition->identifierFields as $idField) { |
| 40 | if (!isset($identifier[$idField])) { |
| 41 | throw OutOfBoundsException::missingPrimaryKeyValue($className, $idField); |
| 42 | } |
| 43 | $definition->reflectionFields[$idField]->setValue($proxy, $identifier[$idField]); |
| 44 | } |
| 45 | return $proxy; |
| 46 | } |
| 47 | public function generateProxyClasses(array $classes, $proxyDir = null) |
| 48 | { |
| 49 | $generated = 0; |
| 50 | foreach ($classes as $class) { |
| 51 | if ($this->skipClass($class)) { |
| 52 | continue; |
| 53 | } |
| 54 | $proxyFileName = $this->proxyGenerator->getProxyFileName($class->getName(), $proxyDir); |
| 55 | $this->proxyGenerator->generateProxyClass($class, $proxyFileName); |
| 56 | $generated += 1; |
| 57 | } |
| 58 | return $generated; |
| 59 | } |
| 60 | public function resetUninitializedProxy(Proxy $proxy) |
| 61 | { |
| 62 | if ($proxy->__isInitialized()) { |
| 63 | throw InvalidArgumentException::unitializedProxyExpected($proxy); |
| 64 | } |
| 65 | $className = ClassUtils::getClass($proxy); |
| 66 | $definition = $this->definitions[$className] ?? $this->getProxyDefinition($className); |
| 67 | $proxy->__setInitializer($definition->initializer); |
| 68 | $proxy->__setCloner($definition->cloner); |
| 69 | return $proxy; |
| 70 | } |
| 71 | private function getProxyDefinition($className) |
| 72 | { |
| 73 | $classMetadata = $this->metadataFactory->getMetadataFor($className); |
| 74 | $className = $classMetadata->getName(); |
| 75 | // aliases and case sensitivity |
| 76 | $this->definitions[$className] = $this->createProxyDefinition($className); |
| 77 | $proxyClassName = $this->definitions[$className]->proxyClassName; |
| 78 | if (!class_exists($proxyClassName, \false)) { |
| 79 | $fileName = $this->proxyGenerator->getProxyFileName($className); |
| 80 | switch ($this->autoGenerate) { |
| 81 | case self::AUTOGENERATE_NEVER: |
| 82 | require $fileName; |
| 83 | break; |
| 84 | case self::AUTOGENERATE_FILE_NOT_EXISTS: |
| 85 | if (!file_exists($fileName)) { |
| 86 | $this->proxyGenerator->generateProxyClass($classMetadata, $fileName); |
| 87 | } |
| 88 | require $fileName; |
| 89 | break; |
| 90 | case self::AUTOGENERATE_ALWAYS: |
| 91 | $this->proxyGenerator->generateProxyClass($classMetadata, $fileName); |
| 92 | require $fileName; |
| 93 | break; |
| 94 | case self::AUTOGENERATE_EVAL: |
| 95 | $this->proxyGenerator->generateProxyClass($classMetadata, \false); |
| 96 | break; |
| 97 | case self::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED: |
| 98 | if (!file_exists($fileName) || filemtime($fileName) < filemtime($classMetadata->getReflectionClass()->getFileName())) { |
| 99 | $this->proxyGenerator->generateProxyClass($classMetadata, $fileName); |
| 100 | } |
| 101 | require $fileName; |
| 102 | break; |
| 103 | } |
| 104 | } |
| 105 | return $this->definitions[$className]; |
| 106 | } |
| 107 | protected abstract function skipClass(ClassMetadata $metadata); |
| 108 | protected abstract function createProxyDefinition($className); |
| 109 | } |
| 110 |