mirror of
https://github.com/flarum/framework.git
synced 2024-11-30 13:36:10 +08:00
Combine URL generator classes into one
This commit is contained in:
parent
cc118d4c29
commit
dbd034eaf4
|
@ -16,6 +16,7 @@ use Flarum\Extension\Event\Enabled;
|
|||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\RouteHandlerFactory;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\Event\Saved;
|
||||
|
||||
class AdminServiceProvider extends AbstractServiceProvider
|
||||
|
@ -25,8 +26,8 @@ class AdminServiceProvider extends AbstractServiceProvider
|
|||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton(UrlGenerator::class, function () {
|
||||
return new UrlGenerator($this->app, $this->app->make('flarum.admin.routes'));
|
||||
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
|
||||
return $url->addCollection('admin', $this->app->make('flarum.admin.routes'), 'admin');
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.admin.routes', function () {
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
<?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\Admin;
|
||||
|
||||
use Flarum\Http\AbstractUrlGenerator;
|
||||
|
||||
class UrlGenerator extends AbstractUrlGenerator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $path = 'admin';
|
||||
}
|
|
@ -19,6 +19,7 @@ use Flarum\Event\ConfigureNotificationTypes;
|
|||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\RouteHandlerFactory;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Tobscure\JsonApi\ErrorHandler;
|
||||
use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler;
|
||||
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
|
||||
|
@ -30,8 +31,8 @@ class ApiServiceProvider extends AbstractServiceProvider
|
|||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton(UrlGenerator::class, function () {
|
||||
return new UrlGenerator($this->app, $this->app->make('flarum.api.routes'));
|
||||
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
|
||||
return $url->addCollection('api', $this->app->make('flarum.api.routes'), 'api');
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.api.routes', function () {
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Api\UrlGenerator;
|
||||
use Flarum\Discussion\Search\DiscussionSearcher;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Tobscure\JsonApi\Document;
|
||||
|
@ -86,7 +86,7 @@ class ListDiscussionsController extends AbstractListController
|
|||
$results = $this->searcher->search($criteria, $limit, $offset, $load);
|
||||
|
||||
$document->addPaginationLinks(
|
||||
$this->url->toRoute('discussions.index'),
|
||||
$this->url->to('api')->route('discussions.index'),
|
||||
$request->getQueryParams(),
|
||||
$offset,
|
||||
$limit,
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Api\UrlGenerator;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Search\SearchCriteria;
|
||||
use Flarum\User\Exception\PermissionDeniedException;
|
||||
use Flarum\User\Search\UserSearcher;
|
||||
|
@ -84,7 +84,7 @@ class ListUsersController extends AbstractListController
|
|||
$results = $this->searcher->search($criteria, $limit, $offset, $load);
|
||||
|
||||
$document->addPaginationLinks(
|
||||
$this->url->toRoute('users.index'),
|
||||
$this->url->to('api')->route('users.index'),
|
||||
$request->getQueryParams(),
|
||||
$offset,
|
||||
$limit,
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
namespace Flarum\Api\Controller;
|
||||
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Http\Controller\ControllerInterface;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\AssertPermissionTrait;
|
||||
use Flarum\User\EmailToken;
|
||||
|
@ -80,7 +80,7 @@ class SendConfirmationEmailController implements ControllerInterface
|
|||
|
||||
$data = [
|
||||
'{username}' => $actor->username,
|
||||
'{url}' => $this->url->toRoute('confirmEmail', ['token' => $token->id]),
|
||||
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->id]),
|
||||
'{forum}' => $this->settings->get('forum_title')
|
||||
];
|
||||
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
namespace Flarum\Api\Serializer;
|
||||
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Foundation\Application;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
|
||||
class ForumSerializer extends AbstractSerializer
|
||||
|
@ -107,7 +107,7 @@ class ForumSerializer extends AbstractSerializer
|
|||
{
|
||||
$logoPath = $this->settings->get('logo_path');
|
||||
|
||||
return $logoPath ? $this->url->toPath('assets/'.$logoPath) : null;
|
||||
return $logoPath ? $this->url->to('forum')->path('assets/'.$logoPath) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -117,6 +117,6 @@ class ForumSerializer extends AbstractSerializer
|
|||
{
|
||||
$faviconPath = $this->settings->get('favicon_path');
|
||||
|
||||
return $faviconPath ? $this->url->toPath('assets/'.$faviconPath) : null;
|
||||
return $faviconPath ? $this->url->to('forum')->path('assets/'.$faviconPath) : null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
<?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;
|
||||
|
||||
use Flarum\Http\AbstractUrlGenerator;
|
||||
|
||||
class UrlGenerator extends AbstractUrlGenerator
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected $path = 'api';
|
||||
}
|
|
@ -13,8 +13,8 @@ namespace Flarum\Forum\Controller;
|
|||
|
||||
use Flarum\Api\Client;
|
||||
use Flarum\Forum\Frontend;
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Http\Exception\RouteNotFoundException;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\User\User;
|
||||
use Illuminate\Contracts\Events\Dispatcher;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
@ -73,7 +73,7 @@ class DiscussionController extends FrontendController
|
|||
$newQueryParams = array_merge($queryParams, $newQueryParams);
|
||||
$queryString = http_build_query($newQueryParams);
|
||||
|
||||
return $this->url->toRoute('discussion', ['id' => $document->data->id]).
|
||||
return $this->url->to('forum')->route('discussion', ['id' => $document->data->id]).
|
||||
($queryString ? '?'.$queryString : '');
|
||||
};
|
||||
|
||||
|
|
|
@ -11,9 +11,9 @@
|
|||
|
||||
namespace Flarum\Forum\Controller;
|
||||
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Http\Controller\ControllerInterface;
|
||||
use Flarum\Http\SessionAuthenticator;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\User\PasswordToken;
|
||||
use Flarum\User\UserValidator;
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
|
@ -81,7 +81,7 @@ class SavePasswordController implements ControllerInterface
|
|||
} catch (ValidationException $e) {
|
||||
$request->getAttribute('session')->set('error', $e->errors()->first());
|
||||
|
||||
return new RedirectResponse($this->url->toRoute('resetPassword', ['token' => $token->id]));
|
||||
return new RedirectResponse($this->url->to('forum')->route('resetPassword', ['token' => $token->id]));
|
||||
}
|
||||
|
||||
$token->user->changePassword($password);
|
||||
|
@ -92,6 +92,6 @@ class SavePasswordController implements ControllerInterface
|
|||
$session = $request->getAttribute('session');
|
||||
$this->authenticator->logIn($session, $token->user->id);
|
||||
|
||||
return new RedirectResponse($this->url->toBase());
|
||||
return new RedirectResponse($this->url->to('forum')->base());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ use Flarum\Extension\Event\Enabled;
|
|||
use Flarum\Foundation\AbstractServiceProvider;
|
||||
use Flarum\Http\RouteCollection;
|
||||
use Flarum\Http\RouteHandlerFactory;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\Event\Saved;
|
||||
|
||||
class ForumServiceProvider extends AbstractServiceProvider
|
||||
|
@ -26,8 +27,8 @@ class ForumServiceProvider extends AbstractServiceProvider
|
|||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->singleton(UrlGenerator::class, function () {
|
||||
return new UrlGenerator($this->app, $this->app->make('flarum.forum.routes'));
|
||||
$this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
|
||||
return $url->addCollection('forum', $this->app->make('flarum.forum.routes'));
|
||||
});
|
||||
|
||||
$this->app->singleton('flarum.forum.routes', function () {
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
<?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\Forum;
|
||||
|
||||
use Flarum\Http\AbstractUrlGenerator;
|
||||
|
||||
class UrlGenerator extends AbstractUrlGenerator
|
||||
{
|
||||
}
|
|
@ -11,14 +11,12 @@
|
|||
|
||||
namespace Flarum\Http;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
|
||||
class AbstractUrlGenerator
|
||||
class RouteCollectionUrlGenerator
|
||||
{
|
||||
/**
|
||||
* @var Application
|
||||
* @var string|null
|
||||
*/
|
||||
protected $app;
|
||||
protected $baseUrl;
|
||||
|
||||
/**
|
||||
* @var RouteCollection
|
||||
|
@ -26,17 +24,12 @@ class AbstractUrlGenerator
|
|||
protected $routes;
|
||||
|
||||
/**
|
||||
* @var string|null
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* @param Application $app
|
||||
* @param string $baseUrl
|
||||
* @param RouteCollection $routes
|
||||
*/
|
||||
public function __construct(Application $app, RouteCollection $routes)
|
||||
public function __construct($baseUrl, RouteCollection $routes)
|
||||
{
|
||||
$this->app = $app;
|
||||
$this->baseUrl = $baseUrl;
|
||||
$this->routes = $routes;
|
||||
}
|
||||
|
||||
|
@ -47,12 +40,12 @@ class AbstractUrlGenerator
|
|||
* @param array $parameters
|
||||
* @return string
|
||||
*/
|
||||
public function toRoute($name, $parameters = [])
|
||||
public function route($name, $parameters = [])
|
||||
{
|
||||
$path = $this->routes->getPath($name, $parameters);
|
||||
$path = ltrim($path, '/');
|
||||
|
||||
return $this->toBase().'/'.$path;
|
||||
return $this->baseUrl.'/'.$path;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -61,9 +54,9 @@ class AbstractUrlGenerator
|
|||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public function toPath($path)
|
||||
public function path($path)
|
||||
{
|
||||
return $this->toBase().'/'.$path;
|
||||
return $this->baseUrl.'/'.$path;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,8 +64,8 @@ class AbstractUrlGenerator
|
|||
*
|
||||
* @return string
|
||||
*/
|
||||
public function toBase()
|
||||
public function base()
|
||||
{
|
||||
return $this->app->url($this->path);
|
||||
return $this->baseUrl;
|
||||
}
|
||||
}
|
59
framework/core/src/Http/UrlGenerator.php
Normal file
59
framework/core/src/Http/UrlGenerator.php
Normal file
|
@ -0,0 +1,59 @@
|
|||
<?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\Http;
|
||||
|
||||
use Flarum\Foundation\Application;
|
||||
|
||||
class UrlGenerator
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $routes = [];
|
||||
|
||||
/**
|
||||
* @param Application $app
|
||||
*/
|
||||
public function __construct(Application $app)
|
||||
{
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a named route collection for URL generation.
|
||||
*
|
||||
* @param string $key
|
||||
* @param RouteCollection $routes
|
||||
* @param string $prefix
|
||||
* @return static
|
||||
*/
|
||||
public function addCollection($key, RouteCollection $routes, $prefix = null)
|
||||
{
|
||||
$this->routes[$key] = new RouteCollectionUrlGenerator(
|
||||
$this->app->url($prefix),
|
||||
$routes
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve an URL generator instance for the given named route collection.
|
||||
*
|
||||
* @param string $collection
|
||||
* @return RouteCollectionUrlGenerator
|
||||
*/
|
||||
public function to($collection)
|
||||
{
|
||||
return $this->routes[$collection];
|
||||
}
|
||||
}
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace Flarum\User\Command;
|
||||
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\PasswordToken;
|
||||
use Flarum\User\UserRepository;
|
||||
|
@ -107,7 +107,7 @@ class RequestPasswordResetHandler
|
|||
|
||||
$data = [
|
||||
'{username}' => $user->username,
|
||||
'{url}' => $this->url->toRoute('resetPassword', ['token' => $token->id]),
|
||||
'{url}' => $this->url->to('forum')->route('resetPassword', ['token' => $token->id]),
|
||||
'{forum}' => $this->settings->get('forum_title'),
|
||||
];
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace Flarum\User;
|
||||
|
||||
use Flarum\Forum\UrlGenerator;
|
||||
use Flarum\Http\UrlGenerator;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Flarum\User\Event\EmailChangeRequested;
|
||||
use Flarum\User\Event\Registered;
|
||||
|
@ -128,7 +128,7 @@ class EmailConfirmationMailer
|
|||
|
||||
return [
|
||||
'{username}' => $user->username,
|
||||
'{url}' => $this->url->toRoute('confirmEmail', ['token' => $token->id]),
|
||||
'{url}' => $this->url->to('forum')->route('confirmEmail', ['token' => $token->id]),
|
||||
'{forum}' => $this->settings->get('forum_title')
|
||||
];
|
||||
}
|
||||
|
|
|
@ -325,9 +325,9 @@ class User extends AbstractModel
|
|||
*/
|
||||
public function getAvatarUrlAttribute()
|
||||
{
|
||||
$urlGenerator = app('Flarum\Forum\UrlGenerator');
|
||||
$urlGenerator = app('Flarum\Http\UrlGenerator');
|
||||
|
||||
return $this->avatar_path ? $urlGenerator->toPath('assets/avatars/'.$this->avatar_path) : null;
|
||||
return $this->avatar_path ? $urlGenerator->to('forum')->path('assets/avatars/'.$this->avatar_path) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
$url = app('Flarum\Forum\UrlGenerator');
|
||||
$url = app('Flarum\Http\UrlGenerator');
|
||||
?>
|
||||
<div class="container">
|
||||
<h2>{{ $translator->trans('core.views.index.all_discussions_heading') }}</h2>
|
||||
|
@ -7,7 +7,7 @@ $url = app('Flarum\Forum\UrlGenerator');
|
|||
<ul>
|
||||
@foreach ($document->data as $discussion)
|
||||
<li>
|
||||
<a href="{{ $url->toRoute('discussion', [
|
||||
<a href="{{ $url->to('forum')->route('discussion', [
|
||||
'id' => $discussion->id . '-' . $discussion->attributes->slug
|
||||
]) }}">
|
||||
{{ $discussion->attributes->title }}
|
||||
|
@ -16,5 +16,5 @@ $url = app('Flarum\Forum\UrlGenerator');
|
|||
@endforeach
|
||||
</ul>
|
||||
|
||||
<a href="{{ $url->toRoute('index') }}?page={{ $page + 1 }}">{{ $translator->trans('core.views.index.next_page_button') }} »</a>
|
||||
<a href="{{ $url->to('forum')->route('index') }}?page={{ $page + 1 }}">{{ $translator->trans('core.views.index.next_page_button') }} »</a>
|
||||
</div>
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
<p style="color:red">{{ $error }}</p>
|
||||
@endif
|
||||
|
||||
<form class="form-horizontal" role="form" method="POST" action="{{ app('Flarum\Forum\UrlGenerator')->toRoute('savePassword') }}">
|
||||
<form class="form-horizontal" role="form" method="POST" action="{{ app('Flarum\Http\UrlGenerator')->to('forum')->route('savePassword') }}">
|
||||
<input type="hidden" name="csrfToken" value="{{ $csrfToken }}">
|
||||
<input type="hidden" name="passwordToken" value="{{ $passwordToken }}">
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user