diff --git a/Classes/Annotations/Defer.php b/Classes/Annotations/Defer.php index 99bf1a7..07b2282 100644 --- a/Classes/Annotations/Defer.php +++ b/Classes/Annotations/Defer.php @@ -11,12 +11,14 @@ * source code. */ -use Doctrine\Common\Annotations\Annotation as DoctrineAnnotation; +use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor; /** * @Annotation - * @DoctrineAnnotation\Target("METHOD") + * @NamedArgumentConstructor + * @Target("METHOD") */ +#[\Attribute(\Attribute::TARGET_METHOD)] final class Defer { /** @@ -32,15 +34,16 @@ final class Defer public $options; /** - * @param array $values - * @throws \InvalidArgumentException + * @param string|null $queueName + * @param array|null $options + * @param string|null $value */ - public function __construct(array $values) + public function __construct(?string $queueName = null, ?array $options = null, ?string $value = null) { - if (!isset($values['value']) && !isset($values['queueName'])) { - throw new \InvalidArgumentException('A Defer annotation must specify a queueName.', 1334128835); + if ($value === null && $queueName === null) { + throw new \InvalidArgumentException('A Defer attribute must specify a queueName.', 1334128835); } - $this->queueName = isset($values['queueName']) ? $values['queueName'] : $values['value']; - $this->options = isset($values['options']) ? $values['options'] : []; + $this->queueName = $queueName ?? $value; + $this->options = $options ?? []; } } diff --git a/README.md b/README.md index 1dda20c..524c253 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,21 @@ Neos Flow package that allows for asynchronous and distributed execution of task } ``` + or use attributes instead of annotations (PHP 8.0 and later): + + ```php + use Flowpack\JobQueue\Common\Annotations as Job; + + class SomeClass { + + #[Job\Defer(queueName: "some-queue")] + public function sendEmail($emailAddress) + { + // send some email to $emailAddress + } + } + ``` + *Note:* The method needs to be *public* and it must not return anything 5. **Start the worker (if required)**