Skip to content
Permalink
Browse files

EntityManager => EntityManagerDecorator (#22)

  • Loading branch information...
MartkCz authored and f3l1x committed Nov 2, 2018
1 parent e97632f commit d4c006e263e94bd16180c87ed7ef38ec8badd019
@@ -4,7 +4,7 @@

- [Minimal configuration](#minimal-configuration)
- [ORM extension](#ormextension)
- [EntityManager](#entitymanager)
- [EntityManagerDecorator](#entitymanagerdecorator)
- [Configuration](#configuration)
- [Bridges](#bridges)
- [Annotations Bridge](#annotations-bridge)
@@ -64,13 +64,25 @@ You can find more examples in our [playground](https://github.com/nettrine/playg

OrmExtension has a few options you can configure. Let's take a look at them.

### EntityManager
### EntityManagerDecorator

Defining your own EntityManager is useful for addind or overriding methods you needed.
Defining your own EntityManagerDecorator is useful for addind or overriding methods you needed.

```yaml
orm:
entityManagerClass: App\Model\Database\EntityManager
entityManagerDecoratorClass: App\Model\Database\EntityManagerDecorator
```

```php
namespace App\Model\Database;
use Nettrine\ORM\EntityManagerDecorator as NettrineEntityManagerDecorator;
class EntityManagerDecorator extends NettrineEntityManagerDecorator
{
}
```

### Configuration
@@ -4,12 +4,12 @@
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager as DoctrineEntityManager;
use Doctrine\ORM\Mapping\UnderscoreNamingStrategy;
use Nette\DI\CompilerExtension;
use Nette\DI\Helpers;
use Nette\DI\Statement;
use Nettrine\ORM\EntityManager;
use Nettrine\ORM\EntityManagerFactory;
use Nettrine\ORM\EntityManagerDecorator;
use Nettrine\ORM\Exception\Logical\InvalidStateException;
use Nettrine\ORM\ManagerRegistry;
use Nettrine\ORM\Mapping\ContainerEntityListenerResolver;
@@ -19,7 +19,7 @@ final class OrmExtension extends CompilerExtension
/** @var mixed[] */
private $defaults = [
'entityManagerClass' => EntityManager::class,
'entityManagerDecoratorClass' => EntityManagerDecorator::class,
'configuration' => [
'proxyDir' => '%tempDir%/proxies',
'autoGenerateProxyClasses' => null,
@@ -115,26 +115,30 @@ public function loadEntityManagerConfiguration(): void
$builder = $this->getContainerBuilder();
$config = $this->getConfig();
$entityManagerClass = $config['entityManagerClass'];
if (!class_exists($entityManagerClass)) {
throw new InvalidStateException(sprintf('EntityManager class "%s" not found', $entityManagerClass));
$entityManagerDecoratorClass = $config['entityManagerDecoratorClass'];
if (!class_exists($entityManagerDecoratorClass)) {
throw new InvalidStateException(sprintf('EntityManagerDecorator class "%s" not found', $entityManagerDecoratorClass));
}
// Entity Manager
$builder->addDefinition($this->prefix('entityManager'))
->setType($entityManagerClass)
->setFactory(EntityManagerFactory::class . '::create', [
$original = $builder->addDefinition($this->prefix('entityManager'))
->setType(DoctrineEntityManager::class)
->setFactory(DoctrineEntityManager::class . '::create', [
$builder->getDefinitionByType(Connection::class), // Nettrine/DBAL
$this->prefix('@configuration'),
$entityManagerClass,
]);
])
->setAutowired(false);
// Entity Manager Decorator
$builder->addDefinition($this->prefix('entityManagerDecorator'))
->setFactory($entityManagerDecoratorClass, [$original]);
// ManagerRegistry
$builder->addDefinition($this->prefix('managerRegistry'))
->setType(ManagerRegistry::class)
->setArguments([
$builder->getDefinitionByType(Connection::class),
$this->prefix('@entityManager'),
$this->prefix('@entityManagerDecorator'),
]);
}

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace Nettrine\ORM;
use Doctrine\ORM\Decorator\EntityManagerDecorator as DoctrineEntityManagerDecorator;
class EntityManagerDecorator extends DoctrineEntityManagerDecorator
{
}

This file was deleted.

Oops, something went wrong.
@@ -4,7 +4,7 @@
use Doctrine\Common\Persistence\AbstractManagerRegistry;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Proxy\Proxy;
use Nette\DI\Container;
@@ -15,7 +15,7 @@ class ManagerRegistry extends AbstractManagerRegistry
/** @var Container */
private $container;
public function __construct(Connection $connection, EntityManager $em, Container $container)
public function __construct(Connection $connection, EntityManagerInterface $em, Container $container)
{
$defaultConnection = $container->findByType(get_class($connection))[0];
$defaultManager = $container->findByType(get_class($em))[0];
@@ -55,7 +55,7 @@ public function getAliasNamespace($alias): string
{
foreach (array_keys($this->getManagers()) as $name) {
try {
/** @var EntityManager $entityManager */
/** @var EntityManagerInterface $entityManager */
$entityManager = $this->getManager($name);
return $entityManager->getConfiguration()->getEntityNamespace($alias);
} catch (ORMException $e) {
@@ -10,7 +10,7 @@
use Nettrine\ORM\DI\OrmAnnotationsExtension;
use Nettrine\ORM\DI\OrmCacheExtension;
use Nettrine\ORM\DI\OrmExtension;
use Nettrine\ORM\EntityManager;
use Nettrine\ORM\EntityManagerDecorator;
use Tests\Nettrine\ORM\Cases\TestCase;
final class OrmCacheExtensionTest extends TestCase
@@ -34,8 +34,8 @@ public function testCacheDrivers(): void
/** @var Container $container */
$container = new $class();
/** @var EntityManager $em */
$em = $container->getByType(EntityManager::class);
/** @var EntityManagerDecorator $em */
$em = $container->getByType(EntityManagerDecorator::class);
self::assertInstanceOf(FilesystemCache::class, $em->getConfiguration()->getHydrationCacheImpl());
self::assertInstanceOf(FilesystemCache::class, $em->getConfiguration()->getMetadataCacheImpl());
@@ -8,9 +8,9 @@
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\ORM\DI\OrmAnnotationsExtension;
use Nettrine\ORM\DI\OrmExtension;
use Nettrine\ORM\EntityManager;
use Nettrine\ORM\EntityManagerDecorator;
use Tests\Nettrine\ORM\Cases\TestCase;
use Tests\Nettrine\ORM\Fixtures\DummyEntityManager;
use Tests\Nettrine\ORM\Fixtures\DummyEntityManagerDecorator;
final class OrmExtensionTest extends TestCase
{
@@ -32,7 +32,7 @@ public function testRegisterAnnotations(): void
/** @var Container $container */
$container = new $class();
self::assertInstanceOf(EntityManager::class, $container->getByType(EntityManager::class));
self::assertInstanceOf(EntityManagerDecorator::class, $container->getByType(EntityManagerDecorator::class));
}
public function testOwnEntityManager(): void
@@ -50,14 +50,14 @@ public function testOwnEntityManager(): void
]);
$compiler->addConfig([
'orm' => [
'entityManagerClass' => DummyEntityManager::class,
'entityManagerDecoratorClass' => DummyEntityManagerDecorator::class,
],
]);
}, __CLASS__ . __METHOD__);
/** @var Container $container */
$container = new $class();
self::assertInstanceOf(DummyEntityManager::class, $container->getByType(DummyEntityManager::class));
self::assertInstanceOf(DummyEntityManagerDecorator::class, $container->getByType(DummyEntityManagerDecorator::class));
}
}

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);
namespace Tests\Nettrine\ORM\Fixtures;
use Nettrine\ORM\EntityManagerDecorator;
class DummyEntityManagerDecorator extends EntityManagerDecorator
{
}

0 comments on commit d4c006e

Please sign in to comment.
You can’t perform that action at this time.