Skip to content

Commit

Permalink
[6.x] Add fallback when facade root accessor has previously been reso…
Browse files Browse the repository at this point in the history
…lved. (#30616)

* [6.x] Add fallback when facade root accessor has previously been
resolved.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Use === true and reuse declared variable.

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>

* Fixes StyleCI

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
  • Loading branch information
crynobone authored and taylorotwell committed Nov 18, 2019
1 parent 899b8c3 commit 9afd29d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Support/Facades/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,13 @@ abstract class Facade
*/
public static function resolved(Closure $callback)
{
static::$app->afterResolving(static::getFacadeAccessor(), function ($service) use ($callback) {
$accessor = static::getFacadeAccessor();

if (static::$app->resolved($accessor) === true) {
$callback(static::getFacadeRoot());
}

static::$app->afterResolving($accessor, function ($service) use ($callback) {
$callback($service);
});
}
Expand Down
42 changes: 42 additions & 0 deletions tests/Integration/Support/FacadesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Illuminate\Tests\Integration\Support;

use Illuminate\Support\Facades\Auth;
use Orchestra\Testbench\TestCase;

class FacadesTest extends TestCase
{
protected function tearDown(): void
{
parent::tearDown();

unset($_SERVER['__laravel.authResolved']);
}

public function testFacadeResolvedCanResolveCallback()
{
Auth::resolved(function () {
$_SERVER['__laravel.authResolved'] = true;
});

$this->assertFalse(isset($_SERVER['__laravel.authResolved']));

$this->app->make('auth');

$this->assertTrue(isset($_SERVER['__laravel.authResolved']));
}

public function testFacadeResolvedCanResolveCallbackAfterAccessRootHasBeenResolved()
{
$this->app->make('auth');

$this->assertFalse(isset($_SERVER['__laravel.authResolved']));

Auth::resolved(function () {
$_SERVER['__laravel.authResolved'] = true;
});

$this->assertTrue(isset($_SERVER['__laravel.authResolved']));
}
}

0 comments on commit 9afd29d

Please sign in to comment.