Skip to content

Commit

Permalink
Make Kdyby/Events optional
Browse files Browse the repository at this point in the history
[Closes #266] [Closes #239] [Ref #238]
  • Loading branch information
enumag authored and fprochazka committed Jul 8, 2016
1 parent 13716b1 commit be31ae2
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 64 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
"doctrine/orm": "~2.5",
"doctrine/dbal": "~2.5",

"kdyby/events": "~3.0@dev",
"kdyby/console": "~2.5@dev",
"kdyby/annotations": "~2.3@dev",
"kdyby/doctrine-cache": "~2.5@dev"
Expand All @@ -38,6 +37,7 @@
"kdyby/doctrine-dbal-batchimport": "Batch import & execute utils for effective processing of SQL Imports from files and strings"
},
"require-dev": {
"kdyby/events": "~3.0@dev",
"nette/application": "~2.3@dev",
"nette/bootstrap": "~2.3@dev",
"nette/caching": "~2.3@dev",
Expand Down
70 changes: 40 additions & 30 deletions src/Kdyby/Doctrine/DI/OrmExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,7 @@ public function loadConfiguration()
$this->configuredManagers =
$this->postCompileRepositoriesQueue = [];

$extensions = array_filter($this->compiler->getExtensions(), function ($item) {
return $item instanceof Kdyby\Annotations\DI\AnnotationsExtension;
});
if (empty($extensions)) {
if (!$this->compiler->getExtensions('Kdyby\Annotations\DI\AnnotationsExtension')) {
throw new Nette\Utils\AssertionException('You should register \'Kdyby\Annotations\DI\AnnotationsExtension\' before \'' . get_class($this) . '\'.', E_USER_NOTICE);
}

Expand Down Expand Up @@ -187,6 +184,10 @@ public function loadConfiguration()
}

if ($this->targetEntityMappings) {
if (!$this->isKdybyEventsPresent()) {
throw new Nette\Utils\AssertionException('The option \'targetEntityMappings\' requires \'Kdyby\Events\DI\EventsExtension\'.', E_USER_NOTICE);
}

$listener = $builder->addDefinition($this->prefix('resolveTargetEntityListener'))
->setClass('Kdyby\Doctrine\Tools\ResolveTargetEntityListener')
->addTag(Kdyby\Events\DI\EventsExtension::SUBSCRIBER_TAG)
Expand Down Expand Up @@ -350,19 +351,24 @@ protected function processEntityManager($name, array $defaults)
$this->targetEntityMappings = Nette\Utils\Arrays::mergeTree($this->targetEntityMappings, $config['targetEntityMappings']);
}

$builder->addDefinition($this->prefix($name . '.evm'))
->setClass('Kdyby\Events\NamespacedEventManager', [Kdyby\Doctrine\Events::NS . '::'])
->addSetup('$dispatchGlobalEvents', [TRUE]) // for BC
->setAutowired(FALSE);
$entityManagerArguments = [
$connectionService = $this->processConnection($name, $defaults, $isDefault),
$this->prefix('@' . $name . '.ormConfiguration'),
];

if ($this->isKdybyEventsPresent()) {
$builder->addDefinition($this->prefix($name . '.evm'))
->setClass('Kdyby\Events\NamespacedEventManager', [Kdyby\Doctrine\Events::NS . '::'])
->addSetup('$dispatchGlobalEvents', [TRUE]) // for BC
->setAutowired(FALSE);

$entityManagerArguments[] = $this->prefix('@' . $name . '.evm');
}

// entity manager
$entityManager = $builder->addDefinition($managerServiceId = $this->prefix($name . '.entityManager'))
->setClass('Kdyby\Doctrine\EntityManager')
->setFactory('Kdyby\Doctrine\EntityManager::create', [
$connectionService = $this->processConnection($name, $defaults, $isDefault),
$this->prefix('@' . $name . '.ormConfiguration'),
$this->prefix('@' . $name . '.evm'),
])
->setFactory('Kdyby\Doctrine\EntityManager::create', $entityManagerArguments)
->addTag(self::TAG_ENTITY_MANAGER)
->addTag('kdyby.doctrine.entityManager')
->setAutowired($isDefault)
Expand Down Expand Up @@ -517,13 +523,19 @@ protected function processConnection($name, array $defaults, $isDefault = FALSE)

// connection
$options = array_diff_key($config, array_flip(['types', 'resultCache', 'connection', 'logging']));

$connectionArguments = [
$options,
$this->prefix('@' . $name . '.dbalConfiguration'),
];

if ($this->isKdybyEventsPresent()) {
$connectionArguments[] = $this->prefix('@' . $name . '.evm');
}

$connection = $builder->addDefinition($connectionServiceId = $this->prefix($name . '.connection'))
->setClass('Kdyby\Doctrine\Connection')
->setFactory('Kdyby\Doctrine\Connection::create', [
$options,
$this->prefix('@' . $name . '.dbalConfiguration'),
$this->prefix('@' . $name . '.evm')
])
->setFactory('Kdyby\Doctrine\Connection::create', $connectionArguments)
->addSetup('setSchemaTypes', [$schemaTypes])
->addSetup('setDbalTypes', [$dbalTypes])
->addTag(self::TAG_CONNECTION)
Expand Down Expand Up @@ -631,18 +643,6 @@ protected function processCache($cache, $suffix)

public function beforeCompile()
{
$eventsExt = NULL;
foreach ($this->compiler->getExtensions() as $extension) {
if ($extension instanceof Kdyby\Events\DI\EventsExtension) {
$eventsExt = $extension;
break;
}
}

if ($eventsExt === NULL) {
throw new Nette\Utils\AssertionException('Please register the required Kdyby\Events\DI\EventsExtension to Compiler.');
}

$this->processRepositories();
}

Expand Down Expand Up @@ -879,6 +879,16 @@ private function isTracyPresent()



/**
* @return bool
*/
private function isKdybyEventsPresent()
{
return (bool) $this->compiler->getExtensions('Kdyby\Events\DI\EventsExtension');
}



private function addCollapsePathsToTracy(Method $init)
{
$blueScreen = 'Tracy\Debugger::getBlueScreen()';
Expand Down
58 changes: 34 additions & 24 deletions tests/KdybyTests/Doctrine/Events.compatibility.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,27 @@ require_once __DIR__ . '/../bootstrap.php';
class EventsCompatibilityTest extends ORMTestCase
{

/**
* @var Kdyby\Doctrine\EntityManager
*/
private $em;



protected function setUp()
{
$this->em = $this->createMemoryManager([
__DIR__ . '/config/events.neon',
]);
}



public function testOuterRegister_new()
{
$em = $this->createMemoryManager();
Assert::type('Kdyby\Events\NamespacedEventManager', $em->getEventManager());
Assert::type('Kdyby\Events\NamespacedEventManager', $this->em->getEventManager());

$outerEvm = $em->getEventManager();
$outerEvm = $this->em->getEventManager();
Assert::false($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

Expand All @@ -42,7 +57,7 @@ class EventsCompatibilityTest extends ORMTestCase
Assert::true($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::true($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($em));
$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($this->em));

Assert::same([[$args]], $new->calls);
}
Expand All @@ -51,10 +66,9 @@ class EventsCompatibilityTest extends ORMTestCase

public function testOuterRegister_old()
{
$em = $this->createMemoryManager();
Assert::type('Kdyby\Events\NamespacedEventManager', $em->getEventManager());
Assert::type('Kdyby\Events\NamespacedEventManager', $this->em->getEventManager());

$outerEvm = $em->getEventManager();
$outerEvm = $this->em->getEventManager();
Assert::false($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

Expand All @@ -63,7 +77,7 @@ class EventsCompatibilityTest extends ORMTestCase
Assert::true($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::true($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($em));
$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($this->em));

Assert::same([[$args]], $old->calls);
}
Expand All @@ -72,10 +86,9 @@ class EventsCompatibilityTest extends ORMTestCase

public function testOuterRegister_combined()
{
$em = $this->createMemoryManager();
Assert::type('Kdyby\Events\NamespacedEventManager', $em->getEventManager());
Assert::type('Kdyby\Events\NamespacedEventManager', $this->em->getEventManager());

$outerEvm = $em->getEventManager();
$outerEvm = $this->em->getEventManager();
Assert::false($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

Expand All @@ -85,7 +98,7 @@ class EventsCompatibilityTest extends ORMTestCase
Assert::true($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::true($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($em));
$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($this->em));

Assert::same([[$args]], $old->calls);
Assert::same([[$args]], $new->calls);
Expand All @@ -95,15 +108,14 @@ class EventsCompatibilityTest extends ORMTestCase

public function testInnerRegister_new()
{
$em = $this->createMemoryManager();
Assert::type('Kdyby\Events\NamespacedEventManager', $em->getEventManager());
Assert::type('Kdyby\Events\NamespacedEventManager', $this->em->getEventManager());

/** @var Kdyby\Events\EventManager $innerEvm */
$innerEvm = $this->serviceLocator->getByType('Kdyby\Events\EventManager');
Assert::false($innerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($innerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm = $em->getEventManager();
$outerEvm = $this->em->getEventManager();
Assert::false($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

Expand All @@ -114,7 +126,7 @@ class EventsCompatibilityTest extends ORMTestCase
Assert::true($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::true($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($em));
$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($this->em));

Assert::same([[$args]], $new->calls);
}
Expand All @@ -123,15 +135,14 @@ class EventsCompatibilityTest extends ORMTestCase

public function testInnerRegister_old()
{
$em = $this->createMemoryManager();
Assert::type('Kdyby\Events\NamespacedEventManager', $em->getEventManager());
Assert::type('Kdyby\Events\NamespacedEventManager', $this->em->getEventManager());

/** @var Kdyby\Events\EventManager $innerEvm */
$innerEvm = $this->serviceLocator->getByType('Kdyby\Events\EventManager');
Assert::false($innerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($innerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm = $em->getEventManager();
$outerEvm = $this->em->getEventManager();
Assert::false($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

Expand All @@ -142,7 +153,7 @@ class EventsCompatibilityTest extends ORMTestCase
Assert::true($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::true($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($em));
$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($this->em));

Assert::same([[$args]], $old->calls);
}
Expand All @@ -151,15 +162,14 @@ class EventsCompatibilityTest extends ORMTestCase

public function testInnerRegister_combined()
{
$em = $this->createMemoryManager();
Assert::type('Kdyby\Events\NamespacedEventManager', $em->getEventManager());
Assert::type('Kdyby\Events\NamespacedEventManager', $this->em->getEventManager());

/** @var Kdyby\Events\EventManager $innerEvm */
$innerEvm = $this->serviceLocator->getByType('Kdyby\Events\EventManager');
Assert::false($innerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($innerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm = $em->getEventManager();
$outerEvm = $this->em->getEventManager();
Assert::false($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::false($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

Expand All @@ -171,7 +181,7 @@ class EventsCompatibilityTest extends ORMTestCase
Assert::true($outerEvm->hasListeners(Doctrine\ORM\Events::onFlush));
Assert::true($outerEvm->hasListeners(Kdyby\Doctrine\Events::onFlush));

$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($em));
$outerEvm->dispatchEvent(Doctrine\ORM\Events::onFlush, $args = new OnFlushEventArgs($this->em));

Assert::same([[$args]], $old->calls);
Assert::same([[$args]], $new->calls);
Expand Down
19 changes: 15 additions & 4 deletions tests/KdybyTests/Doctrine/ORMTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,24 @@ abstract class ORMTestCase extends Tester\TestCase
/**
* @return Kdyby\Doctrine\EntityManager
*/
protected function createMemoryManager()
protected function createMemoryManager(array $files = [])
{
$rootDir = __DIR__ . '/..';

$config = new Nette\Configurator();
$container = $config->setTempDirectory(TEMP_DIR)
$config->setTempDirectory(TEMP_DIR)
->addConfig(__DIR__ . '/../nette-reset.neon')
->addConfig(__DIR__ . '/config/memory.neon')
->addParameters([
'appDir' => $rootDir,
'wwwDir' => $rootDir,
])
->createContainer();
]);

foreach ($files as $file) {
$config->addConfig($file);
}

$container = $config->createContainer();
/** @var Nette\DI\Container $container */

$em = $container->getByType('Kdyby\Doctrine\EntityManager');
Expand All @@ -64,6 +69,12 @@ protected function createMemoryManager()



protected function configure(Configurator $config)
{
}



/**
* @param string $className
* @param array $props
Expand Down
7 changes: 5 additions & 2 deletions tests/KdybyTests/Doctrine/TargetEntityMapping.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require_once __DIR__ . '/models/cms.php';
/**
* @author David Matějka <[email protected]>
*/
class TargetEntityMapping extends KdybyTests\Doctrine\ORMTestCase
class TargetEntityMapping extends ORMTestCase
{

/**
Expand All @@ -33,9 +33,12 @@ class TargetEntityMapping extends KdybyTests\Doctrine\ORMTestCase
private $em;



protected function setUp()
{
$this->em = $this->createMemoryManager();
$this->em = $this->createMemoryManager([
__DIR__ . '/config/events.neon',
]);
}


Expand Down
6 changes: 6 additions & 0 deletions tests/KdybyTests/Doctrine/config/events.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
extensions:
events: Kdyby\Events\DI\EventsExtension

kdyby.doctrine:
targetEntityMappings:
KdybyTests\Doctrine\ICmsAddress: KdybyTests\Doctrine\CmsAddress
2 changes: 0 additions & 2 deletions tests/KdybyTests/Doctrine/config/memory.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ kdyby.doctrine:
memory: true
metadata:
KdybyTests\Doctrine: annotations(%appDir%/Doctrine/models)
targetEntityMappings:
KdybyTests\Doctrine\ICmsAddress: KdybyTests\Doctrine\CmsAddress
3 changes: 2 additions & 1 deletion tests/KdybyTests/nette-reset.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ php:


extensions:
events: Kdyby\Events\DI\EventsExtension
console: Kdyby\Console\DI\ConsoleExtension
annotations: Kdyby\Annotations\DI\AnnotationsExtension
kdyby.doctrine: Kdyby\Doctrine\DI\OrmExtension
Expand Down Expand Up @@ -32,3 +31,5 @@ session:
services:
cacheStorage:
class: Nette\Caching\Storages\MemoryStorage
evm:
class: Doctrine\Common\EventManager

0 comments on commit be31ae2

Please sign in to comment.