Skip to content

Commit a13b48a

Browse files
committed
wip
1 parent a4338a1 commit a13b48a

29 files changed

+720
-59
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ jobs:
7979
run: composer phpstan
8080

8181
- name: Test
82-
run: composer test -- --coverage-clover build/logs/clover.xml
82+
run: composer test -- --testdox --coverage-clover build/logs/clover.xml
8383
env:
8484
PG_HOST: 127.0.0.1
8585
MY_HOST: 127.0.0.1

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"require": {
2525
"php": "^8.0.2",
2626
"ext-pdo": "*",
27+
"illuminate/events": "^8.0 || ^9.0 || ^10.0",
2728
"illuminate/support": "^8.0 || ^9.0 || ^10.0",
2829
"illuminate/database": "^8.0 || ^9.0 || ^10.0",
2930
"illuminate/contracts": "^8.0 || ^9.0 || ^10.0"
@@ -46,6 +47,11 @@
4647
"minimum-stability": "dev",
4748
"prefer-stable": true,
4849
"extra": {
50+
"laravel": {
51+
"providers": [
52+
"Mpyw\\LaravelDatabaseAdvisoryLock\\AdvisoryLockServiceProvider"
53+
]
54+
},
4955
"phpstan": {
5056
"includes": [
5157
"extension.neon"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mpyw\LaravelDatabaseAdvisoryLock;
6+
7+
use Illuminate\Support\ServiceProvider;
8+
9+
/**
10+
* class AdvisoryLockServiceProvider
11+
*
12+
* Automatically registered through package discovery.
13+
*/
14+
final class AdvisoryLockServiceProvider extends ServiceProvider
15+
{
16+
public function register(): void
17+
{
18+
$this->app->singleton(TransactionEventHub::class);
19+
}
20+
21+
public function boot(TransactionEventHub $hub): void
22+
{
23+
TransactionEventHub::setResolver(fn () => $hub);
24+
}
25+
}

src/AdvisoryLocks.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@
99
use Illuminate\Database\QueryException;
1010
use Mpyw\LaravelDatabaseAdvisoryLock\Contracts\LockerFactory as FactoryContract;
1111

12+
/**
13+
* trait AdvisoryLocks
14+
*
15+
* Designed to be mixed in with the Connection classes.
16+
*/
1217
trait AdvisoryLocks
1318
{
1419
public ?FactoryContract $advisoryLocker = null;
1520

21+
/**
22+
* Create LockerFactory or return existing one.
23+
*/
1624
public function advisoryLocker(): FactoryContract
1725
{
1826
assert($this instanceof Connection);
@@ -21,6 +29,8 @@ public function advisoryLocker(): FactoryContract
2129
}
2230

2331
/**
32+
* Overrides the original implementation.
33+
*
2434
* @param string $query
2535
* @param array $bindings
2636
* @throws QueryException
@@ -29,7 +39,7 @@ protected function handleQueryException(QueryException $e, $query, $bindings, Cl
2939
{
3040
assert($this instanceof Connection);
3141

32-
// Don't try again if there are session-level locks
42+
// Don't try again if there are session-level locks.
3343
if ($this->transactionLevel() > 0 || $this->advisoryLocker()->forSession()->hasAny()) {
3444
throw $e;
3545
}

src/Concerns/ReleasesWhenDestructed.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Mpyw\LaravelDatabaseAdvisoryLock\Concerns;
66

7-
/**
8-
* @internal
9-
*/
107
trait ReleasesWhenDestructed
118
{
129
abstract public function release(): bool;

src/Concerns/SessionLocks.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
use Mpyw\LaravelDatabaseAdvisoryLock\Contracts\LockFailedException;
88
use Mpyw\LaravelDatabaseAdvisoryLock\Contracts\SessionLock;
99

10-
/**
11-
* @internal
12-
*/
1310
trait SessionLocks
1411
{
1512
abstract public function lockOrFail(string $key, int $timeout = 0): SessionLock;

src/Concerns/TransactionalLocks.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66

77
use Mpyw\LaravelDatabaseAdvisoryLock\Contracts\LockFailedException;
88

9-
/**
10-
* @internal
11-
*/
129
trait TransactionalLocks
1310
{
1411
public function tryLock(string $key, int $timeout = 0): bool

src/Contracts/InvalidTransactionLevelException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
use BadMethodCallException;
88

9+
/**
10+
* class InvalidTransactionLevelException
11+
*
12+
* You can't use TransactionLocker outside of transaction.
13+
*/
914
class InvalidTransactionLevelException extends BadMethodCallException
1015
{
1116
}

src/Contracts/LockFailedException.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use Illuminate\Database\QueryException;
88
use RuntimeException;
99

10+
/**
11+
* class LockFailedException
12+
*
13+
* Lock acquisition has been failed.
14+
*/
1015
class LockFailedException extends QueryException
1116
{
1217
public function __construct(string $message, string $sql, array $bindings)

src/Contracts/LockerFactory.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,21 @@
44

55
namespace Mpyw\LaravelDatabaseAdvisoryLock\Contracts;
66

7+
/**
8+
* interface LockerFactory
9+
*
10+
* Entrypoint used from the mix-in AdvisoryLocks trait.
11+
* Underlying locker instances are managed as singletons.
12+
*/
713
interface LockerFactory
814
{
15+
/**
16+
* Create a transaction-level locker or return existing one.
17+
*/
918
public function forTransaction(): TransactionLocker;
1019

20+
/**
21+
* Create a session-level locker or return existing one.
22+
*/
1123
public function forSession(): SessionLocker;
1224
}

0 commit comments

Comments
 (0)