mirror of
https://github.com/flarum/core.git
synced 2025-07-22 17:21:27 +02:00
Rough extension management implementation
This commit is contained in:
@@ -33,6 +33,7 @@ class ClientAction extends BaseClientAction
|
||||
$view->setVariable('config', $this->settings->all());
|
||||
$view->setVariable('locales', app('flarum.localeManager')->getLocales());
|
||||
$view->setVariable('permissions', Permission::map());
|
||||
$view->setVariable('extensions', app('flarum.extensions')->getInfo());
|
||||
|
||||
return $view;
|
||||
}
|
||||
|
43
framework/core/src/Api/Actions/Extensions/UpdateAction.php
Normal file
43
framework/core/src/Api/Actions/Extensions/UpdateAction.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php namespace Flarum\Api\Actions\Extensions;
|
||||
|
||||
use Flarum\Api\Actions\JsonApiAction;
|
||||
use Flarum\Api\Request;
|
||||
use Illuminate\Contracts\Bus\Dispatcher;
|
||||
use Flarum\Core\Exceptions\PermissionDeniedException;
|
||||
use Flarum\Support\ExtensionManager;
|
||||
|
||||
class UpdateAction extends JsonApiAction
|
||||
{
|
||||
protected $extensions;
|
||||
|
||||
public function __construct(ExtensionManager $extensions)
|
||||
{
|
||||
$this->extensions = $extensions;
|
||||
}
|
||||
|
||||
protected function respond(Request $request)
|
||||
{
|
||||
if (! $request->actor->isAdmin()) {
|
||||
throw new PermissionDeniedException;
|
||||
}
|
||||
|
||||
$enabled = $request->get('enabled');
|
||||
$name = $request->get('name');
|
||||
|
||||
if ($enabled === true) {
|
||||
$this->extensions->enable($name);
|
||||
} elseif ($enabled === false) {
|
||||
$this->extensions->disable($name);
|
||||
}
|
||||
|
||||
app('flarum.formatter')->flush();
|
||||
|
||||
$assetPath = public_path('assets');
|
||||
$manifest = file_get_contents($assetPath . '/rev-manifest.json');
|
||||
$revisions = json_decode($manifest, true);
|
||||
|
||||
foreach ($revisions as $file => $revision) {
|
||||
@unlink($assetPath . '/' . substr_replace($file, '-' . $revision, strrpos($file, '.'), 0));
|
||||
}
|
||||
}
|
||||
}
|
@@ -313,6 +313,19 @@ class ApiServiceProvider extends ServiceProvider
|
||||
$this->action('Flarum\Api\Actions\Groups\DeleteAction')
|
||||
);
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
| Extensions
|
||||
|--------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
// Toggle an extension
|
||||
$routes->patch(
|
||||
'/extensions/{name}',
|
||||
'flarum.api.extensions.update',
|
||||
$this->action('Flarum\Api\Actions\Extensions\UpdateAction')
|
||||
);
|
||||
|
||||
event(new RegisterApiRoutes($routes));
|
||||
}
|
||||
|
||||
|
@@ -31,6 +31,12 @@ class Formatter
|
||||
return $configurator;
|
||||
}
|
||||
|
||||
public function flush()
|
||||
{
|
||||
$this->cache->forget('flarum.formatter.parser');
|
||||
$this->cache->forget('flarum.formatter.renderer');
|
||||
}
|
||||
|
||||
protected function getComponent($key)
|
||||
{
|
||||
$cacheKey = 'flarum.formatter.' . $key;
|
||||
|
@@ -4,4 +4,11 @@ use Flarum\Support\ServiceProvider;
|
||||
|
||||
class Extension extends ServiceProvider
|
||||
{
|
||||
public function install()
|
||||
{
|
||||
}
|
||||
|
||||
public function uninstall()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
104
framework/core/src/Support/ExtensionManager.php
Normal file
104
framework/core/src/Support/ExtensionManager.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php namespace Flarum\Support;
|
||||
|
||||
use Flarum\Support\ServiceProvider;
|
||||
use Flarum\Core\Settings\SettingsRepository;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
|
||||
class ExtensionManager
|
||||
{
|
||||
protected $config;
|
||||
|
||||
protected $app;
|
||||
|
||||
public function __construct(SettingsRepository $config, Container $app)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
public function getInfo()
|
||||
{
|
||||
$extensionsDir = $this->getExtensionsDir();
|
||||
|
||||
$dirs = array_diff(scandir($extensionsDir), ['.', '..']);
|
||||
$extensions = [];
|
||||
|
||||
foreach ($dirs as $dir) {
|
||||
if (file_exists($manifest = $extensionsDir . '/' . $dir . '/flarum.json')) {
|
||||
$extensions[] = json_decode(file_get_contents($manifest));
|
||||
}
|
||||
}
|
||||
|
||||
return $extensions;
|
||||
}
|
||||
|
||||
public function enable($extension)
|
||||
{
|
||||
$enabled = $this->getEnabled();
|
||||
|
||||
if (! in_array($extension, $enabled)) {
|
||||
$enabled[] = $extension;
|
||||
|
||||
$class = $this->load($extension);
|
||||
|
||||
$class->install();
|
||||
|
||||
// run migrations
|
||||
// vendor publish
|
||||
|
||||
$this->setEnabled($enabled);
|
||||
}
|
||||
}
|
||||
|
||||
public function disable($extension)
|
||||
{
|
||||
$enabled = $this->getEnabled();
|
||||
|
||||
if (($k = array_search($extension, $enabled)) !== false) {
|
||||
unset($enabled[$k]);
|
||||
|
||||
$this->setEnabled($enabled);
|
||||
}
|
||||
}
|
||||
|
||||
public function uninstall($extension)
|
||||
{
|
||||
$this->disable($extension);
|
||||
|
||||
$class = $this->load($extension);
|
||||
|
||||
$class->uninstall();
|
||||
|
||||
// run migrations
|
||||
}
|
||||
|
||||
protected function getEnabled()
|
||||
{
|
||||
$config = $this->config->get('extensions_enabled');
|
||||
|
||||
return json_decode($config, true);
|
||||
}
|
||||
|
||||
protected function setEnabled(array $enabled)
|
||||
{
|
||||
$enabled = array_values(array_unique($enabled));
|
||||
|
||||
$this->config->set('extensions_enabled', json_encode($enabled));
|
||||
}
|
||||
|
||||
protected function load($extension)
|
||||
{
|
||||
if (file_exists($file = $this->getExtensionsDir() . '/' . $extension . '/bootstrap.php')) {
|
||||
$className = require $file;
|
||||
|
||||
$class = new $className($this->app);
|
||||
}
|
||||
|
||||
return $class;
|
||||
}
|
||||
|
||||
protected function getExtensionsDir()
|
||||
{
|
||||
return public_path('extensions');
|
||||
}
|
||||
}
|
@@ -9,6 +9,8 @@ class ExtensionsServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->bind('flarum.extensions', 'Flarum\Support\ExtensionManager');
|
||||
|
||||
$config = $this->app->make('Flarum\Core\Settings\SettingsRepository')->get('extensions_enabled');
|
||||
$extensions = json_decode($config, true);
|
||||
$providers = [];
|
||||
|
Reference in New Issue
Block a user