diff --git a/src/Illuminate/Database/DatabaseServiceProvider.php b/src/Illuminate/Database/DatabaseServiceProvider.php index 9f2ab18503e1..f22b71ce3285 100755 --- a/src/Illuminate/Database/DatabaseServiceProvider.php +++ b/src/Illuminate/Database/DatabaseServiceProvider.php @@ -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; }); diff --git a/src/Illuminate/Foundation/Application.php b/src/Illuminate/Foundation/Application.php index 1759d6cfb5e0..21fcbacc6ebb 100755 --- a/src/Illuminate/Foundation/Application.php +++ b/src/Illuminate/Foundation/Application.php @@ -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], diff --git a/src/Illuminate/Support/Facades/Facade.php b/src/Illuminate/Support/Facades/Facade.php index cb11ba7cfbfd..1eb01f4eb1c5 100755 --- a/src/Illuminate/Support/Facades/Facade.php +++ b/src/Illuminate/Support/Facades/Facade.php @@ -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. * @@ -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()); } @@ -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]; } } diff --git a/src/Illuminate/Support/Facades/RateLimiter.php b/src/Illuminate/Support/Facades/RateLimiter.php index 23b1a31e1538..1c8b6e3ce7b1 100644 --- a/src/Illuminate/Support/Facades/RateLimiter.php +++ b/src/Illuminate/Support/Facades/RateLimiter.php @@ -24,6 +24,6 @@ class RateLimiter extends Facade */ protected static function getFacadeAccessor() { - return 'Illuminate\Cache\RateLimiter'; + return \Illuminate\Cache\RateLimiter::class; } } diff --git a/src/Illuminate/Support/Facades/Schema.php b/src/Illuminate/Support/Facades/Schema.php index 896e1764cd83..8b4f821f5944 100755 --- a/src/Illuminate/Support/Facades/Schema.php +++ b/src/Illuminate/Support/Facades/Schema.php @@ -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. * @@ -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'; } } diff --git a/tests/Database/DatabaseMigratorIntegrationTest.php b/tests/Database/DatabaseMigratorIntegrationTest.php index 3ee9815cadcf..060482d52d68 100644 --- a/tests/Database/DatabaseMigratorIntegrationTest.php +++ b/tests/Database/DatabaseMigratorIntegrationTest.php @@ -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);