Skip to content

Commit

Permalink
Don't rely on LazyProxyTrait in LazyServiceEntityRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Nov 13, 2023
1 parent ca64ca7 commit 965b031
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 107 deletions.
62 changes: 39 additions & 23 deletions Repository/LazyServiceEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Doctrine\ORM\EntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use LogicException;
use Symfony\Component\VarExporter\LazyGhostTrait;
use Symfony\Component\VarExporter\LazyObjectInterface;

use function sprintf;
Expand All @@ -18,41 +17,58 @@
*/
class LazyServiceEntityRepository extends EntityRepository implements ServiceEntityRepositoryInterface
{
use LazyGhostTrait {
createLazyGhost as private;
}
private $registry;

Check failure on line 20 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Property \Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository::$registry does not have native type hint nor @var annotation for its value.
private $entityClass;

Check failure on line 21 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Property \Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository::$entityClass does not have native type hint nor @var annotation for its value.

/**
* @param string $entityClass The class name of the entity this repository manages
* @psalm-param class-string<T> $entityClass
*/
public function __construct(ManagerRegistry $registry, string $entityClass)
{
$initializer = function ($instance, $property) use ($registry, $entityClass) {
$manager = $registry->getManagerForClass($entityClass);
$this->registry = $registry;

Check failure on line 29 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space
$this->entityClass = $entityClass;

if ($manager === null) {
throw new LogicException(sprintf(
'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.',
$entityClass,
));
}
if ($this instanceof LazyObjectInterface) {
$this->initialize();

parent::__construct($manager, $manager->getClassMetadata($entityClass));
return;

Check warning on line 35 in Repository/LazyServiceEntityRepository.php

View check run for this annotation

Codecov / codecov/patch

Repository/LazyServiceEntityRepository.php#L35

Added line #L35 was not covered by tests
}

return $this->$property;
};
unset($this->_em);
unset($this->_class);
unset($this->_entityName);
}

if ($this instanceof LazyObjectInterface) {
$initializer($this, '_entityName');
public function __get($name)

Check failure on line 43 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository::__get() does not have parameter type hint nor @param annotation for its parameter $name.

Check failure on line 43 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Method \Doctrine\Bundle\DoctrineBundle\Repository\LazyServiceEntityRepository::__get() does not have return type hint nor @return annotation for its return value.
{
$this->initialize();

return;
$scope = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? null;

Check failure on line 47 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Function debug_backtrace() should not be referenced via a fallback global name, but via a use statement.

Check failure on line 47 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Constant DEBUG_BACKTRACE_IGNORE_ARGS should not be referenced via a fallback global name, but via a use statement.

return (function () use ($name) { return $this->$name; })->bindTo($this, $scope)();

Check failure on line 49 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Opening brace must be the last content on the line

Check failure on line 49 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Expected 0 lines before "return", found -1.

Check failure on line 49 in Repository/LazyServiceEntityRepository.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Expected 0 lines after "return", found -1.
}

public function __isset($name): bool

Check warning on line 52 in Repository/LazyServiceEntityRepository.php

View check run for this annotation

Codecov / codecov/patch

Repository/LazyServiceEntityRepository.php#L52

Added line #L52 was not covered by tests
{
$this->initialize();

Check warning on line 54 in Repository/LazyServiceEntityRepository.php

View check run for this annotation

Codecov / codecov/patch

Repository/LazyServiceEntityRepository.php#L54

Added line #L54 was not covered by tests

$scope = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'] ?? null;

Check warning on line 56 in Repository/LazyServiceEntityRepository.php

View check run for this annotation

Codecov / codecov/patch

Repository/LazyServiceEntityRepository.php#L56

Added line #L56 was not covered by tests

return (function () use ($name) { return isset($this->$name); })->bindTo($this, $scope)();

Check warning on line 58 in Repository/LazyServiceEntityRepository.php

View check run for this annotation

Codecov / codecov/patch

Repository/LazyServiceEntityRepository.php#L58

Added line #L58 was not covered by tests
}

private function initialize(): void
{
$manager = $this->registry->getManagerForClass($this->entityClass);

if ($manager === null) {
throw new LogicException(sprintf(
'Could not find the entity manager for class "%s". Check your Doctrine configuration to make sure it is configured to load this entity’s metadata.',
$this->entityClass,
));
}

self::createLazyGhost([
"\0*\0_em" => $initializer,
"\0*\0_class" => $initializer,
"\0*\0_entityName" => $initializer,
], null, $this);
parent::__construct($manager, $manager->getClassMetadata($this->entityClass));
}
}
38 changes: 0 additions & 38 deletions Repository/LegacyServiceEntityRepository.php

This file was deleted.

66 changes: 20 additions & 46 deletions Repository/ServiceEntityRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,30 @@
namespace Doctrine\Bundle\DoctrineBundle\Repository;

use Doctrine\ORM\EntityRepository;
use Symfony\Component\VarExporter\LazyGhostTrait;

use function property_exists;
use function trait_exists;

if (property_exists(EntityRepository::class, '_entityName')) {
if (trait_exists(LazyGhostTrait::class)) {
// ORM 2 with VarExporter
/**
* Optional EntityRepository base class with a simplified constructor (for autowiring).
*
* To use in your class, inject the "registry" service and call
* the parent constructor. For example:
*
* class YourEntityRepository extends ServiceEntityRepository
* {
* public function __construct(ManagerRegistry $registry)
* {
* parent::__construct($registry, YourEntity::class);
* }
* }
*
* @template T of object
* @template-extends LazyServiceEntityRepository<T>
*/
class ServiceEntityRepository extends LazyServiceEntityRepository
{
}
} else {
// ORM 2 without VarExporter
/**
* Optional EntityRepository base class with a simplified constructor (for autowiring).
*
* To use in your class, inject the "registry" service and call
* the parent constructor. For example:
*
* class YourEntityRepository extends ServiceEntityRepository
* {
* public function __construct(ManagerRegistry $registry)
* {
* parent::__construct($registry, YourEntity::class);
* }
* }
*
* @template T of object
* @template-extends LegacyServiceEntityRepository<T>
*/
class ServiceEntityRepository extends LegacyServiceEntityRepository
{
}
// ORM 2
/**
* Optional EntityRepository base class with a simplified constructor (for autowiring).
*
* To use in your class, inject the "registry" service and call
* the parent constructor. For example:
*
* class YourEntityRepository extends ServiceEntityRepository
* {
* public function __construct(ManagerRegistry $registry)
* {
* parent::__construct($registry, YourEntity::class);
* }
* }
*
* @template T of object
* @template-extends LazyServiceEntityRepository<T>
*/
class ServiceEntityRepository extends LazyServiceEntityRepository
{
}
} else {
// ORM 3
Expand Down

0 comments on commit 965b031

Please sign in to comment.