Skip to content
This repository was archived by the owner on Apr 13, 2022. It is now read-only.

Commit aea0fb9

Browse files
Lazy connections
1 parent ed18fcd commit aea0fb9

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
},
4242
"extra": {
4343
"branch-alias": {
44-
"dev-master": "6.0-dev"
44+
"dev-master": "6.1-dev"
4545
},
4646
"laravel": {
4747
"providers": [

src/Locker.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ class Locker
2828
/**
2929
* The connection instance.
3030
*
31-
* @var \AltThree\Locker\Connections\ConnectionInterface
31+
* @var \AltThree\Locker\Connections\ConnectionInterface|callable
3232
*/
3333
protected $connection;
3434

3535
/**
3636
* Create a new locker instance.
3737
*
38-
* @param \AltThree\Locker\Connections\ConnectionInterface $connection
38+
* @param \AltThree\Locker\Connections\ConnectionInterface|callable $connection
3939
*
4040
* @return void
4141
*/
42-
public function __construct(ConnectionInterface $connection)
42+
public function __construct($connection)
4343
{
4444
$this->connection = $connection;
4545
}
@@ -59,7 +59,7 @@ public function __construct(ConnectionInterface $connection)
5959
*/
6060
public function make(string $name, int $timeout, int $play = 500, int $interval = 100, int $attempts = 128)
6161
{
62-
return new Lock($this->connection, $name, $timeout, $play, $interval, $attempts);
62+
return new Lock($this->resolveConnection(), $name, $timeout, $play, $interval, $attempts);
6363
}
6464

6565
/**
@@ -95,4 +95,20 @@ public function execute(Closure $function, string $name, int $timeout, int $play
9595

9696
return $result;
9797
}
98+
99+
/**
100+
* Resolve the connection instance.
101+
*
102+
* @return \AltThree\Locker\Connections\ConnectionInterface
103+
*/
104+
protected function resolveConnection()
105+
{
106+
if ($this->connection instanceof ConnectionInterface) {
107+
return $this->connection;
108+
}
109+
110+
$c = $this->connection;
111+
112+
return $this->connection = $c();
113+
}
98114
}

src/LockerServiceProvider.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ protected function registerConnection()
9696
protected function registerLocker()
9797
{
9898
$this->app->singleton('locker', function (Container $app) {
99-
$connection = $app['locker.connection'];
99+
$connection = function () use ($app) {
100+
return $app['locker.connection'];
101+
};
100102

101103
return new Locker($connection);
102104
});

0 commit comments

Comments
 (0)