Skip to content

Commit 0737809

Browse files
committed
Add ProtectedContentBundle SiteAccessAwareEntityManagerFactory
1 parent d5b7a2d commit 0737809

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* NovaeZProtectedContentBundle.
5+
*
6+
* @package Novactive\Bundle\eZProtectedContentBundle
7+
*
8+
* @author Novactive
9+
* @copyright 2019 Novactive
10+
* @license https://github.com/Novactive/eZProtectedContentBundle/blob/master/LICENSE MIT Licence
11+
*/
12+
13+
namespace Novactive\Bundle\eZProtectedContentBundle\Core;
14+
15+
use Doctrine\Bundle\DoctrineBundle\Mapping\ContainerEntityListenerResolver;
16+
use Doctrine\Common\Cache\ArrayCache;
17+
use Doctrine\ORM\Configuration;
18+
use Doctrine\ORM\EntityManager;
19+
use Doctrine\ORM\EntityManagerInterface;
20+
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
21+
use Doctrine\Persistence\ManagerRegistry as Registry;
22+
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
23+
24+
class SiteAccessAwareEntityManagerFactory
25+
{
26+
/**
27+
* @var Registry
28+
*/
29+
private $registry;
30+
31+
/**
32+
* @var RepositoryConfigurationProvider
33+
*/
34+
private $repositoryConfigurationProvider;
35+
36+
/**
37+
* @var array
38+
*/
39+
private $settings;
40+
41+
/**
42+
* @var ContainerEntityListenerResolver
43+
*/
44+
private $resolver;
45+
46+
public function __construct(
47+
Registry $registry,
48+
RepositoryConfigurationProvider $repositoryConfigurationProvider,
49+
ContainerEntityListenerResolver $resolver,
50+
array $settings
51+
) {
52+
$this->registry = $registry;
53+
$this->repositoryConfigurationProvider = $repositoryConfigurationProvider;
54+
$this->settings = $settings;
55+
$this->resolver = $resolver;
56+
}
57+
58+
private function getConnectionName(): string
59+
{
60+
$config = $this->repositoryConfigurationProvider->getRepositoryConfig();
61+
62+
return $config['storage']['connection'] ?? 'default';
63+
}
64+
65+
public function get(): EntityManagerInterface
66+
{
67+
$connectionName = $this->getConnectionName();
68+
// If it is the default connection then we don't bother we can directly use the default entity Manager
69+
if ('default' === $connectionName) {
70+
return $this->registry->getManager();
71+
}
72+
73+
$connection = $this->registry->getConnection($connectionName);
74+
75+
/** @var \Doctrine\DBAL\Connection $connection */
76+
$cache = new ArrayCache();
77+
$config = new Configuration();
78+
$config->setMetadataCacheImpl($cache);
79+
$driverImpl = $config->newDefaultAnnotationDriver(__DIR__.'/../Entity', false);
80+
$config->setMetadataDriverImpl($driverImpl);
81+
$config->setQueryCacheImpl($cache);
82+
$config->setProxyDir($this->settings['cache_dir'].'/eZSEOBundle/');
83+
$config->setProxyNamespace('eZSEOBundle\Proxies');
84+
$config->setAutoGenerateProxyClasses($this->settings['debug']);
85+
$config->setEntityListenerResolver($this->resolver);
86+
$config->setNamingStrategy(new UnderscoreNamingStrategy());
87+
88+
return EntityManager::create($connection, $config);
89+
}
90+
}

bundle/Resources/config/services.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ services:
77
public: false
88
bind:
99
$httpCachePurgeClient: '@ezplatform.http_cache.purge_client'
10+
$entityManager: "@novaezprotectedcontent.doctrine.entity_manager"
1011

1112
Novactive\Bundle\eZProtectedContentBundle\Command\:
1213
resource: '../../Command'
@@ -33,3 +34,13 @@ services:
3334
Novactive\Bundle\eZProtectedContentBundle\Listener\PasswordProvided:
3435
tags:
3536
- { name: kernel.event_listener, event: kernel.request, method: 'onKernelRequest', priority: -100}
37+
38+
novaezprotectedcontent.doctrine.entity_manager:
39+
class: Doctrine\ORM\EntityManagerInterface
40+
factory: ['@Novactive\Bundle\eZSEOBundle\Core\SiteAccessAwareEntityManagerFactory', 'get']
41+
42+
Novactive\Bundle\eZProtectedContentBundle\Core\SiteAccessAwareEntityManagerFactory:
43+
arguments:
44+
$repositoryConfigurationProvider: "@ezpublish.api.repository_configuration_provider"
45+
$resolver: "@doctrine.orm.default_entity_listener_resolver"
46+
$settings: { debug: "%kernel.debug%", cache_dir: "%kernel.cache_dir%" }

0 commit comments

Comments
 (0)