Rename app to container (#2609)

* Rename `app` helper to `resolve`, deprecate old version
* Rename $this->app to $this->container in service providers

We no longer couple Flarum\Foundation\Application to the Laravel container; instead, we use the container separately. Changing our naming to reflect that will make things clearer.
This commit is contained in:
Alexander Skvortsov 2021-03-04 22:14:48 -05:00 committed by GitHub
parent c0f3a0ba4b
commit e5c3339a44
29 changed files with 306 additions and 278 deletions

View File

@ -36,18 +36,18 @@ class AdminServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) { $this->container->extend(UrlGenerator::class, function (UrlGenerator $url) {
return $url->addCollection('admin', $this->app->make('flarum.admin.routes'), 'admin'); return $url->addCollection('admin', $this->container->make('flarum.admin.routes'), 'admin');
}); });
$this->app->singleton('flarum.admin.routes', function () { $this->container->singleton('flarum.admin.routes', function () {
$routes = new RouteCollection; $routes = new RouteCollection;
$this->populateRoutes($routes); $this->populateRoutes($routes);
return $routes; return $routes;
}); });
$this->app->singleton('flarum.admin.middleware', function () { $this->container->singleton('flarum.admin.middleware', function () {
return [ return [
'flarum.admin.error_handler', 'flarum.admin.error_handler',
HttpMiddleware\ParseJsonBody::class, HttpMiddleware\ParseJsonBody::class,
@ -61,23 +61,23 @@ class AdminServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->bind('flarum.admin.error_handler', function () { $this->container->bind('flarum.admin.error_handler', function () {
return new HttpMiddleware\HandleErrors( return new HttpMiddleware\HandleErrors(
$this->app->make(Registry::class), $this->container->make(Registry::class),
$this->app['flarum.config']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class), $this->container['flarum.config']->inDebugMode() ? $this->container->make(WhoopsFormatter::class) : $this->container->make(ViewFormatter::class),
$this->app->tagged(Reporter::class) $this->container->tagged(Reporter::class)
); );
}); });
$this->app->bind('flarum.admin.route_resolver', function () { $this->container->bind('flarum.admin.route_resolver', function () {
return new HttpMiddleware\ResolveRoute($this->app->make('flarum.admin.routes')); return new HttpMiddleware\ResolveRoute($this->container->make('flarum.admin.routes'));
}); });
$this->app->singleton('flarum.admin.handler', function () { $this->container->singleton('flarum.admin.handler', function () {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
foreach ($this->app->make('flarum.admin.middleware') as $middleware) { foreach ($this->container->make('flarum.admin.middleware') as $middleware) {
$pipe->pipe($this->app->make($middleware)); $pipe->pipe($this->container->make($middleware));
} }
$pipe->pipe(new HttpMiddleware\ExecuteRoute()); $pipe->pipe(new HttpMiddleware\ExecuteRoute());
@ -85,9 +85,9 @@ class AdminServiceProvider extends AbstractServiceProvider
return $pipe; return $pipe;
}); });
$this->app->bind('flarum.assets.admin', function () { $this->container->bind('flarum.assets.admin', function () {
/** @var \Flarum\Frontend\Assets $assets */ /** @var \Flarum\Frontend\Assets $assets */
$assets = $this->app->make('flarum.assets.factory')('admin'); $assets = $this->container->make('flarum.assets.factory')('admin');
$assets->js(function (SourceCollector $sources) { $assets->js(function (SourceCollector $sources) {
$sources->addFile(__DIR__.'/../../js/dist/admin.js'); $sources->addFile(__DIR__.'/../../js/dist/admin.js');
@ -97,17 +97,17 @@ class AdminServiceProvider extends AbstractServiceProvider
$sources->addFile(__DIR__.'/../../less/admin.less'); $sources->addFile(__DIR__.'/../../less/admin.less');
}); });
$this->app->make(AddTranslations::class)->forFrontend('admin')->to($assets); $this->container->make(AddTranslations::class)->forFrontend('admin')->to($assets);
$this->app->make(AddLocaleAssets::class)->to($assets); $this->container->make(AddLocaleAssets::class)->to($assets);
return $assets; return $assets;
}); });
$this->app->bind('flarum.frontend.admin', function () { $this->container->bind('flarum.frontend.admin', function () {
/** @var \Flarum\Frontend\Frontend $frontend */ /** @var \Flarum\Frontend\Frontend $frontend */
$frontend = $this->app->make('flarum.frontend.factory')('admin'); $frontend = $this->container->make('flarum.frontend.factory')('admin');
$frontend->content($this->app->make(Content\AdminPayload::class)); $frontend->content($this->container->make(Content\AdminPayload::class));
return $frontend; return $frontend;
}); });
@ -120,14 +120,14 @@ class AdminServiceProvider extends AbstractServiceProvider
{ {
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin'); $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.admin');
$events = $this->app->make('events'); $events = $this->container->make('events');
$events->listen( $events->listen(
[Enabled::class, Disabled::class, ClearingCache::class], [Enabled::class, Disabled::class, ClearingCache::class],
function () { function () {
$recompile = new RecompileFrontendAssets( $recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.admin'), $this->container->make('flarum.assets.admin'),
$this->app->make(LocaleManager::class) $this->container->make(LocaleManager::class)
); );
$recompile->flush(); $recompile->flush();
} }
@ -137,8 +137,8 @@ class AdminServiceProvider extends AbstractServiceProvider
Saved::class, Saved::class,
function (Saved $event) { function (Saved $event) {
$recompile = new RecompileFrontendAssets( $recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.admin'), $this->container->make('flarum.assets.admin'),
$this->app->make(LocaleManager::class) $this->container->make(LocaleManager::class)
); );
$recompile->whenSettingsSaved($event); $recompile->whenSettingsSaved($event);
} }
@ -150,7 +150,7 @@ class AdminServiceProvider extends AbstractServiceProvider
*/ */
protected function populateRoutes(RouteCollection $routes) protected function populateRoutes(RouteCollection $routes)
{ {
$factory = $this->app->make(RouteHandlerFactory::class); $factory = $this->container->make(RouteHandlerFactory::class);
$callback = include __DIR__.'/routes.php'; $callback = include __DIR__.'/routes.php';
$callback($routes, $factory); $callback($routes, $factory);

View File

@ -30,18 +30,18 @@ class ApiServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) { $this->container->extend(UrlGenerator::class, function (UrlGenerator $url) {
return $url->addCollection('api', $this->app->make('flarum.api.routes'), 'api'); return $url->addCollection('api', $this->container->make('flarum.api.routes'), 'api');
}); });
$this->app->singleton('flarum.api.routes', function () { $this->container->singleton('flarum.api.routes', function () {
$routes = new RouteCollection; $routes = new RouteCollection;
$this->populateRoutes($routes); $this->populateRoutes($routes);
return $routes; return $routes;
}); });
$this->app->singleton('flarum.api.throttlers', function () { $this->container->singleton('flarum.api.throttlers', function () {
return [ return [
'bypassThrottlingAttribute' => function ($request) { 'bypassThrottlingAttribute' => function ($request) {
if ($request->getAttribute('bypassThrottling')) { if ($request->getAttribute('bypassThrottling')) {
@ -51,11 +51,11 @@ class ApiServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->bind(Middleware\ThrottleApi::class, function ($app) { $this->container->bind(Middleware\ThrottleApi::class, function ($container) {
return new Middleware\ThrottleApi($app->make('flarum.api.throttlers')); return new Middleware\ThrottleApi($container->make('flarum.api.throttlers'));
}); });
$this->app->singleton('flarum.api.middleware', function () { $this->container->singleton('flarum.api.middleware', function () {
return [ return [
'flarum.api.error_handler', 'flarum.api.error_handler',
HttpMiddleware\ParseJsonBody::class, HttpMiddleware\ParseJsonBody::class,
@ -71,23 +71,23 @@ class ApiServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->bind('flarum.api.error_handler', function () { $this->container->bind('flarum.api.error_handler', function () {
return new HttpMiddleware\HandleErrors( return new HttpMiddleware\HandleErrors(
$this->app->make(Registry::class), $this->container->make(Registry::class),
new JsonApiFormatter($this->app['flarum.config']->inDebugMode()), new JsonApiFormatter($this->container['flarum.config']->inDebugMode()),
$this->app->tagged(Reporter::class) $this->container->tagged(Reporter::class)
); );
}); });
$this->app->bind('flarum.api.route_resolver', function () { $this->container->bind('flarum.api.route_resolver', function () {
return new HttpMiddleware\ResolveRoute($this->app->make('flarum.api.routes')); return new HttpMiddleware\ResolveRoute($this->container->make('flarum.api.routes'));
}); });
$this->app->singleton('flarum.api.handler', function () { $this->container->singleton('flarum.api.handler', function () {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
foreach ($this->app->make('flarum.api.middleware') as $middleware) { foreach ($this->container->make('flarum.api.middleware') as $middleware) {
$pipe->pipe($this->app->make($middleware)); $pipe->pipe($this->container->make($middleware));
} }
$pipe->pipe(new HttpMiddleware\ExecuteRoute()); $pipe->pipe(new HttpMiddleware\ExecuteRoute());
@ -95,7 +95,7 @@ class ApiServiceProvider extends AbstractServiceProvider
return $pipe; return $pipe;
}); });
$this->app->singleton('flarum.api.notification_serializers', function () { $this->container->singleton('flarum.api.notification_serializers', function () {
return [ return [
'discussionRenamed' => BasicDiscussionSerializer::class 'discussionRenamed' => BasicDiscussionSerializer::class
]; ];
@ -109,9 +109,9 @@ class ApiServiceProvider extends AbstractServiceProvider
{ {
$this->setNotificationSerializers(); $this->setNotificationSerializers();
AbstractSerializeController::setContainer($this->app); AbstractSerializeController::setContainer($this->container);
AbstractSerializer::setContainer($this->app); AbstractSerializer::setContainer($this->container);
} }
/** /**
@ -119,7 +119,7 @@ class ApiServiceProvider extends AbstractServiceProvider
*/ */
protected function setNotificationSerializers() protected function setNotificationSerializers()
{ {
$serializers = $this->app->make('flarum.api.notification_serializers'); $serializers = $this->container->make('flarum.api.notification_serializers');
foreach ($serializers as $type => $serializer) { foreach ($serializers as $type => $serializer) {
NotificationSerializer::setSubjectSerializer($type, $serializer); NotificationSerializer::setSubjectSerializer($type, $serializer);
@ -133,7 +133,7 @@ class ApiServiceProvider extends AbstractServiceProvider
*/ */
protected function populateRoutes(RouteCollection $routes) protected function populateRoutes(RouteCollection $routes)
{ {
$factory = $this->app->make(RouteHandlerFactory::class); $factory = $this->container->make(RouteHandlerFactory::class);
$callback = include __DIR__.'/routes.php'; $callback = include __DIR__.'/routes.php';
$callback($routes, $factory); $callback($routes, $factory);

View File

@ -19,18 +19,18 @@ class BusServiceProvider extends AbstractServiceProvider
{ {
public function register() public function register()
{ {
$this->app->bind(BaseDispatcher::class, function ($app) { $this->container->bind(BaseDispatcher::class, function ($container) {
return new Dispatcher($app, function ($connection = null) use ($app) { return new Dispatcher($container, function ($connection = null) use ($container) {
return $app[QueueFactoryContract::class]->connection($connection); return $container[QueueFactoryContract::class]->connection($connection);
}); });
}); });
$this->app->alias( $this->container->alias(
BaseDispatcher::class, BaseDispatcher::class,
DispatcherContract::class DispatcherContract::class
); );
$this->app->alias( $this->container->alias(
BaseDispatcher::class, BaseDispatcher::class,
QueueingDispatcherContract::class QueueingDispatcherContract::class
); );

View File

@ -23,7 +23,7 @@ class ConsoleServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.console.commands', function () { $this->container->singleton('flarum.console.commands', function () {
return [ return [
CacheClearCommand::class, CacheClearCommand::class,
GenerateMigrationCommand::class, GenerateMigrationCommand::class,

View File

@ -24,10 +24,10 @@ class DatabaseServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(Manager::class, function ($app) { $this->container->singleton(Manager::class, function ($container) {
$manager = new Manager($app); $manager = new Manager($container);
$config = $this->app['flarum']->config('database'); $config = $this->container['flarum']->config('database');
$config['engine'] = 'InnoDB'; $config['engine'] = 'InnoDB';
$config['prefix_indexes'] = true; $config['prefix_indexes'] = true;
@ -36,8 +36,8 @@ class DatabaseServiceProvider extends AbstractServiceProvider
return $manager; return $manager;
}); });
$this->app->singleton(ConnectionResolverInterface::class, function ($app) { $this->container->singleton(ConnectionResolverInterface::class, function ($container) {
$manager = $app->make(Manager::class); $manager = $container->make(Manager::class);
$manager->setAsGlobal(); $manager->setAsGlobal();
$manager->bootEloquent(); $manager->bootEloquent();
@ -47,22 +47,22 @@ class DatabaseServiceProvider extends AbstractServiceProvider
return $dbManager; return $dbManager;
}); });
$this->app->alias(ConnectionResolverInterface::class, 'db'); $this->container->alias(ConnectionResolverInterface::class, 'db');
$this->app->singleton(ConnectionInterface::class, function ($app) { $this->container->singleton(ConnectionInterface::class, function ($container) {
$resolver = $app->make(ConnectionResolverInterface::class); $resolver = $container->make(ConnectionResolverInterface::class);
return $resolver->connection(); return $resolver->connection();
}); });
$this->app->alias(ConnectionInterface::class, 'db.connection'); $this->container->alias(ConnectionInterface::class, 'db.connection');
$this->app->alias(ConnectionInterface::class, 'flarum.db'); $this->container->alias(ConnectionInterface::class, 'flarum.db');
$this->app->singleton(MigrationRepositoryInterface::class, function ($app) { $this->container->singleton(MigrationRepositoryInterface::class, function ($container) {
return new DatabaseMigrationRepository($app['flarum.db'], 'migrations'); return new DatabaseMigrationRepository($container['flarum.db'], 'migrations');
}); });
$this->app->singleton('flarum.database.model_private_checkers', function () { $this->container->singleton('flarum.database.model_private_checkers', function () {
// Discussion and Post are explicitly listed here to trigger the deprecated // Discussion and Post are explicitly listed here to trigger the deprecated
// event-based model privacy system. They should be removed in beta 17. // event-based model privacy system. They should be removed in beta 17.
return [ return [
@ -77,10 +77,10 @@ class DatabaseServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
AbstractModel::setConnectionResolver($this->app->make(ConnectionResolverInterface::class)); AbstractModel::setConnectionResolver($this->container->make(ConnectionResolverInterface::class));
AbstractModel::setEventDispatcher($this->app->make('events')); AbstractModel::setEventDispatcher($this->container->make('events'));
foreach ($this->app->make('flarum.database.model_private_checkers') as $modelClass => $checkers) { foreach ($this->container->make('flarum.database.model_private_checkers') as $modelClass => $checkers) {
$modelClass::saving(function ($instance) use ($checkers) { $modelClass::saving(function ($instance) use ($checkers) {
foreach ($checkers as $checker) { foreach ($checkers as $checker) {
if ($checker($instance) === true) { if ($checker($instance) === true) {
@ -95,7 +95,7 @@ class DatabaseServiceProvider extends AbstractServiceProvider
// @deprecated BC layer, remove beta 17 // @deprecated BC layer, remove beta 17
$event = new GetModelIsPrivate($instance); $event = new GetModelIsPrivate($instance);
$instance->is_private = $this->app->make('events')->until($event) === true; $instance->is_private = $this->container->make('events')->until($event) === true;
}); });
} }
} }

View File

@ -20,7 +20,7 @@ class DiscussionServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
$events = $this->app->make('events'); $events = $this->container->make('events');
$events->subscribe(DiscussionMetadataUpdater::class); $events->subscribe(DiscussionMetadataUpdater::class);

View File

@ -132,7 +132,7 @@ class Extension implements Arrayable
$this->id = static::nameToId($this->name); $this->id = static::nameToId($this->name);
} }
public function extend(Container $app) public function extend(Container $container)
{ {
foreach ($this->getExtenders() as $extender) { foreach ($this->getExtenders() as $extender) {
// If an extension has not yet switched to the new extend.php // If an extension has not yet switched to the new extend.php
@ -142,7 +142,7 @@ class Extension implements Arrayable
$extender = new Compat($extender); $extender = new Compat($extender);
} }
$extender->extend($app, $this); $extender->extend($container, $this);
} }
} }

View File

@ -341,12 +341,12 @@ class ExtensionManager
/** /**
* Call on all enabled extensions to extend the Flarum application. * Call on all enabled extensions to extend the Flarum application.
* *
* @param Container $app * @param Container $container
*/ */
public function extend(Container $app) public function extend(Container $container)
{ {
foreach ($this->getEnabledExtensions() as $extension) { foreach ($this->getEnabledExtensions() as $extension) {
$extension->extend($app); $extension->extend($container);
} }
} }

View File

@ -19,15 +19,15 @@ class ExtensionServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(ExtensionManager::class); $this->container->singleton(ExtensionManager::class);
$this->app->alias(ExtensionManager::class, 'flarum.extensions'); $this->container->alias(ExtensionManager::class, 'flarum.extensions');
// Boot extensions when the app is booting. This must be done as a boot // Boot extensions when the app is booting. This must be done as a boot
// listener on the app rather than in the service provider's boot method // listener on the app rather than in the service provider's boot method
// below, so that extensions have a chance to register things on the // below, so that extensions have a chance to register things on the
// container before the core boots up (and starts resolving services). // container before the core boots up (and starts resolving services).
$this->app['flarum']->booting(function () { $this->container['flarum']->booting(function () {
$this->app->make('flarum.extensions')->extend($this->app); $this->container->make('flarum.extensions')->extend($this->container);
}); });
} }
@ -36,7 +36,7 @@ class ExtensionServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
$this->app->make('events')->listen( $this->container->make('events')->listen(
Disabling::class, Disabling::class,
DefaultLanguagePackGuard::class DefaultLanguagePackGuard::class
); );

View File

@ -21,13 +21,13 @@ class FormatterServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.formatter', function (Container $container) { $this->container->singleton('flarum.formatter', function (Container $container) {
return new Formatter( return new Formatter(
new Repository($container->make('cache.filestore')), new Repository($container->make('cache.filestore')),
$this->app[Paths::class]->storage.'/formatter' $this->container[Paths::class]->storage.'/formatter'
); );
}); });
$this->app->alias('flarum.formatter', Formatter::class); $this->container->alias('flarum.formatter', Formatter::class);
} }
} }

View File

@ -41,22 +41,22 @@ class ForumServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) { $this->container->extend(UrlGenerator::class, function (UrlGenerator $url) {
return $url->addCollection('forum', $this->app->make('flarum.forum.routes')); return $url->addCollection('forum', $this->container->make('flarum.forum.routes'));
}); });
$this->app->singleton('flarum.forum.routes', function () { $this->container->singleton('flarum.forum.routes', function () {
$routes = new RouteCollection; $routes = new RouteCollection;
$this->populateRoutes($routes); $this->populateRoutes($routes);
return $routes; return $routes;
}); });
$this->app->afterResolving('flarum.forum.routes', function (RouteCollection $routes) { $this->container->afterResolving('flarum.forum.routes', function (RouteCollection $routes) {
$this->setDefaultRoute($routes); $this->setDefaultRoute($routes);
}); });
$this->app->singleton('flarum.forum.middleware', function () { $this->container->singleton('flarum.forum.middleware', function () {
return [ return [
'flarum.forum.error_handler', 'flarum.forum.error_handler',
HttpMiddleware\ParseJsonBody::class, HttpMiddleware\ParseJsonBody::class,
@ -71,23 +71,23 @@ class ForumServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->bind('flarum.forum.error_handler', function () { $this->container->bind('flarum.forum.error_handler', function () {
return new HttpMiddleware\HandleErrors( return new HttpMiddleware\HandleErrors(
$this->app->make(Registry::class), $this->container->make(Registry::class),
$this->app['flarum.config']->inDebugMode() ? $this->app->make(WhoopsFormatter::class) : $this->app->make(ViewFormatter::class), $this->container['flarum.config']->inDebugMode() ? $this->container->make(WhoopsFormatter::class) : $this->container->make(ViewFormatter::class),
$this->app->tagged(Reporter::class) $this->container->tagged(Reporter::class)
); );
}); });
$this->app->bind('flarum.forum.route_resolver', function () { $this->container->bind('flarum.forum.route_resolver', function () {
return new HttpMiddleware\ResolveRoute($this->app->make('flarum.forum.routes')); return new HttpMiddleware\ResolveRoute($this->container->make('flarum.forum.routes'));
}); });
$this->app->singleton('flarum.forum.handler', function () { $this->container->singleton('flarum.forum.handler', function () {
$pipe = new MiddlewarePipe; $pipe = new MiddlewarePipe;
foreach ($this->app->make('flarum.forum.middleware') as $middleware) { foreach ($this->container->make('flarum.forum.middleware') as $middleware) {
$pipe->pipe($this->app->make($middleware)); $pipe->pipe($this->container->make($middleware));
} }
$pipe->pipe(new HttpMiddleware\ExecuteRoute()); $pipe->pipe(new HttpMiddleware\ExecuteRoute());
@ -95,32 +95,32 @@ class ForumServiceProvider extends AbstractServiceProvider
return $pipe; return $pipe;
}); });
$this->app->bind('flarum.assets.forum', function () { $this->container->bind('flarum.assets.forum', function () {
/** @var Assets $assets */ /** @var Assets $assets */
$assets = $this->app->make('flarum.assets.factory')('forum'); $assets = $this->container->make('flarum.assets.factory')('forum');
$assets->js(function (SourceCollector $sources) { $assets->js(function (SourceCollector $sources) {
$sources->addFile(__DIR__.'/../../js/dist/forum.js'); $sources->addFile(__DIR__.'/../../js/dist/forum.js');
$sources->addString(function () { $sources->addString(function () {
return $this->app->make(Formatter::class)->getJs(); return $this->container->make(Formatter::class)->getJs();
}); });
}); });
$assets->css(function (SourceCollector $sources) { $assets->css(function (SourceCollector $sources) {
$sources->addFile(__DIR__.'/../../less/forum.less'); $sources->addFile(__DIR__.'/../../less/forum.less');
$sources->addString(function () { $sources->addString(function () {
return $this->app->make(SettingsRepositoryInterface::class)->get('custom_less', ''); return $this->container->make(SettingsRepositoryInterface::class)->get('custom_less', '');
}); });
}); });
$this->app->make(AddTranslations::class)->forFrontend('forum')->to($assets); $this->container->make(AddTranslations::class)->forFrontend('forum')->to($assets);
$this->app->make(AddLocaleAssets::class)->to($assets); $this->container->make(AddLocaleAssets::class)->to($assets);
return $assets; return $assets;
}); });
$this->app->bind('flarum.frontend.forum', function () { $this->container->bind('flarum.frontend.forum', function () {
return $this->app->make('flarum.frontend.factory')('forum'); return $this->container->make('flarum.frontend.factory')('forum');
}); });
} }
@ -131,19 +131,19 @@ class ForumServiceProvider extends AbstractServiceProvider
{ {
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum'); $this->loadViewsFrom(__DIR__.'/../../views', 'flarum.forum');
$this->app->make('view')->share([ $this->container->make('view')->share([
'translator' => $this->app->make(TranslatorInterface::class), 'translator' => $this->container->make(TranslatorInterface::class),
'settings' => $this->app->make(SettingsRepositoryInterface::class) 'settings' => $this->container->make(SettingsRepositoryInterface::class)
]); ]);
$events = $this->app->make('events'); $events = $this->container->make('events');
$events->listen( $events->listen(
[Enabled::class, Disabled::class, ClearingCache::class], [Enabled::class, Disabled::class, ClearingCache::class],
function () { function () {
$recompile = new RecompileFrontendAssets( $recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.forum'), $this->container->make('flarum.assets.forum'),
$this->app->make(LocaleManager::class) $this->container->make(LocaleManager::class)
); );
$recompile->flush(); $recompile->flush();
} }
@ -153,15 +153,15 @@ class ForumServiceProvider extends AbstractServiceProvider
Saved::class, Saved::class,
function (Saved $event) { function (Saved $event) {
$recompile = new RecompileFrontendAssets( $recompile = new RecompileFrontendAssets(
$this->app->make('flarum.assets.forum'), $this->container->make('flarum.assets.forum'),
$this->app->make(LocaleManager::class) $this->container->make(LocaleManager::class)
); );
$recompile->whenSettingsSaved($event); $recompile->whenSettingsSaved($event);
$validator = new ValidateCustomLess( $validator = new ValidateCustomLess(
$this->app->make('flarum.assets.forum'), $this->container->make('flarum.assets.forum'),
$this->app->make('flarum.locales'), $this->container->make('flarum.locales'),
$this->app $this->container
); );
$validator->whenSettingsSaved($event); $validator->whenSettingsSaved($event);
} }
@ -171,9 +171,9 @@ class ForumServiceProvider extends AbstractServiceProvider
Saving::class, Saving::class,
function (Saving $event) { function (Saving $event) {
$validator = new ValidateCustomLess( $validator = new ValidateCustomLess(
$this->app->make('flarum.assets.forum'), $this->container->make('flarum.assets.forum'),
$this->app->make('flarum.locales'), $this->container->make('flarum.locales'),
$this->app $this->container
); );
$validator->whenSettingsSaving($event); $validator->whenSettingsSaving($event);
} }
@ -187,7 +187,7 @@ class ForumServiceProvider extends AbstractServiceProvider
*/ */
protected function populateRoutes(RouteCollection $routes) protected function populateRoutes(RouteCollection $routes)
{ {
$factory = $this->app->make(RouteHandlerFactory::class); $factory = $this->container->make(RouteHandlerFactory::class);
$callback = include __DIR__.'/routes.php'; $callback = include __DIR__.'/routes.php';
$callback($routes, $factory); $callback($routes, $factory);
@ -200,8 +200,8 @@ class ForumServiceProvider extends AbstractServiceProvider
*/ */
protected function setDefaultRoute(RouteCollection $routes) protected function setDefaultRoute(RouteCollection $routes)
{ {
$factory = $this->app->make(RouteHandlerFactory::class); $factory = $this->container->make(RouteHandlerFactory::class);
$defaultRoute = $this->app->make('flarum.settings')->get('default_route'); $defaultRoute = $this->container->make('flarum.settings')->get('default_route');
if (isset($routes->getRoutes()['GET'][$defaultRoute]['handler'])) { if (isset($routes->getRoutes()['GET'][$defaultRoute]['handler'])) {
$toDefaultController = $routes->getRoutes()['GET'][$defaultRoute]['handler']; $toDefaultController = $routes->getRoutes()['GET'][$defaultRoute]['handler'];

View File

@ -15,16 +15,23 @@ use Illuminate\Support\ServiceProvider;
abstract class AbstractServiceProvider extends ServiceProvider abstract class AbstractServiceProvider extends ServiceProvider
{ {
/** /**
* @deprecated beta 16, remove beta 17
* @var Container * @var Container
*/ */
protected $app; protected $app;
/**
* @var Container
*/
protected $container;
/** /**
* @param Container $container * @param Container $container
*/ */
public function __construct(Container $container) public function __construct(Container $container)
{ {
$this->app = $container; $this->app = $container;
$this->container = $container;
} }
/** /**

View File

@ -135,9 +135,15 @@ class Application
{ {
\Illuminate\Container\Container::setInstance($this->container); \Illuminate\Container\Container::setInstance($this->container);
/**
* @deprecated beta 16, remove beta 17
*/
$this->container->instance('app', $this->container); $this->container->instance('app', $this->container);
$this->container->alias('app', \Illluminate\Container\Container::class); $this->container->alias('app', \Illluminate\Container\Container::class);
$this->container->instance('container', $this->container);
$this->container->alias('container', \Illluminate\Container\Container::class);
$this->container->instance('flarum', $this); $this->container->instance('flarum', $this);
$this->container->alias('flarum', self::class); $this->container->alias('flarum', self::class);

View File

@ -25,7 +25,7 @@ class ErrorServiceProvider extends AbstractServiceProvider
{ {
public function register() public function register()
{ {
$this->app->singleton('flarum.error.statuses', function () { $this->container->singleton('flarum.error.statuses', function () {
return [ return [
// 400 Bad Request // 400 Bad Request
'csrf_token_mismatch' => 400, 'csrf_token_mismatch' => 400,
@ -50,14 +50,14 @@ class ErrorServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->singleton('flarum.error.classes', function () { $this->container->singleton('flarum.error.classes', function () {
return [ return [
InvalidParameterException::class => 'invalid_parameter', InvalidParameterException::class => 'invalid_parameter',
ModelNotFoundException::class => 'not_found', ModelNotFoundException::class => 'not_found',
]; ];
}); });
$this->app->singleton('flarum.error.handlers', function () { $this->container->singleton('flarum.error.handlers', function () {
return [ return [
IlluminateValidationException::class => ExceptionHandler\IlluminateValidationExceptionHandler::class, IlluminateValidationException::class => ExceptionHandler\IlluminateValidationExceptionHandler::class,
ValidationException::class => ExceptionHandler\ValidationExceptionHandler::class, ValidationException::class => ExceptionHandler\ValidationExceptionHandler::class,
@ -66,14 +66,14 @@ class ErrorServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->singleton(Registry::class, function () { $this->container->singleton(Registry::class, function () {
return new Registry( return new Registry(
$this->app->make('flarum.error.statuses'), $this->container->make('flarum.error.statuses'),
$this->app->make('flarum.error.classes'), $this->container->make('flarum.error.classes'),
$this->app->make('flarum.error.handlers') $this->container->make('flarum.error.handlers')
); );
}); });
$this->app->tag(LogReporter::class, Reporter::class); $this->container->tag(LogReporter::class, Reporter::class);
} }
} }

View File

@ -84,13 +84,13 @@ class UninstalledSite implements SiteInterface
UninstalledSettingsRepository::class UninstalledSettingsRepository::class
); );
$container->singleton('view', function ($app) { $container->singleton('view', function ($container) {
$engines = new EngineResolver(); $engines = new EngineResolver();
$engines->register('php', function () { $engines->register('php', function () {
return new PhpEngine(); return new PhpEngine();
}); });
$finder = new FileViewFinder($app->make('files'), []); $finder = new FileViewFinder($container->make('files'), []);
$dispatcher = $app->make(Dispatcher::class); $dispatcher = $container->make(Dispatcher::class);
return new \Illuminate\View\Factory( return new \Illuminate\View\Factory(
$engines, $engines,

View File

@ -20,13 +20,13 @@ class FrontendServiceProvider extends AbstractServiceProvider
{ {
public function register() public function register()
{ {
$this->app->singleton('flarum.assets.factory', function () { $this->container->singleton('flarum.assets.factory', function () {
return function (string $name) { return function (string $name) {
$paths = $this->app[Paths::class]; $paths = $this->container[Paths::class];
$assets = new Assets( $assets = new Assets(
$name, $name,
$this->app->make('filesystem')->disk('flarum-assets'), $this->container->make('filesystem')->disk('flarum-assets'),
$paths->storage $paths->storage
); );
@ -41,17 +41,17 @@ class FrontendServiceProvider extends AbstractServiceProvider
}; };
}); });
$this->app->singleton('flarum.frontend.factory', function () { $this->container->singleton('flarum.frontend.factory', function () {
return function (string $name) { return function (string $name) {
$frontend = $this->app->make(Frontend::class); $frontend = $this->container->make(Frontend::class);
$frontend->content(function (Document $document) use ($name) { $frontend->content(function (Document $document) use ($name) {
$document->layoutView = 'flarum::frontend.'.$name; $document->layoutView = 'flarum::frontend.'.$name;
}); });
$frontend->content($this->app->make(Content\Assets::class)->forFrontend($name)); $frontend->content($this->container->make(Content\Assets::class)->forFrontend($name));
$frontend->content($this->app->make(Content\CorePayload::class)); $frontend->content($this->container->make(Content\CorePayload::class));
$frontend->content($this->app->make(Content\Meta::class)); $frontend->content($this->container->make(Content\Meta::class));
return $frontend; return $frontend;
}; };
@ -65,9 +65,9 @@ class FrontendServiceProvider extends AbstractServiceProvider
{ {
$this->loadViewsFrom(__DIR__.'/../../views', 'flarum'); $this->loadViewsFrom(__DIR__.'/../../views', 'flarum');
$this->app->make(ViewFactory::class)->share([ $this->container->make(ViewFactory::class)->share([
'translator' => $this->app->make('translator'), 'translator' => $this->container->make('translator'),
'url' => $this->app->make(UrlGenerator::class) 'url' => $this->container->make(UrlGenerator::class)
]); ]);
} }
@ -82,7 +82,7 @@ class FrontendServiceProvider extends AbstractServiceProvider
private function addLessVariables(SourceCollector $sources) private function addLessVariables(SourceCollector $sources)
{ {
$sources->addString(function () { $sources->addString(function () {
$settings = $this->app->make(SettingsRepositoryInterface::class); $settings = $this->container->make(SettingsRepositoryInterface::class);
$vars = [ $vars = [
'config-primary-color' => $settings->get('theme_primary_color', '#000'), 'config-primary-color' => $settings->get('theme_primary_color', '#000'),

View File

@ -24,15 +24,15 @@ class HttpServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.http.csrfExemptPaths', function () { $this->container->singleton('flarum.http.csrfExemptPaths', function () {
return ['token']; return ['token'];
}); });
$this->app->bind(Middleware\CheckCsrfToken::class, function ($app) { $this->container->bind(Middleware\CheckCsrfToken::class, function ($container) {
return new Middleware\CheckCsrfToken($app->make('flarum.http.csrfExemptPaths')); return new Middleware\CheckCsrfToken($container->make('flarum.http.csrfExemptPaths'));
}); });
$this->app->singleton('flarum.http.slugDrivers', function () { $this->container->singleton('flarum.http.slugDrivers', function () {
return [ return [
Discussion::class => [ Discussion::class => [
'default' => IdWithTransliteratedSlugDriver::class 'default' => IdWithTransliteratedSlugDriver::class
@ -43,23 +43,23 @@ class HttpServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->singleton('flarum.http.selectedSlugDrivers', function () { $this->container->singleton('flarum.http.selectedSlugDrivers', function () {
$settings = $this->app->make(SettingsRepositoryInterface::class); $settings = $this->container->make(SettingsRepositoryInterface::class);
$compiledDrivers = []; $compiledDrivers = [];
foreach ($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) { foreach ($this->container->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) {
$driverKey = $settings->get("slug_driver_$resourceClass", 'default'); $driverKey = $settings->get("slug_driver_$resourceClass", 'default');
$driverClass = Arr::get($resourceDrivers, $driverKey, $resourceDrivers['default']); $driverClass = Arr::get($resourceDrivers, $driverKey, $resourceDrivers['default']);
$compiledDrivers[$resourceClass] = $this->app->make($driverClass); $compiledDrivers[$resourceClass] = $this->container->make($driverClass);
} }
return $compiledDrivers; return $compiledDrivers;
}); });
$this->app->bind(SlugManager::class, function () { $this->container->bind(SlugManager::class, function () {
return new SlugManager($this->app->make('flarum.http.selectedSlugDrivers')); return new SlugManager($this->container->make('flarum.http.selectedSlugDrivers'));
}); });
} }

View File

@ -20,7 +20,7 @@ class InstallServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.install.routes', function () { $this->container->singleton('flarum.install.routes', function () {
return new RouteCollection; return new RouteCollection;
}); });
} }
@ -32,7 +32,7 @@ class InstallServiceProvider extends AbstractServiceProvider
{ {
$this->loadViewsFrom(__DIR__.'/../../views/install', 'flarum.install'); $this->loadViewsFrom(__DIR__.'/../../views/install', 'flarum.install');
$this->populateRoutes($this->app->make('flarum.install.routes')); $this->populateRoutes($this->container->make('flarum.install.routes'));
} }
/** /**
@ -40,7 +40,7 @@ class InstallServiceProvider extends AbstractServiceProvider
*/ */
protected function populateRoutes(RouteCollection $routes) protected function populateRoutes(RouteCollection $routes)
{ {
$route = $this->app->make(RouteHandlerFactory::class); $route = $this->container->make(RouteHandlerFactory::class);
$routes->get( $routes->get(
'/{path:.*}', '/{path:.*}',

View File

@ -25,7 +25,7 @@ class LocaleServiceProvider extends AbstractServiceProvider
public function boot(Dispatcher $events) public function boot(Dispatcher $events)
{ {
$events->listen(ClearingCache::class, function () { $events->listen(ClearingCache::class, function () {
$this->app->make('flarum.locales')->clearCache(); $this->container->make('flarum.locales')->clearCache();
}); });
} }
@ -34,9 +34,9 @@ class LocaleServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(LocaleManager::class, function () { $this->container->singleton(LocaleManager::class, function () {
$locales = new LocaleManager( $locales = new LocaleManager(
$this->app->make('translator'), $this->container->make('translator'),
$this->getCacheDir() $this->getCacheDir()
); );
@ -45,14 +45,14 @@ class LocaleServiceProvider extends AbstractServiceProvider
return $locales; return $locales;
}); });
$this->app->alias(LocaleManager::class, 'flarum.locales'); $this->container->alias(LocaleManager::class, 'flarum.locales');
$this->app->singleton('translator', function () { $this->container->singleton('translator', function () {
$translator = new Translator( $translator = new Translator(
$this->getDefaultLocale(), $this->getDefaultLocale(),
null, null,
$this->getCacheDir(), $this->getCacheDir(),
$this->app['flarum.debug'] $this->container['flarum.debug']
); );
$translator->setFallbackLocales(['en']); $translator->setFallbackLocales(['en']);
@ -62,20 +62,20 @@ class LocaleServiceProvider extends AbstractServiceProvider
return $translator; return $translator;
}); });
$this->app->alias('translator', Translator::class); $this->container->alias('translator', Translator::class);
$this->app->alias('translator', TranslatorContract::class); $this->container->alias('translator', TranslatorContract::class);
$this->app->alias('translator', TranslatorInterface::class); $this->container->alias('translator', TranslatorInterface::class);
} }
private function getDefaultLocale(): string private function getDefaultLocale(): string
{ {
$repo = $this->app->make(SettingsRepositoryInterface::class); $repo = $this->container->make(SettingsRepositoryInterface::class);
return $repo->get('default_locale', 'en'); return $repo->get('default_locale', 'en');
} }
private function getCacheDir(): string private function getCacheDir(): string
{ {
return $this->app[Paths::class]->storage.'/locale'; return $this->container[Paths::class]->storage.'/locale';
} }
} }

View File

@ -20,7 +20,7 @@ class MailServiceProvider extends AbstractServiceProvider
{ {
public function register() public function register()
{ {
$this->app->singleton('mail.supported_drivers', function () { $this->container->singleton('mail.supported_drivers', function () {
return [ return [
'mail' => SendmailDriver::class, 'mail' => SendmailDriver::class,
'mailgun' => MailgunDriver::class, 'mailgun' => MailgunDriver::class,
@ -29,50 +29,50 @@ class MailServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->singleton('mail.driver', function () { $this->container->singleton('mail.driver', function () {
$configured = $this->app->make('flarum.mail.configured_driver'); $configured = $this->container->make('flarum.mail.configured_driver');
$settings = $this->app->make(SettingsRepositoryInterface::class); $settings = $this->container->make(SettingsRepositoryInterface::class);
$validator = $this->app->make(Factory::class); $validator = $this->container->make(Factory::class);
return $configured->validate($settings, $validator)->any() return $configured->validate($settings, $validator)->any()
? $this->app->make(NullDriver::class) ? $this->container->make(NullDriver::class)
: $configured; : $configured;
}); });
$this->app->alias('mail.driver', DriverInterface::class); $this->container->alias('mail.driver', DriverInterface::class);
$this->app->singleton('flarum.mail.configured_driver', function () { $this->container->singleton('flarum.mail.configured_driver', function () {
$drivers = $this->app->make('mail.supported_drivers'); $drivers = $this->container->make('mail.supported_drivers');
$settings = $this->app->make(SettingsRepositoryInterface::class); $settings = $this->container->make(SettingsRepositoryInterface::class);
$driverName = $settings->get('mail_driver'); $driverName = $settings->get('mail_driver');
$driverClass = Arr::get($drivers, $driverName); $driverClass = Arr::get($drivers, $driverName);
return $driverClass return $driverClass
? $this->app->make($driverClass) ? $this->container->make($driverClass)
: $this->app->make(NullDriver::class); : $this->container->make(NullDriver::class);
}); });
$this->app->singleton('swift.mailer', function ($app) { $this->container->singleton('swift.mailer', function ($container) {
return new Swift_Mailer( return new Swift_Mailer(
$app->make('mail.driver')->buildTransport( $container->make('mail.driver')->buildTransport(
$app->make(SettingsRepositoryInterface::class) $container->make(SettingsRepositoryInterface::class)
) )
); );
}); });
$this->app->singleton('mailer', function ($app) { $this->container->singleton('mailer', function ($container) {
$mailer = new Mailer( $mailer = new Mailer(
$app['view'], $container['view'],
$app['swift.mailer'], $container['swift.mailer'],
$app['events'] $container['events']
); );
if ($app->bound('queue')) { if ($container->bound('queue')) {
$mailer->setQueue($app->make('queue')); $mailer->setQueue($container->make('queue'));
} }
$settings = $app->make(SettingsRepositoryInterface::class); $settings = $container->make(SettingsRepositoryInterface::class);
$mailer->alwaysFrom($settings->get('mail_from'), $settings->get('forum_title')); $mailer->alwaysFrom($settings->get('mail_from'), $settings->get('forum_title'));
return $mailer; return $mailer;

View File

@ -19,14 +19,14 @@ class NotificationServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.notification.drivers', function () { $this->container->singleton('flarum.notification.drivers', function () {
return [ return [
'alert' => Driver\AlertNotificationDriver::class, 'alert' => Driver\AlertNotificationDriver::class,
'email' => Driver\EmailNotificationDriver::class, 'email' => Driver\EmailNotificationDriver::class,
]; ];
}); });
$this->app->singleton('flarum.notification.blueprints', function () { $this->container->singleton('flarum.notification.blueprints', function () {
return [ return [
DiscussionRenamedBlueprint::class => ['alert'] DiscussionRenamedBlueprint::class => ['alert']
]; ];
@ -47,8 +47,8 @@ class NotificationServiceProvider extends AbstractServiceProvider
*/ */
protected function setNotificationDrivers() protected function setNotificationDrivers()
{ {
foreach ($this->app->make('flarum.notification.drivers') as $driverName => $driver) { foreach ($this->container->make('flarum.notification.drivers') as $driverName => $driver) {
NotificationSyncer::addNotificationDriver($driverName, $this->app->make($driver)); NotificationSyncer::addNotificationDriver($driverName, $this->container->make($driver));
} }
} }
@ -57,7 +57,7 @@ class NotificationServiceProvider extends AbstractServiceProvider
*/ */
protected function setNotificationTypes() protected function setNotificationTypes()
{ {
$blueprints = $this->app->make('flarum.notification.blueprints'); $blueprints = $this->container->make('flarum.notification.blueprints');
foreach ($blueprints as $blueprint => $driversEnabledByDefault) { foreach ($blueprints as $blueprint => $driversEnabledByDefault) {
$this->addType($blueprint, $driversEnabledByDefault); $this->addType($blueprint, $driversEnabledByDefault);

View File

@ -20,7 +20,7 @@ class PostServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->extend('flarum.api.throttlers', function ($throttlers) { $this->container->extend('flarum.api.throttlers', function ($throttlers) {
$throttlers['postTimeout'] = function ($request) { $throttlers['postTimeout'] = function ($request) {
if (! in_array($request->getAttribute('routeName'), ['discussions.create', 'posts.create'])) { if (! in_array($request->getAttribute('routeName'), ['discussions.create', 'posts.create'])) {
return; return;
@ -46,7 +46,7 @@ class PostServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
CommentPost::setFormatter($this->app->make('flarum.formatter')); CommentPost::setFormatter($this->container->make('flarum.formatter'));
$this->setPostTypes(); $this->setPostTypes();

View File

@ -41,33 +41,33 @@ class QueueServiceProvider extends AbstractServiceProvider
{ {
// Register a simple connection factory that always returns the same // Register a simple connection factory that always returns the same
// connection, as that is enough for our purposes. // connection, as that is enough for our purposes.
$this->app->singleton(Factory::class, function () { $this->container->singleton(Factory::class, function () {
return new QueueFactory(function () { return new QueueFactory(function () {
return $this->app->make('flarum.queue.connection'); return $this->container->make('flarum.queue.connection');
}); });
}); });
// Extensions can override this binding if they want to make Flarum use // Extensions can override this binding if they want to make Flarum use
// a different queuing backend. // a different queuing backend.
$this->app->singleton('flarum.queue.connection', function ($app) { $this->container->singleton('flarum.queue.connection', function ($container) {
$queue = new SyncQueue; $queue = new SyncQueue;
$queue->setContainer($app); $queue->setContainer($container);
return $queue; return $queue;
}); });
$this->app->singleton(ExceptionHandling::class, function ($app) { $this->container->singleton(ExceptionHandling::class, function ($container) {
return new ExceptionHandler($app['log']); return new ExceptionHandler($container['log']);
}); });
$this->app->singleton(Worker::class, function ($app) { $this->container->singleton(Worker::class, function ($container) {
/** @var Config $config */ /** @var Config $config */
$config = $app->make(Config::class); $config = $container->make(Config::class);
return new Worker( return new Worker(
$app[Factory::class], $container[Factory::class],
$app['events'], $container['events'],
$app[ExceptionHandling::class], $container[ExceptionHandling::class],
function () use ($config) { function () use ($config) {
return $config->inMaintenanceMode(); return $config->inMaintenanceMode();
} }
@ -76,21 +76,21 @@ class QueueServiceProvider extends AbstractServiceProvider
// Override the Laravel native Listener, so that we can ignore the environment // Override the Laravel native Listener, so that we can ignore the environment
// option and force the binary to flarum. // option and force the binary to flarum.
$this->app->singleton(QueueListener::class, function ($app) { $this->container->singleton(QueueListener::class, function ($container) {
return new Listener($app[Paths::class]->base); return new Listener($container[Paths::class]->base);
}); });
// Bind a simple cache manager that returns the cache store. // Bind a simple cache manager that returns the cache store.
$this->app->singleton('cache', function ($app) { $this->container->singleton('cache', function ($container) {
return new class($app) { return new class($container) {
public function __construct($app) public function __construct($container)
{ {
$this->app = $app; $this->container = $container;
} }
public function driver() public function driver()
{ {
return $this->app['cache.store']; return $this->container['cache.store'];
} }
public function __call($name, $arguments) public function __call($name, $arguments)
@ -100,24 +100,24 @@ class QueueServiceProvider extends AbstractServiceProvider
}; };
}); });
$this->app->singleton('queue.failer', function () { $this->container->singleton('queue.failer', function () {
return new NullFailedJobProvider(); return new NullFailedJobProvider();
}); });
$this->app->alias('flarum.queue.connection', Queue::class); $this->container->alias('flarum.queue.connection', Queue::class);
$this->app->alias(ConnectorInterface::class, 'queue.connection'); $this->container->alias(ConnectorInterface::class, 'queue.connection');
$this->app->alias(Factory::class, 'queue'); $this->container->alias(Factory::class, 'queue');
$this->app->alias(Worker::class, 'queue.worker'); $this->container->alias(Worker::class, 'queue.worker');
$this->app->alias(Listener::class, 'queue.listener'); $this->container->alias(Listener::class, 'queue.listener');
$this->registerCommands(); $this->registerCommands();
} }
protected function registerCommands() protected function registerCommands()
{ {
$this->app->extend('flarum.console.commands', function ($commands) { $this->container->extend('flarum.console.commands', function ($commands) {
$queue = $this->app->make(Queue::class); $queue = $this->container->make(Queue::class);
// There is no need to have the queue commands when using the sync driver. // There is no need to have the queue commands when using the sync driver.
if ($queue instanceof SyncQueue) { if ($queue instanceof SyncQueue) {
@ -132,14 +132,14 @@ class QueueServiceProvider extends AbstractServiceProvider
public function boot() public function boot()
{ {
$this->app['events']->listen(JobFailed::class, function (JobFailed $event) { $this->container['events']->listen(JobFailed::class, function (JobFailed $event) {
/** @var Registry $registry */ /** @var Registry $registry */
$registry = $this->app->make(Registry::class); $registry = $this->container->make(Registry::class);
$error = $registry->handle($event->exception); $error = $registry->handle($event->exception);
/** @var Reporter[] $reporters */ /** @var Reporter[] $reporters */
$reporters = $this->app->tagged(Reporter::class); $reporters = $this->container->tagged(Reporter::class);
if ($error->shouldBeReported()) { if ($error->shouldBeReported()) {
foreach ($reporters as $reporter) { foreach ($reporters as $reporter) {

View File

@ -28,14 +28,14 @@ class SearchServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.simple_search.fulltext_gambits', function () { $this->container->singleton('flarum.simple_search.fulltext_gambits', function () {
return [ return [
DiscussionSearcher::class => DiscussionFulltextGambit::class, DiscussionSearcher::class => DiscussionFulltextGambit::class,
UserSearcher::class => UserFulltextGambit::class UserSearcher::class => UserFulltextGambit::class
]; ];
}); });
$this->app->singleton('flarum.simple_search.gambits', function () { $this->container->singleton('flarum.simple_search.gambits', function () {
return [ return [
DiscussionSearcher::class => [ DiscussionSearcher::class => [
DiscussionQuery\AuthorFilterGambit::class, DiscussionQuery\AuthorFilterGambit::class,
@ -50,7 +50,7 @@ class SearchServiceProvider extends AbstractServiceProvider
]; ];
}); });
$this->app->singleton('flarum.simple_search.search_mutators', function () { $this->container->singleton('flarum.simple_search.search_mutators', function () {
return []; return [];
}); });
} }
@ -63,17 +63,17 @@ class SearchServiceProvider extends AbstractServiceProvider
// The rest of these we can resolve in the when->needs->give callback, // The rest of these we can resolve in the when->needs->give callback,
// but we need to resolve at least one regardless so we know which // but we need to resolve at least one regardless so we know which
// searchers we need to register gambits for. // searchers we need to register gambits for.
$fullTextGambits = $this->app->make('flarum.simple_search.fulltext_gambits'); $fullTextGambits = $this->container->make('flarum.simple_search.fulltext_gambits');
foreach ($fullTextGambits as $searcher => $fullTextGambitClass) { foreach ($fullTextGambits as $searcher => $fullTextGambitClass) {
$this->app $this->container
->when($searcher) ->when($searcher)
->needs(GambitManager::class) ->needs(GambitManager::class)
->give(function () use ($searcher, $fullTextGambitClass) { ->give(function () use ($searcher, $fullTextGambitClass) {
$gambitManager = new GambitManager(); $gambitManager = new GambitManager();
$gambitManager->setFulltextGambit($this->app->make($fullTextGambitClass)); $gambitManager->setFulltextGambit($this->container->make($fullTextGambitClass));
foreach (Arr::get($this->app->make('flarum.simple_search.gambits'), $searcher, []) as $gambit) { foreach (Arr::get($this->container->make('flarum.simple_search.gambits'), $searcher, []) as $gambit) {
$gambitManager->add($this->app->make($gambit)); $gambitManager->add($this->container->make($gambit));
} }
// Temporary BC Layer // Temporary BC Layer
@ -87,16 +87,16 @@ class SearchServiceProvider extends AbstractServiceProvider
foreach ($oldEvents as $oldSearcher => $event) { foreach ($oldEvents as $oldSearcher => $event) {
if ($searcher === $oldSearcher) { if ($searcher === $oldSearcher) {
$tempGambits = new GambitManager; $tempGambits = new GambitManager;
$this->app->make('events')->dispatch( $this->container->make('events')->dispatch(
new $event($tempGambits) new $event($tempGambits)
); );
if (! is_null($fullTextGambit = $tempGambits->getFullTextGambit())) { if (! is_null($fullTextGambit = $tempGambits->getFullTextGambit())) {
$gambitManager->setFullTextGambit($this->app->make($fullTextGambit)); $gambitManager->setFullTextGambit($this->container->make($fullTextGambit));
} }
foreach ($tempGambits->getGambits() as $gambit) { foreach ($tempGambits->getGambits() as $gambit) {
$gambitManager->add($this->app->make($gambit)); $gambitManager->add($this->container->make($gambit));
} }
} }
} }
@ -106,14 +106,14 @@ class SearchServiceProvider extends AbstractServiceProvider
return $gambitManager; return $gambitManager;
}); });
$this->app $this->container
->when($searcher) ->when($searcher)
->needs('$searchMutators') ->needs('$searchMutators')
->give(function () use ($searcher) { ->give(function () use ($searcher) {
$searchMutators = Arr::get($this->app->make('flarum.simple_search.search_mutators'), $searcher, []); $searchMutators = Arr::get($this->container->make('flarum.simple_search.search_mutators'), $searcher, []);
return array_map(function ($mutator) { return array_map(function ($mutator) {
return ContainerUtil::wrapCallback($mutator, $this->app); return ContainerUtil::wrapCallback($mutator, $this->container);
}, $searchMutators); }, $searchMutators);
}); });
} }

View File

@ -26,21 +26,21 @@ class SettingsServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(SettingsRepositoryInterface::class, function () { $this->container->singleton(SettingsRepositoryInterface::class, function () {
return new MemoryCacheSettingsRepository( return new MemoryCacheSettingsRepository(
new DatabaseSettingsRepository( new DatabaseSettingsRepository(
$this->app->make(ConnectionInterface::class) $this->container->make(ConnectionInterface::class)
) )
); );
}); });
$this->app->alias(SettingsRepositoryInterface::class, 'flarum.settings'); $this->container->alias(SettingsRepositoryInterface::class, 'flarum.settings');
$assets = function (Container $app) { $assets = function (Container $container) {
return $app->make(Factory::class)->disk('flarum-assets')->getDriver(); return $container->make(Factory::class)->disk('flarum-assets')->getDriver();
}; };
$this->app->when([ $this->container->when([
DeleteFaviconController::class, DeleteFaviconController::class,
DeleteLogoController::class, DeleteLogoController::class,
UploadFaviconController::class, UploadFaviconController::class,

View File

@ -20,7 +20,7 @@ class UpdateServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('flarum.update.routes', function () { $this->container->singleton('flarum.update.routes', function () {
$routes = new RouteCollection; $routes = new RouteCollection;
$this->populateRoutes($routes); $this->populateRoutes($routes);
@ -41,7 +41,7 @@ class UpdateServiceProvider extends AbstractServiceProvider
*/ */
protected function populateRoutes(RouteCollection $routes) protected function populateRoutes(RouteCollection $routes)
{ {
$route = $this->app->make(RouteHandlerFactory::class); $route = $this->container->make(RouteHandlerFactory::class);
$routes->get( $routes->get(
'/{path:.*}', '/{path:.*}',

View File

@ -20,14 +20,14 @@ class SessionServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton('session.handler', function ($app) { $this->container->singleton('session.handler', function ($container) {
return new FileSessionHandler( return new FileSessionHandler(
$app['files'], $container['files'],
$app['config']['session.files'], $container['config']['session.files'],
$app['config']['session.lifetime'] $container['config']['session.lifetime']
); );
}); });
$this->app->alias('session.handler', SessionHandlerInterface::class); $this->container->alias('session.handler', SessionHandlerInterface::class);
} }
} }

View File

@ -40,11 +40,11 @@ class UserServiceProvider extends AbstractServiceProvider
$this->registerDisplayNameDrivers(); $this->registerDisplayNameDrivers();
$this->registerPasswordCheckers(); $this->registerPasswordCheckers();
$this->app->singleton('flarum.user.group_processors', function () { $this->container->singleton('flarum.user.group_processors', function () {
return []; return [];
}); });
$this->app->singleton('flarum.policies', function () { $this->container->singleton('flarum.policies', function () {
return [ return [
Access\AbstractPolicy::GLOBAL => [], Access\AbstractPolicy::GLOBAL => [],
Discussion::class => [DiscussionPolicy::class], Discussion::class => [DiscussionPolicy::class],
@ -57,44 +57,44 @@ class UserServiceProvider extends AbstractServiceProvider
protected function registerDisplayNameDrivers() protected function registerDisplayNameDrivers()
{ {
$this->app->singleton('flarum.user.display_name.supported_drivers', function () { $this->container->singleton('flarum.user.display_name.supported_drivers', function () {
return [ return [
'username' => UsernameDriver::class, 'username' => UsernameDriver::class,
]; ];
}); });
$this->app->singleton('flarum.user.display_name.driver', function () { $this->container->singleton('flarum.user.display_name.driver', function () {
$drivers = $this->app->make('flarum.user.display_name.supported_drivers'); $drivers = $this->container->make('flarum.user.display_name.supported_drivers');
$settings = $this->app->make(SettingsRepositoryInterface::class); $settings = $this->container->make(SettingsRepositoryInterface::class);
$driverName = $settings->get('display_name_driver', ''); $driverName = $settings->get('display_name_driver', '');
$driverClass = Arr::get($drivers, $driverName); $driverClass = Arr::get($drivers, $driverName);
return $driverClass return $driverClass
? $this->app->make($driverClass) ? $this->container->make($driverClass)
: $this->app->make(UsernameDriver::class); : $this->container->make(UsernameDriver::class);
}); });
$this->app->alias('flarum.user.display_name.driver', DriverInterface::class); $this->container->alias('flarum.user.display_name.driver', DriverInterface::class);
} }
protected function registerAvatarsFilesystem() protected function registerAvatarsFilesystem()
{ {
$avatarsFilesystem = function (Container $app) { $avatarsFilesystem = function (Container $container) {
return $app->make(Factory::class)->disk('flarum-avatars')->getDriver(); return $container->make(Factory::class)->disk('flarum-avatars')->getDriver();
}; };
$this->app->when(AvatarUploader::class) $this->container->when(AvatarUploader::class)
->needs(FilesystemInterface::class) ->needs(FilesystemInterface::class)
->give($avatarsFilesystem); ->give($avatarsFilesystem);
} }
protected function registerPasswordCheckers() protected function registerPasswordCheckers()
{ {
$this->app->singleton('flarum.user.password_checkers', function () { $this->container->singleton('flarum.user.password_checkers', function () {
return [ return [
'standard' => function (User $user, $password) { 'standard' => function (User $user, $password) {
if ($this->app->make('hash')->check($password, $user->password)) { if ($this->container->make('hash')->check($password, $user->password)) {
return true; return true;
} }
} }
@ -107,16 +107,16 @@ class UserServiceProvider extends AbstractServiceProvider
*/ */
public function boot() public function boot()
{ {
foreach ($this->app->make('flarum.user.group_processors') as $callback) { foreach ($this->container->make('flarum.user.group_processors') as $callback) {
User::addGroupProcessor(ContainerUtil::wrapCallback($callback, $this->app)); User::addGroupProcessor(ContainerUtil::wrapCallback($callback, $this->container));
} }
User::setPasswordCheckers($this->app->make('flarum.user.password_checkers')); User::setHasher($this->container->make('hash'));
User::setHasher($this->app->make('hash')); User::setPasswordCheckers($this->container->make('flarum.user.password_checkers'));
User::setGate($this->app->makeWith(Access\Gate::class, ['policyClasses' => $this->app->make('flarum.policies')])); User::setGate($this->container->makeWith(Access\Gate::class, ['policyClasses' => $this->container->make('flarum.policies')]));
User::setDisplayNameDriver($this->app->make('flarum.user.display_name.driver')); User::setDisplayNameDriver($this->container->make('flarum.user.display_name.driver'));
$events = $this->app->make('events'); $events = $this->container->make('events');
$events->listen(Saving::class, SelfDemotionGuard::class); $events->listen(Saving::class, SelfDemotionGuard::class);
$events->listen(Registered::class, AccountActivationMailer::class); $events->listen(Registered::class, AccountActivationMailer::class);

View File

@ -9,13 +9,28 @@
use Illuminate\Container\Container; use Illuminate\Container\Container;
if (! function_exists('resolve')) {
/**
* Resolve a service from the container.
*
* @param string $name
* @param array $parameters
* @return mixed
*/
function resolve($name, $parameters = [])
{
return Container::getInstance()->make($name, $parameters);
}
}
if (! function_exists('app')) { if (! function_exists('app')) {
/** /**
* @deprecated beta 16, remove beta 17. Use container() instead.
* Get the available container instance. * Get the available container instance.
* *
* @param string $make * @param string $make
* @param array $parameters * @param array $parameters
* @return mixed|\Illuminate\Foundation\Application * @return mixed|\Illuminate\Container\Container
*/ */
function app($make = null, $parameters = []) function app($make = null, $parameters = [])
{ {
@ -23,7 +38,7 @@ if (! function_exists('app')) {
return Container::getInstance(); return Container::getInstance();
} }
return Container::getInstance()->make($make, $parameters); return resolve($make, $parameters);
} }
} }