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 55a7714
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 55 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
2 changes: 2 additions & 0 deletions docs/en/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The best way to install Kdyby/Doctrine is using [Composer](http://getcomposer.or

```sh
$ composer require kdyby/doctrine
$ composer require kdyby/events
```

and now enable the extension using your neon config
Expand All @@ -26,6 +27,7 @@ extensions:

Please see documentation, on how to configure [Kdyby/Events](https://github.com/Kdyby/Events/blob/master/docs/en/index.md), [Kdyby/Console](https://github.com/Kdyby/Console/blob/master/docs/en/index.md) and [Kdyby/Annotations](https://github.com/Kdyby/Annotations/blob/master/docs/en/index.md).

Also, you don't have to install Kdyby/Events if you don't want to, Kdyby/Doctrine should work fine without it.

Minimal configuration
---------------------
Expand Down
48 changes: 27 additions & 21 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,10 +351,17 @@ 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);
if ($this->isKdybyEventsPresent()) {
$builder->addDefinition($this->prefix($name . '.evm'))
->setClass('Kdyby\Events\NamespacedEventManager', [Kdyby\Doctrine\Events::NS . '::'])
->addSetup('$dispatchGlobalEvents', [TRUE]) // for BC
->setAutowired(FALSE);

} else {
$builder->addDefinition($this->prefix($name . '.evm'))
->setClass('Doctrine\Common\EventManager')
->setAutowired(FALSE);
}

// entity manager
$entityManager = $builder->addDefinition($managerServiceId = $this->prefix($name . '.entityManager'))
Expand Down Expand Up @@ -522,7 +530,7 @@ protected function processConnection($name, array $defaults, $isDefault = FALSE)
->setFactory('Kdyby\Doctrine\Connection::create', [
$options,
$this->prefix('@' . $name . '.dbalConfiguration'),
$this->prefix('@' . $name . '.evm')
$this->prefix('@' . $name . '.evm'),
])
->addSetup('setSchemaTypes', [$schemaTypes])
->addSetup('setDbalTypes', [$dbalTypes])
Expand Down Expand Up @@ -631,18 +639,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 +875,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
13 changes: 9 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 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
1 change: 0 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

0 comments on commit 55a7714

Please sign in to comment.