Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Use string based accessor for Schema facade #38017

Merged
merged 3 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Illuminate/Database/DatabaseServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ protected function registerConnectionServices()
return $app['db']->connection();
});

$this->app->bind('db.schema', function ($app) {
return $app['db']->connection()->getSchemaBuilder();
});

$this->app->singleton('db.transactions', function ($app) {
return new DatabaseTransactionsManager;
});
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -1304,6 +1304,7 @@ public function registerCoreContainerAliases()
'cookie' => [\Illuminate\Cookie\CookieJar::class, \Illuminate\Contracts\Cookie\Factory::class, \Illuminate\Contracts\Cookie\QueueingFactory::class],
'db' => [\Illuminate\Database\DatabaseManager::class, \Illuminate\Database\ConnectionResolverInterface::class],
'db.connection' => [\Illuminate\Database\Connection::class, \Illuminate\Database\ConnectionInterface::class],
'db.schema' => [\Illuminate\Database\Schema\Builder::class],
'encrypter' => [\Illuminate\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\Encrypter::class, \Illuminate\Contracts\Encryption\StringEncrypter::class],
'events' => [\Illuminate\Events\Dispatcher::class, \Illuminate\Contracts\Events\Dispatcher::class],
'files' => [\Illuminate\Filesystem\Filesystem::class],
Expand Down
23 changes: 15 additions & 8 deletions src/Illuminate/Support/Facades/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ abstract class Facade
*/
protected static $resolvedInstance;

/**
* Determine if the resolved instance should be cached.
*
* @var bool
*/
protected static $cached = true;

/**
* Run a Closure when the facade has been resolved.
*
Expand Down Expand Up @@ -84,8 +91,8 @@ public static function shouldReceive()
$name = static::getFacadeAccessor();

$mock = static::isMock()
? static::$resolvedInstance[$name]
: static::createFreshMockInstance();
? static::$resolvedInstance[$name]
: static::createFreshMockInstance();

return $mock->shouldReceive(...func_get_args());
}
Expand Down Expand Up @@ -197,21 +204,21 @@ protected static function getFacadeAccessor()
/**
* Resolve the facade root instance from the container.
*
* @param object|string $name
* @param string $name
* @return mixed
*/
protected static function resolveFacadeInstance($name)
{
if (is_object($name)) {
return $name;
}

if (isset(static::$resolvedInstance[$name])) {
return static::$resolvedInstance[$name];
}

if (static::$app) {
return static::$resolvedInstance[$name] = static::$app[$name];
if (static::$cached) {
return static::$resolvedInstance[$name] = static::$app[$name];
}

return static::$app[$name];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Support/Facades/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ class RateLimiter extends Facade
*/
protected static function getFacadeAccessor()
{
return 'Illuminate\Cache\RateLimiter';
return \Illuminate\Cache\RateLimiter::class;
}
}
13 changes: 10 additions & 3 deletions src/Illuminate/Support/Facades/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
*/
class Schema extends Facade
{
/**
* Determine if the resolved facade should be cached.
*
* @var bool
*/
protected static $cached = false;

/**
* Get a schema builder instance for a connection.
*
Expand All @@ -36,12 +43,12 @@ public static function connection($name)
}

/**
* Get a schema builder instance for the default connection.
* Get the registered name of the component.
*
* @return \Illuminate\Database\Schema\Builder
* @return string
*/
protected static function getFacadeAccessor()
{
return static::$app['db']->connection()->getSchemaBuilder();
return 'db.schema';
}
}
3 changes: 3 additions & 0 deletions tests/Database/DatabaseMigratorIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ protected function setUp(): void

$container = new Container;
$container->instance('db', $db->getDatabaseManager());
$container->bind('db.schema', function ($app) {
return $app['db']->connection()->getSchemaBuilder();
});

Facade::setFacadeApplication($container);

Expand Down