Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ability to customize the listener priorities #384

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions DependencyInjection/StofDoctrineExtensionsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ private function processObjectManagerConfigurations(array $configs, ContainerBui
);

foreach ($configs as $name => $listeners) {
foreach ($listeners as $ext => $enabled) {
if (!$enabled) {
foreach ($listeners as $ext => $settingValue) {
if (false === $settingValue) {
continue;
}

Expand All @@ -122,6 +122,10 @@ private function processObjectManagerConfigurations(array $configs, ContainerBui
$attributes['priority'] = $listenerPriorities[$ext];
}

if (is_numeric($settingValue)) {
$attributes['priority'] = $settingValue;
}

$definition = $container->getDefinition(sprintf('stof_doctrine_extensions.listener.%s', $ext));
$definition->addTag($doctrineSubscriberTag, $attributes);

Expand Down
37 changes: 37 additions & 0 deletions Tests/DependencyInjection/StofDoctrineExtensionsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,41 @@ public function testLoadBothConfig($listener)
$this->assertCount(1, $def->getTag('doctrine.event_subscriber'));
$this->assertCount(1, $def->getTag('doctrine_mongodb.odm.event_subscriber'));
}

public function testConfigWithCustomListenerPriorities()
{
$extension = new StofDoctrineExtensionsExtension();
$container = new ContainerBuilder();

$config = array(
'orm' => array('default' => array(
'blameable' => 0,
'loggable' => -1,
'uploadable' => true,
'sortable' => false,
)),
'mongodb' => array('default' => array(
'blameable' => 0,
'loggable' => -1,
'uploadable' => true,
'sortable' => false,
)),
);

$extension->load(array($config), $container);

$def = $container->getDefinition('stof_doctrine_extensions.listener.blameable');
$this->assertTrue($def->hasTag('doctrine.event_subscriber'));
$this->assertArraySubset(array(array('priority' => 0)), $def->getTag('doctrine.event_subscriber'));

$def = $container->getDefinition('stof_doctrine_extensions.listener.loggable');
$this->assertTrue($def->hasTag('doctrine.event_subscriber'));
$this->assertArraySubset(array(array('priority' => -1)), $def->getTag('doctrine.event_subscriber'));

$def = $container->getDefinition('stof_doctrine_extensions.listener.uploadable');
$this->assertTrue($def->hasTag('doctrine.event_subscriber'));
$this->assertArraySubset(array(array('priority' => -5)), $def->getTag('doctrine.event_subscriber'));

$this->assertFalse($container->hasDefinition('stof_doctrine_extensions.listener.sortable'));
}
}