merged branch drak/clear_tag (PR #3940)

Commits
-------

80f96b7 [DependencyInjection] Add ability to clear tags by name.

Discussion
----------

[DependencyInjection] Add ability to clear tags by name.

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

---------------------------------------------------------------------------

by jalliot at 2012-04-14T07:50:51Z

@drak Just for information, what is the use case?

---------------------------------------------------------------------------

by drak at 2012-04-14T08:40:13Z

I'm using this to filter (and prevent) some listeners from being loaded
into the dispatcher.

---------------------------------------------------------------------------

by lsmith77 at 2012-04-14T09:37:39Z

tags are used by compiler passes. bundles that set these tags expect some defined bundle to take some specific actions on the given services. as such this is already a fairly "fragile" relationship. once other compiler passes then start messing with these tags it can get even more tricky. then again, as long as you know what you are doing ..

that being said .. this method just adds convenience, since one could always just get all the tags, unset and reset them in bulk.

---------------------------------------------------------------------------

by drak at 2012-04-14T09:42:04Z

@lsmith77 - exactly.
This commit is contained in:
Fabien Potencier 2012-04-14 12:39:38 +02:00
commit 45d43d325a
2 changed files with 35 additions and 0 deletions

View File

@ -451,6 +451,22 @@ class Definition
return isset($this->tags[$name]);
}
/**
* Clears all tags for a given name.
*
* @param string $name The tag name
*
* @return Definition
*/
public function clearTag($name)
{
if (isset($this->tags[$name])) {
unset($this->tags[$name]);
}
return $this;
}
/**
* Clears the tags for this definition.
*

View File

@ -187,6 +187,25 @@ class DefinitionTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(array(), $def->getTags(), '->clearTags() removes all current tags');
}
/**
* @covers Symfony\Component\DependencyInjection\Definition::clearTags
*/
public function testClearTag()
{
$def = new Definition('stdClass');
$this->assertSame($def, $def->clearTags(), '->clearTags() implements a fluent interface');
$def->addTag('1foo1', array('foo1' => 'bar1'));
$def->addTag('2foo2', array('foo2' => 'bar2'));
$def->addTag('3foo3', array('foo3' => 'bar3'));
$def->clearTag('2foo2');
$this->assertTrue($def->hasTag('1foo1'));
$this->assertFalse($def->hasTag('2foo2'));
$this->assertTrue($def->hasTag('3foo3'));
$def->clearTag('1foo1');
$this->assertFalse($def->hasTag('1foo1'));
$this->assertTrue($def->hasTag('3foo3'));
}
/**
* @covers Symfony\Component\DependencyInjection\Definition::addTag
* @covers Symfony\Component\DependencyInjection\Definition::getTag