-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add helper class for single-event subscription
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/Symfony/Component/EventDispatcher/EventListenerTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\EventDispatcher; | ||
|
||
use Symfony\Contracts\EventDispatcher\Event as ContractsEvent; | ||
|
||
/** | ||
* Helper class for single-event subscription. | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
trait EventListenerTrait | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
final public static function getSubscribedEvents(): array | ||
{ | ||
return [static::getEventName() => [static::getMethodName(), static::getPriority()]]; | ||
} | ||
|
||
public static function getEventName(): string | ||
{ | ||
$method = new \ReflectionMethod(static::class, static::getMethodName()); | ||
|
||
if (0 === $method->getNumberOfParameters()) { | ||
throw new \LogicException(sprintf('We cannot infer the Event name from "%s::%s()" method without parameters, you must to define one or implement the "%s::getEventName()" method to return it explicitly.', static::class, $method->getName(), static::class)); | ||
} | ||
|
||
$parameter = $method->getParameters()[0]; | ||
|
||
if (null === $type = $parameter->getType()) { | ||
throw new \LogicException(sprintf('We cannot infer the Event name from "%s::%s()" method without type-hint the "$%s" parameter, you must to type-hint it or implement the "%s::getEventName()" method to return it explicitly.', ContractsEvent::class, $method->getName(), $parameter->getName(), static::class)); | ||
} | ||
|
||
if ($type->isBuiltin()) { | ||
throw new \LogicException(sprintf('The "$%s" parameter defined in "%s::%s()" method must be a class of "%s", "%s" given.', $parameter->getName(), static::class, $method->getName(), ContractsEvent::class, $type->getName())); | ||
} | ||
|
||
return $type->getName(); | ||
} | ||
|
||
public static function getMethodName(): string | ||
{ | ||
return '__invoke'; | ||
} | ||
|
||
public static function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters