Skip to content

Commit

Permalink
is subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Dec 15, 2024
1 parent b0e9a5d commit b050f81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/Attributes/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,16 @@
#[Attribute(Attribute::TARGET_CLASS)]
class EventSubscriber
{
//
public static function isSubscriber(object|string $objectOrClass): bool
{
if ($objectOrClass instanceof \ReflectionClass) {
$ref = $objectOrClass;
} else {
$ref = new \ReflectionClass($objectOrClass);
}

$subscriberAttributes = $ref->getAttributes(self::class, \ReflectionAttribute::IS_INSTANCEOF);

return count($subscriberAttributes) > 0;
}
}
3 changes: 1 addition & 2 deletions src/Provider/SubscribableListenerProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ public function on(
public function subscribe(object $subscriber, ?int $priority = null): void
{
$ref = new ReflectionObject($subscriber);
$subscriberAttributes = $ref->getAttributes(EventSubscriber::class, ReflectionAttribute::IS_INSTANCEOF);

if (count($subscriberAttributes) > 0) {
if (EventSubscriber::isSubscriber($ref)) {
foreach ($ref->getMethods() as $method) {
// Handle ListenTo attributes
foreach ($method->getAttributes(ListenTo::class, ReflectionAttribute::IS_INSTANCEOF) as $attribute) {
Expand Down

0 comments on commit b050f81

Please sign in to comment.