mirror of
https://github.com/flarum/core.git
synced 2025-08-09 01:46:35 +02:00
Use Repositories instead of models
This commit is contained in:
@@ -15,6 +15,17 @@ use Flarum\User\User;
|
|||||||
|
|
||||||
class IdWithTransliteratedSlugDriver implements SlugDriverInterface
|
class IdWithTransliteratedSlugDriver implements SlugDriverInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $discussions DiscussionRepository
|
||||||
|
*/
|
||||||
|
protected $discussions;
|
||||||
|
|
||||||
|
public function __construct(DiscussionRepository $discussions)
|
||||||
|
{
|
||||||
|
$this->discussions = $discussions;
|
||||||
|
}
|
||||||
|
|
||||||
public function toSlug(AbstractModel $instance): string
|
public function toSlug(AbstractModel $instance): string
|
||||||
{
|
{
|
||||||
return $instance->id.(trim($instance->slug) ? '-'.$instance->slug : '');
|
return $instance->id.(trim($instance->slug) ? '-'.$instance->slug : '');
|
||||||
@@ -27,6 +38,6 @@ class IdWithTransliteratedSlugDriver implements SlugDriverInterface
|
|||||||
$slug = $slug_array[0];
|
$slug = $slug_array[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
return Discussion::where('id', $slug)->whereVisibleTo($actor)->firstOrFail();
|
return $this->discussions->findOrFail($slug, $actor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -51,11 +51,9 @@ class HttpServiceProvider extends AbstractServiceProvider
|
|||||||
|
|
||||||
foreach ($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) {
|
foreach ($this->app->make('flarum.http.slugDrivers') as $resourceClass => $resourceDrivers) {
|
||||||
$driverKey = $settings->get("slug_driver_$resourceClass", 'default');
|
$driverKey = $settings->get("slug_driver_$resourceClass", 'default');
|
||||||
if (array_key_exists($driverKey, $resourceDrivers)) {
|
|
||||||
$driverClass = $resourceDrivers[$driverKey];
|
$driverClass = array_key_exists($driverKey, $resourceDrivers) ? $resourceDrivers[$driverKey] : $resourceDrivers['default'];
|
||||||
} else {
|
|
||||||
$driverClass = $resourceDrivers['default'];
|
|
||||||
}
|
|
||||||
$compiledDrivers[$resourceClass] = $this->app->make($driverClass);
|
$compiledDrivers[$resourceClass] = $this->app->make($driverClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -40,6 +40,23 @@ class UserRepository
|
|||||||
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
|
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a user by username, optionally making sure it is visible to a certain
|
||||||
|
* user, or throw an exception.
|
||||||
|
*
|
||||||
|
* @param int $id
|
||||||
|
* @param User $actor
|
||||||
|
* @return User
|
||||||
|
*
|
||||||
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
|
||||||
|
*/
|
||||||
|
public function findOrFailByUsername($username, User $actor = null)
|
||||||
|
{
|
||||||
|
$query = User::where('id', $username);
|
||||||
|
|
||||||
|
return $this->scopeVisibleTo($query, $actor)->firstOrFail();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find a user by an identification (username or email).
|
* Find a user by an identification (username or email).
|
||||||
*
|
*
|
||||||
|
@@ -14,6 +14,17 @@ use Flarum\Http\SlugDriverInterface;
|
|||||||
|
|
||||||
class UsernameSlugDriver implements SlugDriverInterface
|
class UsernameSlugDriver implements SlugDriverInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var $users UserRepository
|
||||||
|
*/
|
||||||
|
protected $users;
|
||||||
|
|
||||||
|
public function __construct(UserRepository $users)
|
||||||
|
{
|
||||||
|
$this->users = $users;
|
||||||
|
}
|
||||||
|
|
||||||
public function toSlug(AbstractModel $instance): string
|
public function toSlug(AbstractModel $instance): string
|
||||||
{
|
{
|
||||||
return $instance->username;
|
return $instance->username;
|
||||||
@@ -21,6 +32,6 @@ class UsernameSlugDriver implements SlugDriverInterface
|
|||||||
|
|
||||||
public function fromSlug(string $slug, User $actor): AbstractModel
|
public function fromSlug(string $slug, User $actor): AbstractModel
|
||||||
{
|
{
|
||||||
return User::where('username', $slug)->whereVisibleTo($actor)->firstOrFail();
|
return $this->users->findOrFailByUsername($slug, $actor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user