1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 23:47:32 +02:00

Allow configuring all drivers via frontend (#1169)

This includes an API endpoint for fetching the list of possible
drivers and their configuration fields. In the future, this can
be extended to include more meta information about each field.
This commit is contained in:
Franz Liedke
2019-03-19 09:56:20 +01:00
parent 2bd40b50c7
commit 5154d7e5a6
11 changed files with 196 additions and 32 deletions

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Controller;
use Flarum\Api\Serializer\MailDriverSerializer;
use Flarum\User\AssertPermissionTrait;
use Illuminate\Contracts\Container\Container;
use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document;
class ListMailDriversController extends AbstractListController
{
use AssertPermissionTrait;
/**
* {@inheritdoc}
*/
public $serializer = MailDriverSerializer::class;
/**
* {@inheritdoc}
*/
protected function data(ServerRequestInterface $request, Document $document)
{
$this->assertAdmin($request->getAttribute('actor'));
$drivers = self::$container->make('mail.supported_drivers');
array_walk($drivers, function (&$driver, $key) {
$driver = [
'id' => $key,
'driver' => self::$container->make($driver),
];
});
return $drivers;
}
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of Flarum.
*
* (c) Toby Zerner <toby.zerner@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Flarum\Api\Serializer;
use Flarum\Mail\DriverInterface;
use InvalidArgumentException;
class MailDriverSerializer extends AbstractSerializer
{
/**
* {@inheritdoc}
*/
protected $type = 'mail-drivers';
/**
* {@inheritdoc}
*
* @param \Flarum\Mail\DriverInterface $driver
* @throws InvalidArgumentException
*/
protected function getDefaultAttributes($driver)
{
if (! ($driver['driver'] instanceof DriverInterface)) {
throw new InvalidArgumentException(
get_class($this).' can only serialize instances of '.DriverInterface::class
);
}
$driver = $driver['driver'];
return [
'fields' => $driver->availableSettings(),
];
}
public function getId($model)
{
return $model['id'];
}
}

View File

@@ -308,4 +308,11 @@ return function (RouteCollection $map, RouteHandlerFactory $route) {
'cache.clear',
$route->toController(Controller\ClearCacheController::class)
);
// List available mail drivers and their configuration fields
$map->get(
'/mail-drivers',
'mailDrivers.index',
$route->toController(Controller\ListMailDriversController::class)
);
};