Skip to content

Commit 7dd6a6a

Browse files
authored
Merge pull request #57 from Flowpack/56-php-8-attribute-for-defer-annotation
"defer" annotation as PHP 8 attribute
2 parents 3e7a68e + 18f2cf4 commit 7dd6a6a

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

Classes/Annotations/Defer.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@
1111
* source code.
1212
*/
1313

14-
use Doctrine\Common\Annotations\Annotation as DoctrineAnnotation;
14+
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
1515

1616
/**
1717
* @Annotation
18-
* @DoctrineAnnotation\Target("METHOD")
18+
* @NamedArgumentConstructor
19+
* @Target("METHOD")
1920
*/
21+
#[\Attribute(\Attribute::TARGET_METHOD)]
2022
final class Defer
2123
{
2224
/**
@@ -32,15 +34,16 @@ final class Defer
3234
public $options;
3335

3436
/**
35-
* @param array $values
36-
* @throws \InvalidArgumentException
37+
* @param string|null $queueName
38+
* @param array|null $options
39+
* @param string|null $value
3740
*/
38-
public function __construct(array $values)
41+
public function __construct(?string $queueName = null, ?array $options = null, ?string $value = null)
3942
{
40-
if (!isset($values['value']) && !isset($values['queueName'])) {
41-
throw new \InvalidArgumentException('A Defer annotation must specify a queueName.', 1334128835);
43+
if ($value === null && $queueName === null) {
44+
throw new \InvalidArgumentException('A Defer attribute must specify a queueName.', 1334128835);
4245
}
43-
$this->queueName = isset($values['queueName']) ? $values['queueName'] : $values['value'];
44-
$this->options = isset($values['options']) ? $values['options'] : [];
46+
$this->queueName = $queueName ?? $value;
47+
$this->options = $options ?? [];
4548
}
4649
}

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ Neos Flow package that allows for asynchronous and distributed execution of task
6363
}
6464
```
6565

66+
or use attributes instead of annotations (PHP 8.0 and later):
67+
68+
```php
69+
use Flowpack\JobQueue\Common\Annotations as Job;
70+
71+
class SomeClass {
72+
73+
#[Job\Defer(queueName: "some-queue")]
74+
public function sendEmail($emailAddress)
75+
{
76+
// send some email to $emailAddress
77+
}
78+
}
79+
```
80+
6681
*Note:* The method needs to be *public* and it must not return anything
6782

6883
5. **Start the worker (if required)**

0 commit comments

Comments
 (0)