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