Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -17766,6 +17766,12 @@ parameters:
count: 1
path: src/lib/Persistence/Cache/UserPreferenceHandler.php

-
message: '#^Return type \(Doctrine\\ORM\\Mapping\\ClassMetadataFactory\) of method Ibexa\\Core\\Persistence\\Doctrine\\SiteAccessAwareEntityManager\:\:getMetadataFactory\(\) should be compatible with return type \(Doctrine\\Persistence\\Mapping\\ClassMetadataFactory\<Doctrine\\Persistence\\Mapping\\ClassMetadata\<object\>\>\) of method Doctrine\\Persistence\\ObjectManager\:\:getMetadataFactory\(\)$#'
identifier: method.childReturnType
count: 2
path: src/lib/Persistence/Doctrine/SiteAccessAwareEntityManager.php

-
message: '#^Property Ibexa\\Core\\Persistence\\FieldTypeRegistry\:\:\$coreFieldTypes \(array\<Ibexa\\Contracts\\Core\\FieldType\\FieldType\>\) does not accept array\<Ibexa\\Core\\Persistence\\FieldType\>\.$#'
identifier: assign.propertyType
Expand Down
8 changes: 5 additions & 3 deletions src/bundle/Core/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,11 @@ services:
- { name: console.command }

ibexa.doctrine.orm.entity_manager:
class: Doctrine\ORM\EntityManager
lazy: true
factory: ['@ibexa.doctrine.orm.entity_manager_factory', 'getEntityManager']
class: Ibexa\Core\Persistence\Doctrine\SiteAccessAwareEntityManager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we also have decorates: here?
Regardless: doesn't that approach leave us with some technical debt we need to reassess later while upgrading dependencies late? I wonder how Doctrine themselves have dealt with providing support for PHP8.4. Have you checked that? First idea is the one we initially have - bumping the dependency version. However, maybe there is a specific PHP8.4-compatibility-related part we might be able to utilize for the time being.

POV ping @alongosz @Steveb-p.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should NOT decorate the primary Doctrine Entity Manager.

We only expect it to "overwrite" the default behavior specifically when we ask for ibexa.doctrine.orm.entity_manager service.

arguments:
$entityManagerFactory: '@ibexa.doctrine.orm.entity_manager_factory'
tags:
- { name: 'kernel.reset', method: 'reset' }

ibexa.doctrine.orm.entity_manager_factory:
class: Ibexa\Bundle\Core\Entity\EntityManagerFactory
Expand Down
308 changes: 308 additions & 0 deletions src/lib/Persistence/Doctrine/SiteAccessAwareEntityManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Core\Persistence\Doctrine;

use Doctrine\DBAL\Connection;
use Doctrine\ORM\Cache;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\NativeQuery;
use Doctrine\ORM\Proxy\ProxyFactory;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\Expr;
use Doctrine\ORM\Query\FilterCollection;
use Doctrine\ORM\Query\ResultSetMapping;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\UnitOfWork;
use Ibexa\Bundle\Core\Entity\EntityManagerFactory;
use Ibexa\Contracts\Core\MVC\EventSubscriber\ConfigScopeChangeSubscriber;
use Ibexa\Core\MVC\Symfony\Event\ScopeChangeEvent;
use Symfony\Contracts\Service\ResetInterface;

