Skip to content

Commit

Permalink
chore: merge the app with the container & implement the ApplicationCo…
Browse files Browse the repository at this point in the history
…ntract (#3862)

* chore: merge the app with the container & implement the ApplicationContract

Illuminate components always expect the app to be the container, but also expect the app to be implementing the laravel app contract. This means that very often between minor illuminate updates we get a call to a method on the app that doesn't exist in the Flarum app. This fixes the issue once and for all.

* chore: improve concern implementation readability
* chore: service provider no longer has to change app type
* chore: unimplement `terminat(e/ing)`
* Apply fixes from StyleCI
* chore: recover `container` prop
* chore: return types
* fix: phpstan errors
  • Loading branch information
SychO9 authored Sep 15, 2023
1 parent 23fdddf commit ec5cb98
Show file tree
Hide file tree
Showing 9 changed files with 332 additions and 157 deletions.
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

0 comments on commit ec5cb98

Please sign in to comment.