-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSiteAccessAwareEntityManagerFactory.php
More file actions
91 lines (77 loc) · 2.92 KB
/
SiteAccessAwareEntityManagerFactory.php
File metadata and controls
91 lines (77 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?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\Psr6\DoctrineProvider;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Doctrine\Persistence\ManagerRegistry as Registry;
use Ibexa\Bundle\Core\ApiLoader\RepositoryConfigurationProvider;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
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 ArrayAdapter();
$config = new Configuration();
$config->setMetadataCacheImpl(DoctrineProvider::wrap($cache));
$driverImpl = $config->newDefaultAnnotationDriver(__DIR__.'/../Entity', false);
$config->setMetadataDriverImpl($driverImpl);
$config->setQueryCacheImpl(DoctrineProvider::wrap($cache));
$config->setProxyDir($this->settings['cache_dir'].'/eZProtectedContent/');
$config->setProxyNamespace('eZProtectedContent\Proxies');
$config->setAutoGenerateProxyClasses($this->settings['debug']);
$config->setEntityListenerResolver($this->resolver);
$config->setNamingStrategy(new UnderscoreNamingStrategy());
return EntityManager::create($connection, $config);
}
}