Skip to content

Commit

Permalink
Feature: listeners lazy-loading [closes #1]
Browse files Browse the repository at this point in the history
  • Loading branch information
f3l1x committed Mar 9, 2018
1 parent 94dddd7 commit d94d504
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/DI/NextrasOrmEventsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ private function loadListenerByAnnotation($events, $repository, $listener)
throw new ServiceCreationException(sprintf("Object '%s' should implement '%s'", $listener, $interface));
}

$repositoryDef->addSetup('$service->?[] = ?', [
$repositoryDef->addSetup('$service->?[] = function() {call_user_func_array([?, ?], func_get_args());}', [
$event,
$listenerDef,
$event,
[$listenerDef, $event],
]);
}
}

}
20 changes: 20 additions & 0 deletions tests/cases/EventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,26 @@ protected function createSimpleContainer()
});
}

/**
* @return void
*/
public function testListenerLazyLoading()
{
$container = $this->createSimpleContainer();
$repository = $container->getByType(FooRepository::class);

Assert::falsey($container->isCreated($container->findByType(FooListener::class)[0]));
Assert::falsey($container->isCreated($container->findByType(FooLifecycleListener::class)[0]));

$entity = new Foo();
$entity->bar = 'foobar';

$repository->persistAndFlush($entity);

Assert::truthy($container->isCreated($container->findByType(FooListener::class)[0]));
Assert::truthy($container->isCreated($container->findByType(FooLifecycleListener::class)[0]));
}

/**
* @return void
*/
Expand Down

0 comments on commit d94d504

Please sign in to comment.