Skip to content

Commit

Permalink
Merge pull request #1 from laravel-doctrine/1.0
Browse files Browse the repository at this point in the history
Merge upstream
  • Loading branch information
maxbrokman committed Sep 23, 2015
2 parents ae4849c + 8549679 commit 13e2575
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,31 @@ protected function registerListeners($settings = [], EntityManagerInterface $man
{
if (isset($settings['events']['listeners'])) {
foreach ($settings['events']['listeners'] as $event => $listener) {
if (!class_exists($listener, false)) {
throw new InvalidArgumentException("Listener {$listener} does not exist");
}
$this->registerListener($event, $listener, $manager);
}
}
}

$manager->getEventManager()->addEventListener($event, new $listener);
/**
* @param string $event
* @param string|string[] $listener
* @param EntityManagerInterface $manager
*/
private function registerListener($event, $listener, EntityManagerInterface $manager)
{
if (is_array($listener)) {
foreach ($listener as $individualListener) {
$this->registerListener($event, $individualListener, $manager);
}

return;
}

if (!class_exists($listener, false)) {
throw new InvalidArgumentException("Listener {$listener} does not exist");
}

$manager->getEventManager()->addEventListener($event, new $listener);
}

/**
Expand Down
27 changes: 27 additions & 0 deletions tests/EntityManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,29 @@ public function test_can_set_listeners()
$this->assertTrue(array_key_exists('name', $manager->getEventManager()->getListeners()));
}

public function test_can_set_multiple_listeners()
{
$this->disableDebugbar();
$this->disableSecondLevelCaching();
$this->disableCustomCacheNamespace();
$this->disableCustomFunctions();
$this->enableLaravelNamingStrategy();

$this->settings['events']['listeners'] = [
'name' => [
ListenerStub::class,
AnotherListenerStub::class
]
];

$manager = $this->factory->create($this->settings);

$this->assertEntityManager($manager);
$this->assertCount(1, $manager->getEventManager()->getListeners());
$this->assertTrue(array_key_exists('name', $manager->getEventManager()->getListeners()));
$this->assertCount(2, $manager->getEventManager()->getListeners('name'));
}

public function test_can_set_subscribers()
{
$this->disableDebugbar();
Expand Down Expand Up @@ -531,6 +554,10 @@ class ListenerStub
{
}

class AnotherListenerStub
{
}

class SubscriberStub implements EventSubscriber
{
/**
Expand Down

0 comments on commit 13e2575

Please sign in to comment.