/**
* @internal
*/
final class SiteAccessAwareEntityManager implements EntityManagerInterface, ConfigScopeChangeSubscriber, ResetInterface
{
private EntityManagerFactory $entityManagerFactory;

private ?EntityManagerInterface $resolvedEntityManager = null;

public function __construct(EntityManagerFactory $entityManagerFactory)
{
$this->entityManagerFactory = $entityManagerFactory;
}

public function onConfigScopeChange(ScopeChangeEvent $event): void
{
$this->resolvedEntityManager = null;
}

public function reset(): void
{
$this->resolvedEntityManager = null;
}

private function getWrapped(): EntityManagerInterface
{
return $this->resolvedEntityManager ??= $this->entityManagerFactory->getEntityManager();
}

public function getConnection(): Connection
{
return $this->getWrapped()->getConnection();
}

public function getExpressionBuilder(): Expr
{
return $this->getWrapped()->getExpressionBuilder();
}

public function beginTransaction(): void
{
$this->getWrapped()->beginTransaction();
}

public function transactional($func)
{
return $this->getWrapped()->transactional($func);
}

/**
* @return mixed
*/
public function wrapInTransaction(callable $func)
{
return $this->getWrapped()->wrapInTransaction($func);
}

public function commit(): void
{
$this->getWrapped()->commit();
}

public function rollback(): void
{
$this->getWrapped()->rollback();
}

public function createQuery($dql = ''): Query
{
return $this->getWrapped()->createQuery($dql);
}

public function createNamedQuery($name): Query
{
return $this->getWrapped()->createNamedQuery($name);
}

public function createNativeQuery($sql, ResultSetMapping $rsm): NativeQuery
{
return $this->getWrapped()->createNativeQuery($sql, $rsm);
}

public function createNamedNativeQuery($name): NativeQuery
{
return $this->getWrapped()->createNamedNativeQuery($name);
}

public function createQueryBuilder(): QueryBuilder
{
return $this->getWrapped()->createQueryBuilder();
}

/**
* @template T of object
*
* @param class-string<T> $entityName
*
* @return T|null
*/
public function getReference($entityName, $id): ?object
{
return $this->getWrapped()->getReference($entityName, $id);
}

/**
* @template T of object
*
* @param class-string<T> $entityName
*
* @return T|null
*/
public function getPartialReference($entityName, $identifier): ?object
{
return $this->getWrapped()->getPartialReference($entityName, $identifier);
}

public function close(): void
{
$this->getWrapped()->close();
}

/**
* @template T of object
*
* @param T $entity
* @param bool $deep
*
* @return T
*/
public function copy($entity, $deep = false): object
{
/** @var T */
return $this->getWrapped()->copy($entity, $deep);
}

public function lock($entity, $lockMode, $lockVersion = null): void
{
$this->getWrapped()->lock($entity, $lockMode, $lockVersion);
}

public function getEventManager(): \Doctrine\Common\EventManager
{
return $this->getWrapped()->getEventManager();
}

public function getConfiguration(): Configuration
{
return $this->getWrapped()->getConfiguration();
}

public function isOpen(): bool
{
return $this->getWrapped()->isOpen();
}

public function getUnitOfWork(): UnitOfWork
{
return $this->getWrapped()->getUnitOfWork();
}

public function getHydrator($hydrationMode): AbstractHydrator
{
return $this->getWrapped()->getHydrator($hydrationMode);
}

public function newHydrator($hydrationMode): AbstractHydrator
{
return $this->getWrapped()->newHydrator($hydrationMode);
}

public function getProxyFactory(): ProxyFactory
{
return $this->getWrapped()->getProxyFactory();
}

public function getFilters(): FilterCollection
{
return $this->getWrapped()->getFilters();
}

public function isFiltersStateClean(): bool
{
return $this->getWrapped()->isFiltersStateClean();
}

public function hasFilters(): bool
{
return $this->getWrapped()->hasFilters();
}

public function getCache(): ?Cache
{
return $this->getWrapped()->getCache();
}

public function find($className, $id): ?object
{
return $this->getWrapped()->find($className, $id);
}

public function persist(object $object): void
{
$this->getWrapped()->persist($object);
}

public function remove(object $object): void
{
$this->getWrapped()->remove($object);
}

public function clear(): void
{
$this->getWrapped()->clear();
}

public function detach(object $object): void
{
$this->getWrapped()->detach($object);
}

public function refresh(object $object, ?int $lockMode = null): void
{
$this->getWrapped()->refresh($object, $lockMode);
}

public function flush(): void
{
$this->getWrapped()->flush();
}

/**
* @template T of object
*
* @param class-string<T> $className
*
* @return EntityRepository<T>
*/
public function getRepository($className): EntityRepository
{
return $this->getWrapped()->getRepository($className);
}

/**
* @template T of object
*
* @param class-string<T> $className
*
* @return ClassMetadata<T>
*/
public function getClassMetadata($className): ClassMetadata
{
return $this->getWrapped()->getClassMetadata($className);
}

public function getMetadataFactory(): ClassMetadataFactory
{
return $this->getWrapped()->getMetadataFactory();
}

public function initializeObject(object $obj): void
{
$this->getWrapped()->initializeObject($obj);
}

/**
* @param mixed $value
*/
public function isUninitializedObject($value): bool
{
return $this->getWrapped()->isUninitializedObject($value);
}

public function contains(object $object): bool
{
return $this->getWrapped()->contains($object);
}
}
Loading