61 lines
1.7 KiB
PHP
61 lines
1.7 KiB
PHP
<?php
|
|
/**
|
|
* MineAdmin is committed to providing solutions for quickly building web applications
|
|
* Please view the LICENSE file that was distributed with this source code,
|
|
* For the full copyright and license information.
|
|
* Thank you very much for using MineAdmin.
|
|
*
|
|
* @Author X.Mo<root@imoi.cn>
|
|
* @Link https://gitee.com/xmo/MineAdmin
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
namespace Builder\Aspect;
|
|
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Di\Annotation\Aspect;
|
|
use Hyperf\Di\Aop\AbstractAspect;
|
|
use Hyperf\Di\Aop\ProceedingJoinPoint;
|
|
use Builder\Annotation\Transaction;
|
|
use Builder\Exception\MineException;
|
|
|
|
/**
|
|
* Class TransactionAspect
|
|
* @package Builder\Aspect
|
|
*/
|
|
#[Aspect]
|
|
class TransactionAspect extends AbstractAspect
|
|
{
|
|
public array $annotations = [
|
|
Transaction::class
|
|
];
|
|
|
|
/**
|
|
* @param ProceedingJoinPoint $proceedingJoinPoint
|
|
* @return mixed
|
|
*/
|
|
public function process(ProceedingJoinPoint $proceedingJoinPoint)
|
|
{
|
|
/** @var Transaction $transaction */
|
|
if (isset($proceedingJoinPoint->getAnnotationMetadata()->method[Transaction::class])) {
|
|
$transaction = $proceedingJoinPoint->getAnnotationMetadata()->method[Transaction::class];
|
|
}
|
|
try {
|
|
Db::beginTransaction();
|
|
$number = 0;
|
|
$retry = intval($transaction->retry);
|
|
do {
|
|
$result = $proceedingJoinPoint->process();
|
|
if (! is_null($result)) {
|
|
break;
|
|
}
|
|
++$number;
|
|
} while ($number < $retry);
|
|
Db::commit();
|
|
} catch (\Throwable $e) {
|
|
Db::rollBack();
|
|
throw new MineException($e->getMessage(), 500);
|
|
}
|
|
return $result;
|
|
}
|
|
} |