1
0
mirror of https://github.com/flarum/core.git synced 2025-07-23 09:41:26 +02:00

Merge pull request #841 from Luceos/drop-ext-dir

Refactoring to drop extensions dir, see #774
This commit is contained in:
Toby Zerner
2016-03-02 18:46:08 +10:30
3 changed files with 87 additions and 38 deletions

View File

@@ -96,8 +96,9 @@ class Extension implements Arrayable
*/ */
protected function assignId() protected function assignId()
{ {
$segments = explode('/', $this->path); list($vendor, $package) = explode('/', $this->name);
$this->id = end($segments); $package = str_replace(['flarum-ext-', 'flarum-'], '', $package);
$this->id = "$vendor-$package";
} }
/** /**

View File

@@ -18,6 +18,7 @@ use Flarum\Foundation\Application;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem; use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
class ExtensionManager class ExtensionManager
@@ -38,6 +39,11 @@ class ExtensionManager
*/ */
protected $filesystem; protected $filesystem;
/**
* @var Collection|null
*/
protected $extensions;
public function __construct( public function __construct(
SettingsRepositoryInterface $config, SettingsRepositoryInterface $config,
Application $app, Application $app,
@@ -57,39 +63,32 @@ class ExtensionManager
*/ */
public function getExtensions() public function getExtensions()
{ {
$extensionsDir = $this->getExtensionsDir(); if (is_null($this->extensions) && $this->filesystem->exists(public_path('vendor/composer/installed.json'))) {
$extensions = new Collection();
$dirs = array_diff(scandir($extensionsDir), ['.', '..']); // Load all packages installed by composer.
$extensions = new Collection(); $installed = json_decode($this->filesystem->get(public_path('vendor/composer/installed.json')), true);
$installed = json_decode(file_get_contents(public_path('vendor/composer/installed.json')), true); foreach ($installed as $package) {
if (Arr::get($package, 'type') != 'flarum-extension' || empty(Arr::get($package, 'name'))) {
foreach ($dirs as $dir) {
if (file_exists($manifest = $extensionsDir.'/'.$dir.'/composer.json')) {
$extension = new Extension(
$extensionsDir.'/'.$dir,
json_decode(file_get_contents($manifest), true)
);
if (empty($extension->name)) {
continue; continue;
} }
// Instantiates an Extension object using the package path and composer.json file.
$extension = new Extension($this->getExtensionsDir().'/'.Arr::get($package, 'name'), $package);
foreach ($installed as $package) { // Per default all extensions are installed if they are registered in composer.
if ($package['name'] === $extension->name) { $extension->setInstalled(true);
$extension->setInstalled(true); $extension->setVersion(Arr::get($package, 'version'));
$extension->setVersion($package['version']); $extension->setEnabled($this->isEnabled($extension->getId()));
$extension->setEnabled($this->isEnabled($dir));
}
}
$extensions->put($dir, $extension); $extensions->put($extension->getId(), $extension);
} }
$this->extensions = $extensions->sortBy(function ($extension, $name) {
return $extension->composerJsonAttribute('extra.flarum-extension.title');
});
} }
return $extensions->sortBy(function ($extension, $name) { return $this->extensions;
return $extension->composerJsonAttribute('extra.flarum-extension.title');
});
} }
/** /**
@@ -241,18 +240,59 @@ class ExtensionManager
$this->migrate($extension, false); $this->migrate($extension, false);
} }
/**
* The database migrator.
*
* @return Migrator
*/
public function getMigrator() public function getMigrator()
{ {
return $this->migrator; return $this->migrator;
} }
protected function getEnabled() /**
* Get only enabled extensions.
*
* @return Collection
*/
public function getEnabledExtensions()
{ {
$config = $this->config->get('extensions_enabled'); return $this->getExtensions()->only($this->getEnabled());
return json_decode($config, true);
} }
/**
* Loads all bootstrap.php files of the enabled extensions.
*
* @return Collection
*/
public function getEnabledBootstrappers()
{
$bootstrappers = new Collection;
foreach ($this->getEnabledExtensions() as $extension) {
if ($this->filesystem->exists($file = $extension->getPath().'/bootstrap.php')) {
$bootstrappers->push($file);
}
}
return $bootstrappers;
}
/**
* The id's of the enabled extensions.
*
* @return array
*/
public function getEnabled()
{
return json_decode($this->config->get('extensions_enabled'), true);
}
/**
* Persist the currently enabled extensions.
*
* @param array $enabled
*/
protected function setEnabled(array $enabled) protected function setEnabled(array $enabled)
{ {
$enabled = array_values(array_unique($enabled)); $enabled = array_values(array_unique($enabled));
@@ -260,13 +300,24 @@ class ExtensionManager
$this->config->set('extensions_enabled', json_encode($enabled)); $this->config->set('extensions_enabled', json_encode($enabled));
} }
/**
* Whether the extension is enabled.
*
* @param $extension
* @return bool
*/
public function isEnabled($extension) public function isEnabled($extension)
{ {
return in_array($extension, $this->getEnabled()); return in_array($extension, $this->getEnabled());
} }
/**
* The extensions path.
*
* @return string
*/
protected function getExtensionsDir() protected function getExtensionsDir()
{ {
return public_path('extensions'); return public_path('vendor');
} }
} }

View File

@@ -21,15 +21,12 @@ class ExtensionServiceProvider extends AbstractServiceProvider
{ {
$this->app->bind('flarum.extensions', 'Flarum\Extension\ExtensionManager'); $this->app->bind('flarum.extensions', 'Flarum\Extension\ExtensionManager');
$config = $this->app->make('flarum.settings')->get('extensions_enabled'); $bootstrappers = $this->app->make('flarum.extensions')->getEnabledBootstrappers();
$extensions = json_decode($config, true);
foreach ($extensions as $extension) { foreach ($bootstrappers as $file) {
if (file_exists($file = public_path().'/extensions/'.$extension.'/bootstrap.php')) { $bootstrapper = require $file;
$bootstrapper = require $file;
$this->app->call($bootstrapper); $this->app->call($bootstrapper);
}
} }
} }
} }