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

chore: merge the app with the container & implement the ApplicationContract #3862

Merged
merged 8 commits into from
Sep 15, 2023
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
19 changes: 6 additions & 13 deletions framework/core/src/Foundation/AbstractServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,17 @@

namespace Flarum\Foundation;

use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Foundation\Application as ApplicationContract;
use Illuminate\Support\ServiceProvider;

abstract class AbstractServiceProvider extends ServiceProvider
{
/**
* @deprecated perpetually, not removed because Laravel needs it.
* @var Container
*/
protected $app;
protected ApplicationContract $container;

public function __construct(
protected Container $container
) {
parent::__construct($container);
}

public function register()
public function __construct(ApplicationContract $app)
{
parent::__construct($app);

$this->container = $app;
}
}
71 changes: 25 additions & 46 deletions framework/core/src/Foundation/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@

namespace Flarum\Foundation;

use Illuminate\Contracts\Container\Container;
use Flarum\Foundation\Concerns\InteractsWithLaravel;
use Illuminate\Container\Container as IlluminateContainer;
use Illuminate\Contracts\Foundation\Application as LaravelApplication;
use Illuminate\Events\EventServiceProvider;
use Illuminate\Support\Arr;
use Illuminate\Support\ServiceProvider;

class Application
class Application extends IlluminateContainer implements LaravelApplication
{
use InteractsWithLaravel;

/**
* The Flarum version.
*
Expand All @@ -34,7 +38,6 @@ class Application
protected array $loadedProviders = [];

public function __construct(
private readonly Container $container,
protected Paths $paths
) {
$this->registerBaseBindings();
Expand All @@ -44,19 +47,14 @@ public function __construct(

public function config(string $key, mixed $default = null): mixed
{
$config = $this->container->make('flarum.config');
$config = $this->make('flarum.config');

return $config[$key] ?? $default;
}

public function inDebugMode(): bool
{
return $this->config('debug', true);
}

public function url(string $path = null): string
{
$config = $this->container->make('flarum.config');
$config = $this->make('flarum.config');
$url = (string) $config->url();

if ($path) {
Expand All @@ -68,31 +66,27 @@ public function url(string $path = null): string

protected function registerBaseBindings(): void
{
\Illuminate\Container\Container::setInstance($this->container);
IlluminateContainer::setInstance($this);

/**
* Needed for the laravel framework code.
* Use container inside flarum instead.
*/
$this->container->instance('app', $this->container);
$this->container->alias('app', \Illuminate\Container\Container::class);
$this->instance('app', $this);
$this->alias('app', IlluminateContainer::class);

$this->container->instance('container', $this->container);
$this->container->alias('container', \Illuminate\Container\Container::class);
$this->instance('container', $this);
$this->alias('container', IlluminateContainer::class);

$this->container->instance('flarum', $this);
$this->container->alias('flarum', self::class);
$this->instance('flarum', $this);
$this->alias('flarum', self::class);

$this->container->instance('flarum.paths', $this->paths);
$this->container->alias('flarum.paths', Paths::class);
$this->instance('flarum.paths', $this->paths);
$this->alias('flarum.paths', Paths::class);
}

protected function registerBaseServiceProviders(): void
{
$this->register(new EventServiceProvider($this->container));
$this->register(new EventServiceProvider($this));
}

public function register(string|ServiceProvider $provider, array $options = [], bool $force = false): ServiceProvider
public function register($provider, $force = false): ServiceProvider
{
if (($registered = $this->getProvider($provider)) && ! $force) {
return $registered;
Expand All @@ -102,18 +96,11 @@ public function register(string|ServiceProvider $provider, array $options = [],
// application instance automatically for the developer. This is simply
// a more convenient way of specifying your service provider classes.
if (is_string($provider)) {
$provider = $this->resolveProviderClass($provider);
$provider = $this->resolveProvider($provider);
}

$provider->register();

// Once we have registered the service we will iterate through the options
// and set each of them on the application so they will be available on
// the actual loading of the service objects and for developer usage.
foreach ($options as $key => $value) {
$this[$key] = $value;
}

$this->markAsRegistered($provider);

// If the application has already booted, we will call this boot method on
Expand All @@ -140,14 +127,14 @@ public function getProvider(string|ServiceProvider $provider): ?ServiceProvider
*
* @param class-string<ServiceProvider> $provider
*/
public function resolveProviderClass(string $provider): ServiceProvider
public function resolveProvider($provider): ServiceProvider
{
return new $provider($this->container);
return new $provider($this);
}

protected function markAsRegistered(ServiceProvider $provider): void
{
$this->container['events']->dispatch($class = get_class($provider), [$provider]);
$this['events']->dispatch($class = get_class($provider), [$provider]);

$this->serviceProviders[] = $provider;

Expand Down Expand Up @@ -182,7 +169,7 @@ public function boot(): void
protected function bootProvider(ServiceProvider $provider): mixed
{
if (method_exists($provider, 'boot')) {
return $this->container->call([$provider, 'boot']);
return $this->call([$provider, 'boot']);
}

return null;
Expand Down Expand Up @@ -232,7 +219,7 @@ public function registerCoreContainerAliases(): void

foreach ($aliases as $key => $aliasGroup) {
foreach ($aliasGroup as $alias) {
$this->container->alias($key, $alias);
$this->alias($key, $alias);
}
}
}
Expand All @@ -241,12 +228,4 @@ public function version(): string
{
return static::VERSION;
}

public function terminating(): void
{
}

public function terminate(): void
{
}
}
Loading