1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
* |
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
14
|
|
|
* and is licensed under the MIT license. |
15
|
|
|
* |
16
|
|
|
* Copyright (c) 2015-2020 Yuuki Takezawa |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
namespace Ytake\LaravelAspect\Interceptor; |
20
|
|
|
|
21
|
|
|
use Ray\Aop\MethodInvocation; |
22
|
|
|
use Ray\Aop\MethodInterceptor; |
23
|
|
|
use Illuminate\Contracts\Bus\Dispatcher; |
24
|
|
|
use Ytake\LaravelAspect\Queue\LazyMessage; |
25
|
|
|
use Ytake\LaravelAspect\Queue\EagerMessage; |
26
|
|
|
use Ytake\LaravelAspect\Annotation\LazyQueue; |
27
|
|
|
use Ytake\LaravelAspect\Annotation\MessageDriven; |
28
|
|
|
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class MessageDrivenInterceptor |
32
|
|
|
*/ |
33
|
|
|
class MessageDrivenInterceptor implements MethodInterceptor |
34
|
|
|
{ |
35
|
|
|
use AnnotationReaderTrait; |
36
|
|
|
|
37
|
|
|
/** @var Dispatcher */ |
38
|
|
|
protected static $dispatcher; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param MethodInvocation $invocation |
42
|
|
|
* |
43
|
|
|
* @return object |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
|
|
public function invoke(MethodInvocation $invocation) |
47
|
|
|
{ |
48
|
|
|
/** @var MessageDriven $annotation */ |
49
|
|
|
$annotation = $invocation->getMethod()->getAnnotation($this->annotation) ?? new $this->annotation([]); |
50
|
|
|
$command = new EagerMessage($invocation); |
51
|
|
|
if ($annotation->value instanceof LazyQueue) { |
52
|
|
|
$command = new LazyMessage($invocation); |
53
|
|
|
$command->onQueue($annotation->onQueue) |
54
|
|
|
->delay($annotation->value->delay()) |
55
|
|
|
->onConnection($annotation->mappedName); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return static::$dispatcher->dispatch($command); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Dispatcher $dispatcher |
63
|
|
|
*/ |
64
|
|
|
public function setBusDispatcher(Dispatcher $dispatcher) |
65
|
|
|
{ |
66
|
|
|
static::$dispatcher = $dispatcher; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|