Skip to content

Commit

Permalink
merged branch mtdowling/event_dispatcher_add_iterator (PR symfony#5268)
Browse files Browse the repository at this point in the history
Commits
-------

0ad00f8 [EventDispatcher] Adding IteratorAggregate to GenericEvent

Discussion
----------

[EventDispatcher] Adding IteratorAggregate to GenericEvent

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

by drak at 2012-08-16T07:43:29Z

What is the use case for this that it should be part of the Generic event?

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

by mtdowling at 2012-08-16T17:12:28Z

This allows for the GenericEvent to be even more generic. Now listeners don't need to know an exact key from the arguments, but rather can iterate over the arguments to find what they are looking for. This makes the GenericEvent more like an array.

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

by mtdowling at 2012-08-17T19:31:04Z

How would this be a nasty break? It's just giving the GenericEvent more capabilities with IteratorAggregate.

This is a completely separate PR from the one that flipped the constructor args.

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

by schmittjoh at 2012-08-17T19:34:47Z

Why are you not just doing ``foreach ($event->getArguments() as $arg) { /** ... */ }``?

If you just have ``foreach ($event)``, to me at least it would not be so clear what we are actually iterating over.

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

by mtdowling at 2012-08-17T19:39:23Z

This class already has ArrayAccess. If you're already using this class like an array, then I think you should expect to be able to iterate it like an array. I'm just finishing that concept off by implementing IteratorAggregate.

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

by schmittjoh at 2012-08-17T19:47:43Z

Indeed, if we already have ArrayAccess which we probably don't want to remove again, then that seems reasonable.
  • Loading branch information
fabpot committed Aug 18, 2012
2 parents 1f37191 + 0ad00f8 commit 7fe18d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/Symfony/Component/EventDispatcher/GenericEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author Drak <[email protected]>
*/
class GenericEvent extends Event implements \ArrayAccess
class GenericEvent extends Event implements \ArrayAccess, \IteratorAggregate
{
/**
* Observer pattern subject.
Expand Down Expand Up @@ -177,4 +177,14 @@ public function offsetExists($key)
{
return $this->hasArgument($key);
}

/**
* IteratorAggregate for iterating over the object like an array
*
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->arguments);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,13 @@ public function testGetSubject()
{
$this->assertSame($this->subject, $this->event->getSubject());
}

public function testHasIterator()
{
$data = array();
foreach ($this->event as $key => $value) {
$data[$key] = $value;
}
$this->assertEquals(array('name' => 'Event'), $data);
}
}

0 comments on commit 7fe18d1

Please sign in to comment.