Skip to content
This repository has been archived by the owner on Apr 17, 2022. It is now read-only.

Commit

Permalink
default values now get returned if listeners are registered but none …
Browse files Browse the repository at this point in the history
…of them handled the event conditionally
  • Loading branch information
calvinalkan committed Apr 21, 2021
1 parent 1deece7 commit 8358b06
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
43 changes: 29 additions & 14 deletions src/Dispatchers/WordpressDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,20 +215,38 @@ public function dispatch( $event, ...$payload ) {
if ( ! $this->hasListeners( $event ) ) {


if ( is_callable( [ $payload, 'default' ] ) ) {
return $payload->default();
}

return is_object( $payload ) ? $payload : $payload[0];
return $this->determineDefault($payload);


}

return $this->hook_api->applyFilter( $event, $payload );



$filtered = $this->hook_api->applyFilter( $event, $payload );

if ( $filtered === $payload ) {

return $this->determineDefault($payload);

}

return $filtered;


}


private function determineDefault( $payload) {

if ( is_callable( [ $payload, 'default' ] ) ) {

return $payload->default();

}

return is_object( $payload ) ? $payload : $payload[0];

}

/**
* Parse the given event and payload and prepare them for dispatching.
* If the event that got dispatched is an object we will use the classname as the event and
Expand All @@ -252,8 +270,7 @@ private function parseEventAndPayload( $event, $payload ): array {


}



/**
*
* Searches the first registered listener that implements
Expand Down Expand Up @@ -400,8 +417,7 @@ private function resolveAlias( $event, $callable ) {
return $this->aliases[ $event ][ $key ] ?? classNameIfClassExists( $callable );

}



/**
* Uses the alias of the listener to find and return
* the object hash of the listener callable
Expand Down Expand Up @@ -471,8 +487,7 @@ public function forgetOne( string $event, $listener ) {
}

}



/**
*
* Checks if a registered closure is marked as unremovable
Expand Down
36 changes: 33 additions & 3 deletions tests/Unit/WordpressDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -803,8 +803,7 @@ public function a_listener_can_handle_an_event_conditionally_based_on_runtime_de


}



/** @test */
public function the_payload_is_not_changed_when_a_listener_does_not_listen_at_runtime() {

Expand Down Expand Up @@ -1043,7 +1042,38 @@ public function if_no_return_value_can_be_created_from_the_dispatched_object_the


}


/** @test */
public function if_listeners_are_present_but_none_of_them_handles_the_event_conditionally_the_default_value_is_returned_for_object_events()
{

// Class Listener
$_SERVER['should_handle'] = FALSE;

$this->dispatcher->listen( FilterableEvent::class, ConditionalListener::class . '@foobar' );

$result = $this->dispatcher->dispatch( new FilterableEvent( 'foobar' ) );

$this->assertSame( 'foobar', $result );


}

/** @test */
public function if_listeners_are_present_but_none_of_them_handles_the_event_conditionally_the_default_value_is_returned_for_non_object_events()
{

// Class Listener
$_SERVER['should_handle'] = FALSE;

$this->dispatcher->listen( 'event', ConditionalListener::class . '@foobar' );

$value1 = $this->dispatcher->dispatch( 'event', 'foo', 'bar', 'baz' );

$this->assertEquals( 'foo', $value1 );


}

private function reset(): void {

Expand Down

0 comments on commit 8358b06

Please sign in to comment.