mirror of
https://github.com/flarum/core.git
synced 2025-07-21 08:41:17 +02:00
Slug Driver Support (#2456)
- Support slug drivers for core's sluggable models, easily extends to other models - Add automated testing for affected single-model API routes - Fix nickname selection UI - Serialize slugs as `slug` attribute - Make min search length a constant
This commit is contained in:
@@ -9,7 +9,13 @@
|
||||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Flarum\Discussion\Discussion;
|
||||
use Flarum\Discussion\IdWithTransliteratedSlugDriver;
|
||||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\User;
|
||||
use Flarum\User\UsernameSlugDriver;
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class HttpServiceProvider extends AbstractServiceProvider
|
||||
{
|
||||
@@ -25,5 +31,35 @@ class HttpServiceProvider extends AbstractServiceProvider
|
||||
$this->app->bind(Middleware\CheckCsrfToken::class, function ($app) {
|
||||
return new Middleware\CheckCsrfToken($app->make('flarum.http.csrfExemptPaths'));
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.http.slugDrivers', function () {
|
||||
return [
|
||||
Discussion::class => [
|
||||
'default' => IdWithTransliteratedSlugDriver::class
|
||||
],
|
||||
User::class => [
|
||||
'default' => UsernameSlugDriver::class
|
||||
],
|
||||
];
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.http.selectedSlugDrivers', function () {
|
||||
$settings = $this->app->make(SettingsRepositoryInterface::class);
|
||||
|
||||
$compiledDrivers = [];
|
||||
|
||||
foreach ($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) {
|
||||
$driverKey = $settings->get("slug_driver_$resourceClass", 'default');
|
||||
|
||||
$driverClass = Arr::get($resourceDrivers, $driverKey, $resourceDrivers['default']);
|
||||
|
||||
$compiledDrivers[$resourceClass] = $this->app->make($driverClass);
|
||||
}
|
||||
|
||||
return $compiledDrivers;
|
||||
});
|
||||
$this->app->bind(SlugManager::class, function () {
|
||||
return new SlugManager($this->app->make('flarum.http.selectedSlugDrivers'));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
20
framework/core/src/Http/SlugDriverInterface.php
Normal file
20
framework/core/src/Http/SlugDriverInterface.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Flarum\Database\AbstractModel;
|
||||
use Flarum\User\User;
|
||||
|
||||
interface SlugDriverInterface
|
||||
{
|
||||
public function toSlug(AbstractModel $instance): string;
|
||||
|
||||
public function fromSlug(string $slug, User $actor): AbstractModel;
|
||||
}
|
27
framework/core/src/Http/SlugManager.php
Normal file
27
framework/core/src/Http/SlugManager.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* For detailed copyright and license information, please view the
|
||||
* LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
|
||||
class SlugManager
|
||||
{
|
||||
protected $drivers = [];
|
||||
|
||||
public function __construct(array $drivers)
|
||||
{
|
||||
$this->drivers = $drivers;
|
||||
}
|
||||
|
||||
public function forResource(string $resourceName): SlugDriverInterface
|
||||
{
|
||||
return Arr::get($this->drivers, $resourceName, null);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user