Skip to content

Commit

Permalink
Add user id slug driver (#2787)
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 authored Apr 20, 2021
1 parent 1f2411e commit b62debf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Http/HttpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Flarum\Discussion\IdWithTransliteratedSlugDriver;
use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\IdSlugDriver;
use Flarum\User\User;
use Flarum\User\UsernameSlugDriver;
use Illuminate\Support\Arr;
Expand All @@ -38,7 +39,8 @@ public function register()
'default' => IdWithTransliteratedSlugDriver::class
],
User::class => [
'default' => UsernameSlugDriver::class
'default' => UsernameSlugDriver::class,
'id' => IdSlugDriver::class
],
];
});
Expand Down
36 changes: 36 additions & 0 deletions src/User/IdSlugDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\User;

use Flarum\Database\AbstractModel;
use Flarum\Http\SlugDriverInterface;

class IdSlugDriver implements SlugDriverInterface
{
/**
* @var UserRepository
*/
protected $users;

public function __construct(UserRepository $users)
{
$this->users = $users;
}

public function toSlug(AbstractModel $instance): string
{
return $instance->id;
}

public function fromSlug(string $slug, User $actor): AbstractModel
{
return $this->users->findOrFail($slug, $actor);
}
}

0 comments on commit b62debf

Please sign in to comment.