|
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
|
|
|
|
|
20
|
|
|
namespace Ytake\LaravelAspect\Interceptor; |
|
21
|
|
|
|
|
22
|
|
|
use Ray\Aop\MethodInvocation; |
|
23
|
|
|
use Ray\Aop\MethodInterceptor; |
|
24
|
|
|
use Illuminate\Database\DatabaseManager; |
|
25
|
|
|
use Ytake\LaravelAspect\Transaction\Runner; |
|
26
|
|
|
use Ytake\LaravelAspect\Transaction\Execute; |
|
27
|
|
|
use Ytake\LaravelAspect\Transaction\TransactionInvoker; |
|
28
|
|
|
use Ytake\LaravelAspect\Annotation\AnnotationReaderTrait; |
|
29
|
|
|
|
|
30
|
|
|
use function is_array; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Class TransactionalInterceptor |
|
34
|
|
|
*/ |
|
35
|
|
|
class TransactionalInterceptor implements MethodInterceptor |
|
36
|
|
|
{ |
|
37
|
|
|
use AnnotationReaderTrait; |
|
38
|
|
|
|
|
39
|
|
|
/** @var DatabaseManager */ |
|
40
|
|
|
protected static $databaseManager; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param MethodInvocation $invocation |
|
44
|
|
|
* |
|
45
|
|
|
* @return object |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
|
|
public function invoke(MethodInvocation $invocation) |
|
49
|
|
|
{ |
|
50
|
|
|
$annotation = $invocation->getMethod()->getAnnotation($this->annotation) ?? new $this->annotation([]); |
|
51
|
|
|
// database connection name |
|
52
|
|
|
$connections = $annotation->value; |
|
53
|
|
|
if (!is_array($connections)) { |
|
54
|
|
|
$connections = [$connections]; |
|
55
|
|
|
} |
|
56
|
|
|
$processes = []; |
|
57
|
|
|
foreach ($connections as $connection) { |
|
58
|
|
|
$processes[] = new TransactionInvoker($connection); |
|
59
|
|
|
} |
|
60
|
|
|
$processes[] = new Execute($invocation); |
|
61
|
|
|
$runner = new Runner($processes); |
|
62
|
|
|
|
|
63
|
|
|
return $runner(static::$databaseManager, ltrim($annotation->expect, '\\')); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param DatabaseManager $databaseManager |
|
68
|
|
|
*/ |
|
69
|
|
|
public function setDatabaseManager(DatabaseManager $databaseManager): void |
|
70
|
|
|
{ |
|
71
|
|
|
static::$databaseManager = $databaseManager; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|