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

Commit 1deece7

Browse files
author
calvinalkan
committed
fix bug in ClassListener.php that caused shouldListen to not be called when a Listener got created as a string callable Listener@handle
1 parent be4c346 commit 1deece7

File tree

2 files changed

+52
-13
lines changed

2 files changed

+52
-13
lines changed

src/Listeners/ClassListener.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
use BetterWpHooks\Contracts\AbstractListener;
66
use BetterWpHooks\Traits\ReflectsCallable;
77
use Contracts\ContainerAdapter;
8-
9-
use function BetterWpHooks\Functions\normalizeClassMethod;
8+
9+
use Illuminate\Support\Str;
10+
11+
use function BetterWpHooks\Functions\normalizeClassMethod;
1012

1113
class ClassListener extends AbstractListener {
1214

@@ -24,7 +26,7 @@ class ClassListener extends AbstractListener {
2426

2527
public function __construct( array $listener, ContainerAdapter $container ) {
2628

27-
$this->class_callable = array_values( $listener );
29+
$this->class_callable = $this->toArrayCallable( $listener );
2830
$this->container = $container;
2931

3032
}
@@ -35,15 +37,13 @@ public function toArray(): array {
3537
return $this->class_callable;
3638

3739
}
38-
39-
40+
4041
public function execute( $payload ) {
4142

4243
return $this->callClassMethod( $this->class_callable, $payload );
4344

4445
}
45-
46-
46+
4747
public function aliases(): array {
4848

4949
return [
@@ -54,8 +54,7 @@ public function aliases(): array {
5454
];
5555

5656
}
57-
58-
57+
5958
public function shouldHandle( $payload ): bool {
6059

6160
if ( ! $hasTrait = $this->hasConditionalTrait( $this->class_callable[0] ) ) {
@@ -66,8 +65,7 @@ public function shouldHandle( $payload ): bool {
6665

6766

6867
}
69-
70-
68+
7169
private function callClassMethod( array $class_callable, $payload ) {
7270

7371
$parameters = $this->buildParameterNames( $class_callable, $payload );
@@ -76,6 +74,20 @@ private function callClassMethod( array $class_callable, $payload ) {
7674

7775

7876
}
79-
77+
78+
private function toArrayCallable(array $listener) : array
79+
{
80+
81+
$listener = array_values($listener);
82+
83+
if (Str::contains($listener[0],'@')) {
84+
85+
$listener = Str::parseCallback($listener[0]);
86+
87+
}
88+
89+
return $listener;
90+
91+
}
8092

8193
}

tests/Unit/WordpressDispatcherTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,34 @@ public function the_payload_is_not_changed_when_a_listener_does_not_listen_at_ru
821821

822822

823823
}
824-
824+
825+
/** @test */
826+
public function should_listen_gets_called_even_when_the_listener_got_created_as_a_string_callable()
827+
{
828+
829+
// Class Listener
830+
$_SERVER['should_handle'] = TRUE;
831+
832+
$this->dispatcher->listen( 'event', ConditionalListener::class . '@foobar' );
833+
834+
$this->dispatchAndAssertAction( 'event', 'foo', [ ConditionalListener::class . '@foobar', 'foobar' ] );
835+
836+
$this->reset();
837+
838+
839+
// Nothing
840+
$_SERVER['should_handle'] = FALSE;
841+
842+
$this->dispatcher->listen( 'event', ConditionalListener::class . '@foobar' );
843+
844+
$this->dispatchAndAssertNoActionDone( 'event', 'foo', [
845+
ConditionalListener::class . '@foobar',
846+
'foobar',
847+
] );
848+
849+
unset( $_SERVER['should_handle'] );
850+
851+
}
825852

826853

827854
/**

0 commit comments

Comments
 (0)