Skip to content

Commit

Permalink
Add ProtectedContentBundle SiteAccessAwareEntityManagerFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Plopix committed Apr 30, 2021
1 parent d5b7a2d commit 0737809
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
90 changes: 90 additions & 0 deletions bundle/Core/SiteAccessAwareEntityManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/**
* NovaeZProtectedContentBundle.
*
* @package Novactive\Bundle\eZProtectedContentBundle
*
* @author Novactive
* @copyright 2019 Novactive
* @license https://github.com/Novactive/eZProtectedContentBundle/blob/master/LICENSE MIT Licence
*/

namespace Novactive\Bundle\eZProtectedContentBundle\Core;

use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\Persistence\ManagerRegistry as Registry;
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;

class SiteAccessAwareEntityManagerFactory
{
/**
* @var Registry
*/
private $registry;

/**
* @var RepositoryConfigurationProvider
*/
private $repositoryConfigurationProvider;

/**
* @var array
*/
private $settings;

/**
* @var ContainerEntityListenerResolver
*/
private $resolver;

public function __construct(
Registry $registry,
RepositoryConfigurationProvider $repositoryConfigurationProvider,
ContainerEntityListenerResolver $resolver,
array $settings
) {
$this->registry = $registry;
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
$this->settings = $settings;
$this->resolver = $resolver;
}

private function getConnectionName(): string
{
$config = $this->repositoryConfigurationProvider->getRepositoryConfig();

return $config['storage']['connection'] ?? 'default';
}

public function get(): EntityManagerInterface
{
$connectionName = $this->getConnectionName();
// If it is the default connection then we don't bother we can directly use the default entity Manager
if ('default' === $connectionName) {
return $this->registry->getManager();
}

$connection = $this->registry->getConnection($connectionName);

/** @var \Doctrine\DBAL\Connection $connection */
$cache = new ArrayCache();
$config = new Configuration();
$config->setMetadataCacheImpl($cache);
$driverImpl = $config->newDefaultAnnotationDriver(__DIR__.'/../Entity', false);
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl($cache);
$config->setProxyDir($this->settings['cache_dir'].'/eZSEOBundle/');
$config->setProxyNamespace('eZSEOBundle\Proxies');
$config->setAutoGenerateProxyClasses($this->settings['debug']);
$config->setEntityListenerResolver($this->resolver);
$config->setNamingStrategy(new UnderscoreNamingStrategy());

return EntityManager::create($connection, $config);
}
}
11 changes: 11 additions & 0 deletions bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
public: false
bind:
$httpCachePurgeClient: '@ezplatform.http_cache.purge_client'
$entityManager: "@novaezprotectedcontent.doctrine.entity_manager"

Novactive\Bundle\eZProtectedContentBundle\Command\:
resource: '../../Command'
Expand All @@ -33,3 +34,13 @@ services:
Novactive\Bundle\eZProtectedContentBundle\Listener\PasswordProvided:
tags:
- { name: kernel.event_listener, event: kernel.request, method: 'onKernelRequest', priority: -100}

novaezprotectedcontent.doctrine.entity_manager:
class: Doctrine\ORM\EntityManagerInterface
factory: ['@Novactive\Bundle\eZSEOBundle\Core\SiteAccessAwareEntityManagerFactory', 'get']

Novactive\Bundle\eZProtectedContentBundle\Core\SiteAccessAwareEntityManagerFactory:
arguments:
$repositoryConfigurationProvider: "@ezpublish.api.repository_configuration_provider"
$resolver: "@doctrine.orm.default_entity_listener_resolver"
$settings: { debug: "%kernel.debug%", cache_dir: "%kernel.cache_dir%" }

0 comments on commit 0737809

Please sign in to comment.