Skip to content

Commit

Permalink
Merge pull request #57 from Flowpack/56-php-8-attribute-for-defer-ann…
Browse files Browse the repository at this point in the history
…otation

"defer" annotation as PHP 8 attribute
  • Loading branch information
robertlemke authored May 17, 2022
2 parents 3e7a68e + 18f2cf4 commit 7dd6a6a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
21 changes: 12 additions & 9 deletions Classes/Annotations/Defer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand All @@ -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 ?? [];
}
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)**
Expand Down

0 comments on commit 7dd6a6a

Please sign in to comment.