Skip to content

Commit

Permalink
Merge pull request #275 from alleyinteractive/attributes-event-listener
Browse files Browse the repository at this point in the history
  • Loading branch information
srtfisher authored May 24, 2022
2 parents 663aa6b + 90b06cb commit 9a4c540
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/mantle/framework/events/class-discover-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Mantle\Framework\Events;

use Mantle\Support\Attributes\Action;
use Mantle\Support\Reflector;
use Mantle\Support\Str;
use ReflectionClass;
Expand Down Expand Up @@ -79,8 +80,26 @@ protected static function get_listener_events( $listeners, string $base_path ):
}

foreach ( $listener->getMethods( ReflectionMethod::IS_PUBLIC ) as $method ) {
// Check for attribute support with PHP 8.
if ( version_compare( phpversion(), '8.0.0', '>=' ) ) {
// Check if the method has an attribute action.
$action_attributes = $method->getAttributes( Action::class );

if ( ! empty( $action_attributes ) ) {
foreach ( $action_attributes as $attribute ) {
$instance = $attribute->newInstance();

$listener_events[ $listener->name . '@' . $method->name ] = [
[ $instance->action ],
$instance->priority,
];
}

continue;
}
}

// Handle WordPress hooks being registered with a listener.
// todo: move to use attributes with PHP 8.
if ( Str::starts_with( $method->name, 'on_' ) ) {
$hook = Str::after( $method->name, 'on_' );
$priority = 10;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Mantle\Tests\Framework\Events\Fixtures\Listeners;

use Mantle\Support\Attributes\Action;
use Mantle\Tests\Framework\Events\Fixtures\Events\Event_One;
use Mantle\Tests\Framework\Events\Fixtures\Events\Event_Two;
use WP_Query;
Expand Down Expand Up @@ -29,4 +30,22 @@ public function on_wp_loaded( WP_Query $query ) {
public function on_pre_get_posts_at_20( WP_Query $query ) {
// ...
}

#[Action('attribute-event')]
public function handle_attribute_string_callback( $event ) {
// ...

}

#[Action('attribute-event', 20)]
public function handle_attribute_string_callback_priority( $event ) {
// ...

}

#[Action(Event_One::class)]
public function handle_attribute_event_one( Event_One $event ) {
// ...

}
}
22 changes: 20 additions & 2 deletions tests/framework/events/test-discover-events.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Mantle\Tests\Framework\Events\Fixtures\Events\Event_Two;
use Mantle\Tests\Framework\Events\Fixtures\Listeners\Example_Listener;

use function Mantle\Support\Helpers\collect;

class Test_Discover_Events extends Framework_Test_Case {
protected function setUp(): void {
parent::setUp();
Expand All @@ -33,11 +35,14 @@ public function test_events_can_be_discovered() {
getcwd(),
);

$this->assertEquals( [
$is_php_8 = version_compare( PHP_VERSION, '8.0.0', '>=' );

$expected = [
// Type hinted events.
Event_One::class => [
[ Example_Listener::class . '@handle', 10 ],
[ Example_Listener::class . '@handle_event_one', 10 ],
[ Example_Listener::class . '@handle_attribute_event_one', 10 ],
],
Event_Two::class => [
[ Example_Listener::class . '@handle_event_two', 10 ],
Expand All @@ -50,6 +55,19 @@ public function test_events_can_be_discovered() {
'pre_get_posts' => [
[ Example_Listener::class . '@on_pre_get_posts_at_20', 20 ],
],
], $events );
'attribute-event' => $is_php_8 ? [
[ Example_Listener::class . '@handle_attribute_string_callback', 10 ],
[ Example_Listener::class . '@handle_attribute_string_callback_priority', 20 ],
] : null,
];

// Filter out expected events for PHP 7.4.
if ( ! $is_php_8 ) {
$expected = collect( $expected )
->filter( fn ( $events ) => null !== $events )
->to_array();
}

$this->assertEquals( $expected, $events );
}
}

0 comments on commit 9a4c540

Please sign in to comment.