From c151bc700aff5bf3cc03c77d1522e39981e8290c Mon Sep 17 00:00:00 2001 From: Daniel Klabbers Date: Wed, 2 Mar 2016 08:56:37 +0100 Subject: [PATCH] Refactoring to drop extensions dir, see #774 satisfy nitpick --- framework/core/src/Extension/Extension.php | 5 +- .../core/src/Extension/ExtensionManager.php | 109 +++++++++++++----- .../Extension/ExtensionServiceProvider.php | 11 +- 3 files changed, 87 insertions(+), 38 deletions(-) diff --git a/framework/core/src/Extension/Extension.php b/framework/core/src/Extension/Extension.php index 9f7f08ad4..e4d5fdb2c 100644 --- a/framework/core/src/Extension/Extension.php +++ b/framework/core/src/Extension/Extension.php @@ -96,8 +96,9 @@ class Extension implements Arrayable */ protected function assignId() { - $segments = explode('/', $this->path); - $this->id = end($segments); + list($vendor, $package) = explode('/', $this->name); + $package = str_replace(['flarum-ext-', 'flarum-'], '', $package); + $this->id = "$vendor-$package"; } /** diff --git a/framework/core/src/Extension/ExtensionManager.php b/framework/core/src/Extension/ExtensionManager.php index 35b160c9a..dcdde941a 100644 --- a/framework/core/src/Extension/ExtensionManager.php +++ b/framework/core/src/Extension/ExtensionManager.php @@ -18,6 +18,7 @@ use Flarum\Foundation\Application; use Flarum\Settings\SettingsRepositoryInterface; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Filesystem\Filesystem; +use Illuminate\Support\Arr; use Illuminate\Support\Collection; class ExtensionManager @@ -38,6 +39,11 @@ class ExtensionManager */ protected $filesystem; + /** + * @var Collection|null + */ + protected $extensions; + public function __construct( SettingsRepositoryInterface $config, Application $app, @@ -57,39 +63,32 @@ class ExtensionManager */ 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), ['.', '..']); - $extensions = new Collection(); + // Load all packages installed by composer. + $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 ($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)) { + foreach ($installed as $package) { + if (Arr::get($package, 'type') != 'flarum-extension' || empty(Arr::get($package, 'name'))) { 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) { - if ($package['name'] === $extension->name) { - $extension->setInstalled(true); - $extension->setVersion($package['version']); - $extension->setEnabled($this->isEnabled($dir)); - } - } + // Per default all extensions are installed if they are registered in composer. + $extension->setInstalled(true); + $extension->setVersion(Arr::get($package, 'version')); + $extension->setEnabled($this->isEnabled($extension->getId())); - $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 $extension->composerJsonAttribute('extra.flarum-extension.title'); - }); + return $this->extensions; } /** @@ -241,18 +240,59 @@ class ExtensionManager $this->migrate($extension, false); } + /** + * The database migrator. + * + * @return Migrator + */ public function getMigrator() { return $this->migrator; } - protected function getEnabled() + /** + * Get only enabled extensions. + * + * @return Collection + */ + public function getEnabledExtensions() { - $config = $this->config->get('extensions_enabled'); - - return json_decode($config, true); + return $this->getExtensions()->only($this->getEnabled()); } + /** + * 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) { $enabled = array_values(array_unique($enabled)); @@ -260,13 +300,24 @@ class ExtensionManager $this->config->set('extensions_enabled', json_encode($enabled)); } + /** + * Whether the extension is enabled. + * + * @param $extension + * @return bool + */ public function isEnabled($extension) { return in_array($extension, $this->getEnabled()); } + /** + * The extensions path. + * + * @return string + */ protected function getExtensionsDir() { - return public_path('extensions'); + return public_path('vendor'); } } diff --git a/framework/core/src/Extension/ExtensionServiceProvider.php b/framework/core/src/Extension/ExtensionServiceProvider.php index e93351d9f..d5b15e201 100644 --- a/framework/core/src/Extension/ExtensionServiceProvider.php +++ b/framework/core/src/Extension/ExtensionServiceProvider.php @@ -21,15 +21,12 @@ class ExtensionServiceProvider extends AbstractServiceProvider { $this->app->bind('flarum.extensions', 'Flarum\Extension\ExtensionManager'); - $config = $this->app->make('flarum.settings')->get('extensions_enabled'); - $extensions = json_decode($config, true); + $bootstrappers = $this->app->make('flarum.extensions')->getEnabledBootstrappers(); - foreach ($extensions as $extension) { - if (file_exists($file = public_path().'/extensions/'.$extension.'/bootstrap.php')) { - $bootstrapper = require $file; + foreach ($bootstrappers as $file) { + $bootstrapper = require $file; - $this->app->call($bootstrapper); - } + $this->app->call($bootstrapper); } } }