Feature: Replace the Illuminate\Foundation\Application with \Illuminate\Container\Container in service provider #3471
Unanswered
macropay-solutions
asked this question in
General
Replies: 2 comments 3 replies
|
The interface |
3 replies
|
Just a note. We moved the register contents from the above provider in the Application's construct and with that we get 23-30 ms responses on local for list and create requests in Maravel, which supports registerExplicitBindingsMap. We also replaced $this->app from the register callbacks to $this: /** $app->resolving prevented calls */
/**
* @see \MongoDB\Laravel\MongoDbServiceProvider
*/
$this->resolvingCallbacks = [
'db' => [
// Add database driver.
function ($db) {
$db->extend('mongodb', function ($config, $name) {
$config['name'] = $name;
return new Connection($config);
});
}
],
SessionManager::class => [
// Session handler for MongoDB
function (SessionManager $sessionManager) {
$sessionManager->extend('mongodb', function (Container $app) {
$connectionName = $app->config->get('session.connection') ?: 'mongodb';
$connection = $app->make('db')->connection($connectionName);
assert($connection instanceof Connection, new \InvalidArgumentException(sprintf(
'The database connection "%s" used for the session does not use the "mongodb" driver.',
$connectionName
)));
return new MongoDbSessionHandler(
$connection,
$app->config->get('session.table', 'sessions'),
$app->config->get('session.lifetime'),
$app,
);
});
},
],
'cache' => [
// Add cache and lock drivers.
function (CacheManager $cache) {
$cache->extend('mongodb', function (Container $app, array $config): \Illuminate\Cache\Repository {
// The closure is bound to the CacheManager
assert($this instanceof CacheManager);
$store = new MongoStore(
$app['db']->connection($config['connection'] ?? null),
$config['collection'] ?? 'cache',
$this->getPrefix($config),
$app['db']->connection($config['lock_connection'] ?? $config['connection'] ?? null),
$config['lock_collection'] ?? ($config['collection'] ?? 'cache') . '_locks',
$config['lock_lottery'] ?? [2, 100],
$config['lock_timeout'] ?? 86400,
);
return $this->repository($store, $config);
});
},
],
'queue' => [
// Add connector for queue support.
function ($queue) {
$queue::addConnector('mongodb', function () { // we have to change from -> to :: because the facade is coming here
return new MongoConnector($this['db']); // notice $this as $this->app from ServiceProvider
});
},
],
'filesystem' => [
// registerFlysystemAdapter
// GridFS adapter for filesystem
static function (FilesystemManager $filesystemManager) {
$filesystemManager->extend('gridfs', static function (Container $app, array $config) {
if (!class_exists(GridFSAdapter::class)) {
throw new \RuntimeException(
'GridFS adapter for Flysystem is missing. ' .
'Try running "composer require league/flysystem-gridfs"'
);
}
$bucket = $config['bucket'] ?? null;
if ($bucket instanceof \Closure) {
// Get the bucket from a factory function
$bucket = $bucket($app, $config);
} elseif (is_string($bucket) && $app->has($bucket)) {
// Get the bucket from a service
$bucket = $app->get($bucket);
} elseif (is_string($bucket) || $bucket === null) {
// Get the bucket from the database connection
$connection = $app['db']->connection($config['connection']);
if (!$connection instanceof Connection) {
throw new \InvalidArgumentException(
sprintf(
'The database connection "%s" does not use the "mongodb" driver.',
$config['connection'] ?? $app['config']['database.default']
)
);
}
$bucket = $connection->getClient()
->getDatabase($config['database'] ?? $connection->getDatabaseName())
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs', 'disableMD5' => true]);
}
if (!$bucket instanceof Bucket) {
throw new \InvalidArgumentException(
sprintf(
'Unexpected value for GridFS "bucket" configuration. Expecting "%s". Got "%s"',
Bucket::class,
get_debug_type($bucket)
)
);
}
$adapter = new GridFSAdapter($bucket, $config['prefix'] ?? '');
/** @see FilesystemManager::createFlysystem() */
if ($config['read-only'] ?? false) {
if (!class_exists(ReadOnlyFilesystemAdapter::class)) {
throw new \RuntimeException(
'Read-only Adapter for Flysystem is missing. ' .
'Try running "composer require league/flysystem-read-only"'
);
}
$adapter = new ReadOnlyFilesystemAdapter($adapter);
}
/** Prevent using backslash on Windows in {@see FilesystemAdapter::__construct()} */
$config['directory_separator'] = '/';
return new FilesystemAdapter(new Filesystem($adapter, $config), $adapter, $config);
});
},
],
EngineManager::class => [
// registerScoutEngine
function (EngineManager $engineManager) {
$engineManager->extend('mongodb', function (Container $app) {
$connectionName = $app->get('config')->get('scout.mongodb.connection', 'mongodb');
$connection = $app->get('db')->connection($connectionName);
$softDelete = (bool)$app->get('config')->get('scout.soft_delete', false);
$indexDefinitions = $app->get('config')->get('scout.mongodb.index-definitions', []);
assert(
$connection instanceof Connection,
new \InvalidArgumentException(
sprintf('The connection "%s" is not a MongoDB connection.', $connectionName)
)
);
return new ScoutEngine($connection->getDatabase(), $softDelete, $indexDefinitions);
});
return $engineManager;
},
],
]; |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
We had to replace the \MongoDB\Laravel\MongoDBServiceProvider to remove the Illuminate\Foundation\Application type hint so we can use it in Maravel (improvement of Lumen) which implements the Illuminate\Contracts\Foundation\Application unlike Lumen.
If you want, you can change the Illuminate\Foundation\Application with Illuminate\Contracts\Foundation\Application or remove it so it can be used also with Lumen (we know it is deprecated, that is why Maravel was created).
We also noticed that other Foundation FQNs are used:
But those can be avoided in Lumen/Maravel.
All reactions