Skip to content

Commit db6a06a

Browse files
daniserSergey Danilchenko
andauthored
[13.x] Bind manager instances to custom driver closures (#57173)
* [13.x] Bind manager instances to custom driver closures * fix test --------- Co-authored-by: Sergey Danilchenko <[email protected]>
1 parent 69a7220 commit db6a06a

File tree

12 files changed

+90
-14
lines changed

12 files changed

+90
-14
lines changed

src/Illuminate/Auth/AuthManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,14 @@ public function resolveUsersUsing(Closure $userResolver)
263263
*
264264
* @param string $driver
265265
* @param \Closure $callback
266+
*
267+
* @param-closure-this $this $callback
268+
*
266269
* @return $this
267270
*/
268271
public function extend($driver, Closure $callback)
269272
{
270-
$this->customCreators[$driver] = $callback;
273+
$this->customCreators[$driver] = $callback->bindTo($this, $this);
271274

272275
return $this;
273276
}

src/Illuminate/Broadcasting/BroadcastManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,11 +476,14 @@ public function purge($name = null)
476476
*
477477
* @param string $driver
478478
* @param \Closure $callback
479+
*
480+
* @param-closure-this $this $callback
481+
*
479482
* @return $this
480483
*/
481484
public function extend($driver, Closure $callback)
482485
{
483-
$this->customCreators[$driver] = $callback;
486+
$this->customCreators[$driver] = $callback->bindTo($this, $this);
484487

485488
return $this;
486489
}

src/Illuminate/Filesystem/FilesystemManager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,14 @@ public function purge($name = null)
430430
*
431431
* @param string $driver
432432
* @param \Closure $callback
433+
*
434+
* @param-closure-this $this $callback
435+
*
433436
* @return $this
434437
*/
435438
public function extend($driver, Closure $callback)
436439
{
437-
$this->customCreators[$driver] = $callback;
440+
$this->customCreators[$driver] = $callback->bindTo($this, $this);
438441

439442
return $this;
440443
}

src/Illuminate/Support/Manager.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,14 @@ protected function callCustomCreator($driver)
120120
*
121121
* @param string $driver
122122
* @param \Closure $callback
123+
*
124+
* @param-closure-this $this $callback
125+
*
123126
* @return $this
124127
*/
125128
public function extend($driver, Closure $callback)
126129
{
127-
$this->customCreators[$driver] = $callback;
130+
$this->customCreators[$driver] = $callback->bindTo($this, $this);
128131

129132
return $this;
130133
}

tests/Auth/AuthenticateMiddlewareTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ public function testMultipleDriversAuthenticatedUpdatesDefault()
148148
$this->assertSame($secondary, $this->auth->guard());
149149
}
150150

151+
public function testCustomDriverClosureBoundObjectIsAuthManager()
152+
{
153+
$this->auth->extend(__CLASS__, fn () => $this);
154+
$this->assertSame($this->auth, $this->auth->guard(__CLASS__));
155+
}
156+
151157
/**
152158
* Create a new config repository instance.
153159
*
@@ -161,6 +167,7 @@ protected function createConfig()
161167
'guards' => [
162168
'default' => ['driver' => 'default'],
163169
'secondary' => ['driver' => 'secondary'],
170+
__CLASS__ => ['driver' => __CLASS__],
164171
],
165172
],
166173
]);

tests/Cache/CacheManagerTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,17 @@ protected function tearDown(): void
2222

2323
public function testCustomDriverClosureBoundObjectIsCacheManager()
2424
{
25-
$cacheManager = new CacheManager([
26-
'config' => [
27-
'cache.stores.'.__CLASS__ => [
28-
'driver' => __CLASS__,
25+
$manager = new CacheManager($this->getApp([
26+
'cache' => [
27+
'stores' => [
28+
__CLASS__ => [
29+
'driver' => __CLASS__,
30+
],
2931
],
3032
],
31-
]);
32-
$driver = function () {
33-
return $this;
34-
};
35-
$cacheManager->extend(__CLASS__, $driver);
36-
$this->assertEquals($cacheManager, $cacheManager->store(__CLASS__));
33+
]));
34+
$manager->extend(__CLASS__, fn () => $this);
35+
$this->assertSame($manager, $manager->store(__CLASS__));
3736
}
3837

3938
public function testCustomDriverOverridesInternalDrivers()

tests/Filesystem/FilesystemManagerTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ public function testCanBuildInlineScopedDisks()
217217
}
218218
}
219219

220+
public function testCustomDriverClosureBoundObjectIsFilesystemManager()
221+
{
222+
$manager = new FilesystemManager(tap(new Application, function ($app) {
223+
$app['config'] = [
224+
'filesystems.disks.'.__CLASS__ => [
225+
'driver' => __CLASS__,
226+
],
227+
];
228+
}));
229+
$manager->extend(__CLASS__, fn () => $this);
230+
$this->assertSame($manager, $manager->disk(__CLASS__));
231+
}
232+
220233
// public function testKeepTrackOfAdapterDecoration()
221234
// {
222235
// try {

tests/Integration/Broadcasting/BroadcastManagerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ public function testThrowExceptionWhenUnknownStoreIsUsed()
100100
$broadcastManager->connection('alien_connection');
101101
}
102102

103+
public function testCustomDriverClosureBoundObjectIsBroadcastManager()
104+
{
105+
$manager = new BroadcastManager($this->getApp([
106+
'broadcasting' => [
107+
'connections' => [
108+
__CLASS__ => [
109+
'driver' => __CLASS__,
110+
],
111+
],
112+
],
113+
]));
114+
$manager->extend(__CLASS__, fn () => $this);
115+
$this->assertSame($manager, $manager->connection(__CLASS__));
116+
}
117+
103118
protected function getApp(array $userConfig)
104119
{
105120
$app = new Container;

tests/Integration/Support/Fixtures/MultipleInstanceManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ public function getInstanceConfig($name)
8989
'driver' => 'mysql_database-connection',
9090
'mysql_database-connection-option' => 'option-value',
9191
];
92+
case 'custom':
93+
return [
94+
'driver' => 'custom',
95+
];
9296
default:
9397
return [];
9498
}

tests/Integration/Support/ManagerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,11 @@ public function testDefaultDriverCannotBeNull()
1414

1515
(new NullableManager($this->app))->driver();
1616
}
17+
18+
public function testCustomDriverClosureBoundObjectIsManager()
19+
{
20+
$manager = new NullableManager($this->app);
21+
$manager->extend(__CLASS__, fn () => $this);
22+
$this->assertSame($manager, $manager->driver(__CLASS__));
23+
}
1724
}

0 commit comments

Comments
 (0)