Load per-site extenders, if available

Closes #1559.
This commit is contained in:
Franz Liedke 2018-09-01 16:16:03 +02:00
parent d7c283a48f
commit 5f5af894ab
No known key found for this signature in database
GPG Key ID: 9A0231A879B055F4
2 changed files with 33 additions and 2 deletions

View File

@ -69,6 +69,11 @@ class InstalledSite implements SiteInterface
*/
protected $config;
/**
* @var \Flarum\Extend\ExtenderInterface[]
*/
protected $extenders = [];
public function __construct($basePath, $publicPath, array $config)
{
$this->basePath = $basePath;
@ -100,6 +105,17 @@ class InstalledSite implements SiteInterface
return $this;
}
/**
* @param \Flarum\Extend\ExtenderInterface[] $extenders
* @return InstalledSite
*/
public function extendWith(array $extenders): self
{
$this->extenders = $extenders;
return $this;
}
protected function bootLaravel(): Application
{
$laravel = new Application($this->basePath, $this->publicPath);
@ -156,6 +172,10 @@ class InstalledSite implements SiteInterface
$laravel->boot();
foreach ($this->extenders as $extension) {
$extension($laravel);
}
return $laravel;
}

View File

@ -35,11 +35,11 @@ class Site
date_default_timezone_set('UTC');
if (static::hasConfigFile($paths['base'])) {
return new InstalledSite(
return (new InstalledSite(
$paths['base'],
$paths['public'],
static::loadConfig($paths['base'])
);
))->extendWith(static::loadExtenders($paths['base']));
} else {
return new UninstalledSite($paths['base'], $paths['public']);
}
@ -60,4 +60,15 @@ class Site
return $config;
}
private static function loadExtenders($basePath): array
{
$extenderFile = "$basePath/extend.php";
if (!file_exists($extenderFile)) {
return [];
}
return array_flatten(require $extenderFile);
}
}