|
| 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 | +} |
0 commit comments