1
0
mirror of https://github.com/flarum/core.git synced 2025-08-03 23:17:43 +02:00

Implemented extensions as an object, usable by backend and frontend.

This commit is contained in:
Daniel Klabbers
2016-02-10 15:12:24 +01:00
committed by Franz Liedke
parent 96c4e6b147
commit 191589e2b1
10 changed files with 430 additions and 117 deletions

View File

@@ -19,6 +19,8 @@ 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,70 +40,84 @@ class ExtensionManager
*/
protected $filesystem;
public function __construct(SettingsRepositoryInterface $config, Application $app, Migrator $migrator, Dispatcher $dispatcher, Filesystem $filesystem)
{
$this->config = $config;
$this->app = $app;
$this->migrator = $migrator;
public function __construct(
SettingsRepositoryInterface $config,
Application $app,
Migrator $migrator,
Dispatcher $dispatcher,
Filesystem $filesystem
) {
$this->config = $config;
$this->app = $app;
$this->migrator = $migrator;
$this->dispatcher = $dispatcher;
$this->filesystem = $filesystem;
}
public function getInfo()
/**
* @return Collection
*/
public function getExtensions()
{
$extensionsDir = $this->getExtensionsDir();
$dirs = array_diff(scandir($extensionsDir), ['.', '..']);
$extensions = [];
$dirs = array_diff(scandir($extensionsDir), ['.', '..']);
$extensions = new Collection();
$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 = json_decode(file_get_contents($manifest), true);
$extension = new Extension(
$extensionsDir . '/' . $dir,
json_decode(file_get_contents($manifest), true)
);
if (empty($extension['name'])) {
if (empty($extension->name)) {
continue;
}
if (isset($extension['extra']['flarum-extension']['icon'])) {
$icon = &$extension['extra']['flarum-extension']['icon'];
if ($file = array_get($icon, 'image')) {
$file = $extensionsDir . '/' . $dir . '/' . $file;
if (file_exists($file)) {
$mimetype = pathinfo($file, PATHINFO_EXTENSION) === 'svg'
? 'image/svg+xml'
: finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file);
$data = file_get_contents($file);
$icon['backgroundImage'] = 'url(\'data:' . $mimetype . ';base64,' . base64_encode($data) . '\')';
}
}
}
foreach ($installed as $package) {
if ($package['name'] === $extension['name']) {
$extension['version'] = $package['version'];
if ($package['name'] === $extension->name) {
$extension->setInstalled(true);
$extension->setVersion($package['version']);
$extension->setEnabled($this->isEnabled($dir));
}
}
$extension['id'] = $dir;
$extensions[$dir] = $extension;
$extensions->put($dir, $extension);
}
}
return $extensions;
return $extensions->sortBy(function ($extension, $name) {
return $extension->composerJsonAttribute('extra.flarum-extension.title');
});
}
public function enable($extension)
/**
* Loads an Extension with all information.
*
* @param string $name
* @return Extension|null
*/
public function getExtension($name)
{
if (! $this->isEnabled($extension)) {
return $this->getExtensions()->get($name);
}
/**
* Enables the extension.
*
* @param string $name
*/
public function enable($name)
{
if (!$this->isEnabled($name)) {
$extension = $this->getExtension($name);
$enabled = $this->getEnabled();
$enabled[] = $extension;
$enabled[] = $name;
$this->migrate($extension);
@@ -109,87 +125,122 @@ class ExtensionManager
$this->setEnabled($enabled);
$extension->setEnabled(true);
$this->dispatcher->fire(new ExtensionWasEnabled($extension));
}
}
public function disable($extension)
/**
* Disables an extension.
*
* @param string $name
*/
public function disable($name)
{
$enabled = $this->getEnabled();
if (($k = array_search($extension, $enabled)) !== false) {
if (($k = array_search($name, $enabled)) !== false) {
unset($enabled[$k]);
$extension = $this->getExtension($name);
$this->setEnabled($enabled);
$extension->setEnabled(false);
$this->dispatcher->fire(new ExtensionWasDisabled($extension));
}
}
public function uninstall($extension)
/**
* Uninstalls an extension.
*
* @param string $name
*/
public function uninstall($name)
{
$this->disable($extension);
$extension = $this->getExtension($name);
$this->disable($name);
$this->migrateDown($extension);
$this->unpublishAssets($extension);
$extension->setInstalled(false);
$this->dispatcher->fire(new ExtensionWasUninstalled($extension));
}
/**
* Copy the assets from an extension's assets directory into public view.
*
* @param string $extension
* @param Extension $extension
*/
protected function publishAssets($extension)
protected function publishAssets(Extension $extension)
{
$this->filesystem->copyDirectory(
$this->app->basePath().'/extensions/'.$extension.'/assets',
$this->app->basePath().'/assets/extensions/'.$extension
);
if ($extension->hasAssets()) {
$this->filesystem->copyDirectory(
$this->app->basePath() . '/extensions/' . $extension->getId() . '/assets',
$this->app->basePath() . '/assets/extensions/' . $extension->getId()
);
}
}
/**
* Delete an extension's assets from public view.
*
* @param string $extension
* @param Extension $extension
*/
protected function unpublishAssets($extension)
protected function unpublishAssets(Extension $extension)
{
$this->filesystem->deleteDirectory($this->app->basePath().'/assets/extensions/'.$extension);
$this->filesystem->deleteDirectory($this->app->basePath() . '/assets/extensions/' . $extension);
}
/**
* Get the path to an extension's published asset.
*
* @param string $extension
* @param string $path
* @param Extension $extension
* @param string $path
* @return string
*/
public function getAsset($extension, $path)
public function getAsset(Extension $extension, $path)
{
return $this->app->basePath().'/assets/extensions/'.$extension.$path;
return $this->app->basePath() . '/assets/extensions/' . $extension->getId() . $path;
}
public function migrate($extension, $up = true)
/**
* Runs the database migrations for the extension.
*
* @param Extension $extension
* @param bool|true $up
*/
public function migrate(Extension $extension, $up = true)
{
$migrationDir = public_path('extensions/' . $extension . '/migrations');
if ($extension->hasMigrations()) {
$migrationDir = public_path('extensions/' . $extension->getId() . '/migrations');
$this->app->bind('Illuminate\Database\Schema\Builder', function ($container) {
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
});
$this->app->bind('Illuminate\Database\Schema\Builder', function ($container) {
return $container->make('Illuminate\Database\ConnectionInterface')->getSchemaBuilder();
});
if ($up) {
$this->migrator->run($migrationDir, $extension);
} else {
$this->migrator->reset($migrationDir, $extension);
if ($up) {
$this->migrator->run($migrationDir, $extension);
} else {
$this->migrator->reset($migrationDir, $extension);
}
}
}
public function migrateDown($extension)
/**
* Runs the database migrations to reset the database to its old state.
*
* @param Extension $extension
*/
public function migrateDown(Extension $extension)
{
$this->migrate($extension, false);
$this->migrate($extension->getId(), false);
}
public function getMigrator()