Skip to content

Commit

Permalink
Set a standard convention for service listner IDs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Crell committed Mar 2, 2019
1 parent 166c4a4 commit 46c4a4a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/OrderedCollection/OrderedCollection.php
Expand Up @@ -79,6 +79,8 @@ public function addItem($item, int $priority = 0, string $id = null) : string
*/
public function addItemBefore(string $pivotId, $item, string $id = null) : string
{
$id = $this->enforceUniqueId($id);

// If the item this new item is pivoting off of is already defined, add it normally.
if (isset($this->itemLookup[$pivotId])) {
// Because high numbers come first, we have to ADD one to get the new item to be returned first.
Expand Down Expand Up @@ -113,6 +115,8 @@ public function addItemBefore(string $pivotId, $item, string $id = null) : strin
*/
public function addItemAfter(string $pivotId, $item, string $id = null) : string
{
$id = $this->enforceUniqueId($id);

// If the item this new item is pivoting off of is already defined, add it normally.
if (isset($this->itemLookup[$pivotId])) {
// Because high numbers come first, we have to SUBTRACT one to get the new item to be returned first.
Expand Down
3 changes: 3 additions & 0 deletions src/OrderedListenerProvider.php
Expand Up @@ -65,16 +65,19 @@ public function addListenerAfter(string $pivotId, callable $listener, string $id

public function addListenerService(string $serviceName, string $methodName, string $type, $priority = 0, string $id = null): string
{
$id = $id ?? $serviceName . '-' . $methodName;
return $this->addListener($this->makeListenerForService($serviceName, $methodName), $priority, $id, $type);
}

public function addListenerServiceBefore(string $pivotId, string $serviceName, string $methodName, string $type, string $id = null) : string
{
$id = $id ?? $serviceName . '-' . $methodName;
return $this->addListenerBefore($pivotId, $this->makeListenerForService($serviceName, $methodName), $id, $type);
}

public function addListenerServiceAfter(string $pivotId, string $serviceName, string $methodName, string $type, string $id = null) : string
{
$id = $id ?? $serviceName . '-' . $methodName;
return $this->addListenerAfter($pivotId, $this->makeListenerForService($serviceName, $methodName), $id, $type);
}

Expand Down
33 changes: 33 additions & 0 deletions tests/OrderedListenerProviderIdTest.php
Expand Up @@ -128,4 +128,37 @@ public function test_explicit_id_for_function(): void
$this->assertEquals('BACD', implode($event->result()));
}

public function test_natural_id_for_service_listener() : void
{
$container = new MockContainer();

$container->addService('A', new class
{
public function listen(CollectingEvent $event)
{
$event->add('A');
}
});
$container->addService('B', new class
{
public function listen(CollectingEvent $event)
{
$event->add('B');
}
});

$p = new OrderedListenerProvider($container);

$idA = $p->addListenerService('A', 'listen', CollectingEvent::class, -4);
$p->addListenerServiceAfter('A-listen', 'B', 'listen', CollectingEvent::class);

$event = new CollectingEvent();
foreach ($p->getListenersForEvent($event) as $listener) {
$listener($event);
}

$this->assertEquals('A-listen', $idA);
$this->assertEquals('AB', implode($event->result()));
}

}

0 comments on commit 46c4a4a

Please sign in to comment.