Skip to content

Commit

Permalink
Remove duplicate constants
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed Oct 19, 2024
1 parent f8f5956 commit 847dac8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 50 deletions.
1 change: 1 addition & 0 deletions lib/Doctrine/ODM/MongoDB/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Configuration
* @psalm-var array{
* autoGenerateHydratorClasses?: self::AUTOGENERATE_*,
* autoGeneratePersistentCollectionClasses?: self::AUTOGENERATE_*,
* autoGenerateProxyClasses?: self::AUTOGENERATE_*,
* classMetadataFactoryName?: class-string<ClassMetadataFactoryInterface>,
* defaultCommitOptions?: CommitOptions,
* defaultDocumentRepositoryClassName?: class-string<ObjectRepository<object>>,
Expand Down
62 changes: 12 additions & 50 deletions lib/Doctrine/ODM/MongoDB/Proxy/ProxyFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\ODM\MongoDB\Proxy;

use Closure;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\DocumentNotFoundException;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
Expand Down Expand Up @@ -51,45 +52,6 @@
*/
class ProxyFactory
{
/**
* Never autogenerate a proxy and rely that it was generated by some
* process before deployment.
*/
public const AUTOGENERATE_NEVER = 0;

/**
* Always generates a new proxy in every request.
*
* This is only sane during development.
*/
public const AUTOGENERATE_ALWAYS = 1;

/**
* Autogenerate the proxy class when the proxy file does not exist.
*
* This strategy causes a file_exists() call whenever any proxy is used the
* first time in a request.
*/
public const AUTOGENERATE_FILE_NOT_EXISTS = 2;

/**
* Generate the proxy classes using eval().
*
* This strategy is only sane for development, and even then it gives me
* the creeps a little.
*/
public const AUTOGENERATE_EVAL = 3;

/**
* Autogenerate the proxy class when the proxy file does not exist or
* when the proxied file changed.
*
* This strategy causes a file_exists() call whenever any proxy is used the
* first time in a request. When the proxied file is changed, the proxy will
* be updated.
*/
public const AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED = 4;

private const PROXY_CLASS_TEMPLATE = <<<'EOPHP'
<?php
Expand Down Expand Up @@ -118,7 +80,7 @@ public function __serialize(): array
/** The UnitOfWork this factory uses to retrieve persisters */
private readonly UnitOfWork $uow;

/** @var self::AUTOGENERATE_* */
/** @var Configuration::AUTOGENERATE_* */
private $autoGenerate;

/** @var array<class-string, Closure> */
Expand All @@ -128,16 +90,16 @@ public function __serialize(): array
* Initializes a new instance of the <tt>ProxyFactory</tt> class that is
* connected to the given <tt>EntityManager</tt>.
*
* @param DocumentManager $dm 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 bool|self::AUTOGENERATE_* $autoGenerate The strategy for automatically generating proxy classes.
* @param DocumentManager $dm 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 bool|Configuration::AUTOGENERATE_* $autoGenerate The strategy for automatically generating proxy classes.
*/
public function __construct(
private readonly DocumentManager $dm,
private readonly string $proxyDir,
private readonly string $proxyNs,
bool|int $autoGenerate = self::AUTOGENERATE_NEVER,
bool|int $autoGenerate = Configuration::AUTOGENERATE_NEVER,
) {
if (! $proxyDir) {
throw new InvalidArgumentException('You must configure a proxy directory. See docs for details');
Expand All @@ -152,7 +114,7 @@ public function __construct(
}

$this->uow = $dm->getUnitOfWork();
$this->autoGenerate = self::AUTOGENERATE_ALWAYS; // @todo (int) $autoGenerate;
$this->autoGenerate = Configuration::AUTOGENERATE_ALWAYS; // @todo (int) $autoGenerate;
}

/** @param array<mixed> $identifier */
Expand Down Expand Up @@ -300,7 +262,7 @@ private function loadProxyClass(ClassMetadata $class): string
return $proxyClassName;
}

if ($this->autoGenerate === self::AUTOGENERATE_EVAL) {
if ($this->autoGenerate === Configuration::AUTOGENERATE_EVAL) {
$this->generateProxyClass($class, null, $proxyClassName);

return $proxyClassName;
Expand All @@ -309,17 +271,17 @@ private function loadProxyClass(ClassMetadata $class): string
$fileName = $this->getProxyFileName($class->getName(), $this->proxyDir);

switch ($this->autoGenerate) {
case self::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED:
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS_OR_CHANGED:
if (file_exists($fileName) && filemtime($fileName) >= filemtime($class->getReflectionClass()->getFileName())) {
break;
}
// no break
case self::AUTOGENERATE_FILE_NOT_EXISTS:
case Configuration::AUTOGENERATE_FILE_NOT_EXISTS:
if (file_exists($fileName)) {
break;
}
// no break
case self::AUTOGENERATE_ALWAYS:
case Configuration::AUTOGENERATE_ALWAYS:
$this->generateProxyClass($class, $fileName, $proxyClassName);
break;
}
Expand Down

0 comments on commit 847dac8

Please sign in to comment.