Combine URL generator classes into one

This commit is contained in:
Franz Liedke 2017-06-25 23:33:02 +02:00
parent cc118d4c29
commit dbd034eaf4
19 changed files with 104 additions and 111 deletions

View File

@ -16,6 +16,7 @@ use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Http\RouteCollection; use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory; use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\Event\Saved; use Flarum\Settings\Event\Saved;
class AdminServiceProvider extends AbstractServiceProvider class AdminServiceProvider extends AbstractServiceProvider
@ -25,8 +26,8 @@ class AdminServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(UrlGenerator::class, function () { $this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
return new UrlGenerator($this->app, $this->app->make('flarum.admin.routes')); return $url->addCollection('admin', $this->app->make('flarum.admin.routes'), 'admin');
}); });
$this->app->singleton('flarum.admin.routes', function () { $this->app->singleton('flarum.admin.routes', function () {

View File

@ -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';
}

View File

@ -19,6 +19,7 @@ use Flarum\Event\ConfigureNotificationTypes;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Http\RouteCollection; use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory; use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Tobscure\JsonApi\ErrorHandler; use Tobscure\JsonApi\ErrorHandler;
use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler; use Tobscure\JsonApi\Exception\Handler\FallbackExceptionHandler;
use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler; use Tobscure\JsonApi\Exception\Handler\InvalidParameterExceptionHandler;
@ -30,8 +31,8 @@ class ApiServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(UrlGenerator::class, function () { $this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
return new UrlGenerator($this->app, $this->app->make('flarum.api.routes')); return $url->addCollection('api', $this->app->make('flarum.api.routes'), 'api');
}); });
$this->app->singleton('flarum.api.routes', function () { $this->app->singleton('flarum.api.routes', function () {

View File

@ -11,8 +11,8 @@
namespace Flarum\Api\Controller; namespace Flarum\Api\Controller;
use Flarum\Api\UrlGenerator;
use Flarum\Discussion\Search\DiscussionSearcher; use Flarum\Discussion\Search\DiscussionSearcher;
use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria; use Flarum\Search\SearchCriteria;
use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\ServerRequestInterface;
use Tobscure\JsonApi\Document; use Tobscure\JsonApi\Document;
@ -86,7 +86,7 @@ class ListDiscussionsController extends AbstractListController
$results = $this->searcher->search($criteria, $limit, $offset, $load); $results = $this->searcher->search($criteria, $limit, $offset, $load);
$document->addPaginationLinks( $document->addPaginationLinks(
$this->url->toRoute('discussions.index'), $this->url->to('api')->route('discussions.index'),
$request->getQueryParams(), $request->getQueryParams(),
$offset, $offset,
$limit, $limit,

View File

@ -11,7 +11,7 @@
namespace Flarum\Api\Controller; namespace Flarum\Api\Controller;
use Flarum\Api\UrlGenerator; use Flarum\Http\UrlGenerator;
use Flarum\Search\SearchCriteria; use Flarum\Search\SearchCriteria;
use Flarum\User\Exception\PermissionDeniedException; use Flarum\User\Exception\PermissionDeniedException;
use Flarum\User\Search\UserSearcher; use Flarum\User\Search\UserSearcher;
@ -84,7 +84,7 @@ class ListUsersController extends AbstractListController
$results = $this->searcher->search($criteria, $limit, $offset, $load); $results = $this->searcher->search($criteria, $limit, $offset, $load);
$document->addPaginationLinks( $document->addPaginationLinks(
$this->url->toRoute('users.index'), $this->url->to('api')->route('users.index'),
$request->getQueryParams(), $request->getQueryParams(),
$offset, $offset,
$limit, $limit,

View File

@ -11,8 +11,8 @@
namespace Flarum\Api\Controller; namespace Flarum\Api\Controller;
use Flarum\Forum\UrlGenerator;
use Flarum\Http\Controller\ControllerInterface; use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\AssertPermissionTrait; use Flarum\User\AssertPermissionTrait;
use Flarum\User\EmailToken; use Flarum\User\EmailToken;
@ -80,7 +80,7 @@ class SendConfirmationEmailController implements ControllerInterface
$data = [ $data = [
'{username}' => $actor->username, '{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') '{forum}' => $this->settings->get('forum_title')
]; ];

View File

@ -11,8 +11,8 @@
namespace Flarum\Api\Serializer; namespace Flarum\Api\Serializer;
use Flarum\Forum\UrlGenerator;
use Flarum\Foundation\Application; use Flarum\Foundation\Application;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
class ForumSerializer extends AbstractSerializer class ForumSerializer extends AbstractSerializer
@ -107,7 +107,7 @@ class ForumSerializer extends AbstractSerializer
{ {
$logoPath = $this->settings->get('logo_path'); $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'); $faviconPath = $this->settings->get('favicon_path');
return $faviconPath ? $this->url->toPath('assets/'.$faviconPath) : null; return $faviconPath ? $this->url->to('forum')->path('assets/'.$faviconPath) : null;
} }
} }

View File

@ -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';
}

View File

@ -13,8 +13,8 @@ namespace Flarum\Forum\Controller;
use Flarum\Api\Client; use Flarum\Api\Client;
use Flarum\Forum\Frontend; use Flarum\Forum\Frontend;
use Flarum\Forum\UrlGenerator;
use Flarum\Http\Exception\RouteNotFoundException; use Flarum\Http\Exception\RouteNotFoundException;
use Flarum\Http\UrlGenerator;
use Flarum\User\User; use Flarum\User\User;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Psr\Http\Message\ServerRequestInterface as Request; use Psr\Http\Message\ServerRequestInterface as Request;
@ -73,7 +73,7 @@ class DiscussionController extends FrontendController
$newQueryParams = array_merge($queryParams, $newQueryParams); $newQueryParams = array_merge($queryParams, $newQueryParams);
$queryString = http_build_query($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 : ''); ($queryString ? '?'.$queryString : '');
}; };

View File

@ -11,9 +11,9 @@
namespace Flarum\Forum\Controller; namespace Flarum\Forum\Controller;
use Flarum\Forum\UrlGenerator;
use Flarum\Http\Controller\ControllerInterface; use Flarum\Http\Controller\ControllerInterface;
use Flarum\Http\SessionAuthenticator; use Flarum\Http\SessionAuthenticator;
use Flarum\Http\UrlGenerator;
use Flarum\User\PasswordToken; use Flarum\User\PasswordToken;
use Flarum\User\UserValidator; use Flarum\User\UserValidator;
use Illuminate\Contracts\Validation\Factory; use Illuminate\Contracts\Validation\Factory;
@ -81,7 +81,7 @@ class SavePasswordController implements ControllerInterface
} catch (ValidationException $e) { } catch (ValidationException $e) {
$request->getAttribute('session')->set('error', $e->errors()->first()); $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); $token->user->changePassword($password);
@ -92,6 +92,6 @@ class SavePasswordController implements ControllerInterface
$session = $request->getAttribute('session'); $session = $request->getAttribute('session');
$this->authenticator->logIn($session, $token->user->id); $this->authenticator->logIn($session, $token->user->id);
return new RedirectResponse($this->url->toBase()); return new RedirectResponse($this->url->to('forum')->base());
} }
} }

View File

@ -17,6 +17,7 @@ use Flarum\Extension\Event\Enabled;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Http\RouteCollection; use Flarum\Http\RouteCollection;
use Flarum\Http\RouteHandlerFactory; use Flarum\Http\RouteHandlerFactory;
use Flarum\Http\UrlGenerator;
use Flarum\Settings\Event\Saved; use Flarum\Settings\Event\Saved;
class ForumServiceProvider extends AbstractServiceProvider class ForumServiceProvider extends AbstractServiceProvider
@ -26,8 +27,8 @@ class ForumServiceProvider extends AbstractServiceProvider
*/ */
public function register() public function register()
{ {
$this->app->singleton(UrlGenerator::class, function () { $this->app->extend(UrlGenerator::class, function (UrlGenerator $url) {
return new UrlGenerator($this->app, $this->app->make('flarum.forum.routes')); return $url->addCollection('forum', $this->app->make('flarum.forum.routes'));
}); });
$this->app->singleton('flarum.forum.routes', function () { $this->app->singleton('flarum.forum.routes', function () {

View File

@ -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
{
}

View File

@ -11,14 +11,12 @@
namespace Flarum\Http; namespace Flarum\Http;
use Flarum\Foundation\Application; class RouteCollectionUrlGenerator
class AbstractUrlGenerator
{ {
/** /**
* @var Application * @var string|null
*/ */
protected $app; protected $baseUrl;
/** /**
* @var RouteCollection * @var RouteCollection
@ -26,17 +24,12 @@ class AbstractUrlGenerator
protected $routes; protected $routes;
/** /**
* @var string|null * @param string $baseUrl
*/
protected $path;
/**
* @param Application $app
* @param RouteCollection $routes * @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; $this->routes = $routes;
} }
@ -47,12 +40,12 @@ class AbstractUrlGenerator
* @param array $parameters * @param array $parameters
* @return string * @return string
*/ */
public function toRoute($name, $parameters = []) public function route($name, $parameters = [])
{ {
$path = $this->routes->getPath($name, $parameters); $path = $this->routes->getPath($name, $parameters);
$path = ltrim($path, '/'); $path = ltrim($path, '/');
return $this->toBase().'/'.$path; return $this->baseUrl.'/'.$path;
} }
/** /**
@ -61,9 +54,9 @@ class AbstractUrlGenerator
* @param string $path * @param string $path
* @return string * @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 * @return string
*/ */
public function toBase() public function base()
{ {
return $this->app->url($this->path); return $this->baseUrl;
} }
} }

View 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];
}
}

View File

@ -11,7 +11,7 @@
namespace Flarum\User\Command; namespace Flarum\User\Command;
use Flarum\Forum\UrlGenerator; use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\PasswordToken; use Flarum\User\PasswordToken;
use Flarum\User\UserRepository; use Flarum\User\UserRepository;
@ -107,7 +107,7 @@ class RequestPasswordResetHandler
$data = [ $data = [
'{username}' => $user->username, '{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'), '{forum}' => $this->settings->get('forum_title'),
]; ];

View File

@ -11,7 +11,7 @@
namespace Flarum\User; namespace Flarum\User;
use Flarum\Forum\UrlGenerator; use Flarum\Http\UrlGenerator;
use Flarum\Settings\SettingsRepositoryInterface; use Flarum\Settings\SettingsRepositoryInterface;
use Flarum\User\Event\EmailChangeRequested; use Flarum\User\Event\EmailChangeRequested;
use Flarum\User\Event\Registered; use Flarum\User\Event\Registered;
@ -128,7 +128,7 @@ class EmailConfirmationMailer
return [ return [
'{username}' => $user->username, '{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') '{forum}' => $this->settings->get('forum_title')
]; ];
} }

View File

@ -325,9 +325,9 @@ class User extends AbstractModel
*/ */
public function getAvatarUrlAttribute() 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;
} }
/** /**

View File

@ -1,5 +1,5 @@
<?php <?php
$url = app('Flarum\Forum\UrlGenerator'); $url = app('Flarum\Http\UrlGenerator');
?> ?>
<div class="container"> <div class="container">
<h2>{{ $translator->trans('core.views.index.all_discussions_heading') }}</h2> <h2>{{ $translator->trans('core.views.index.all_discussions_heading') }}</h2>
@ -7,7 +7,7 @@ $url = app('Flarum\Forum\UrlGenerator');
<ul> <ul>
@foreach ($document->data as $discussion) @foreach ($document->data as $discussion)
<li> <li>
<a href="{{ $url->toRoute('discussion', [ <a href="{{ $url->to('forum')->route('discussion', [
'id' => $discussion->id . '-' . $discussion->attributes->slug 'id' => $discussion->id . '-' . $discussion->attributes->slug
]) }}"> ]) }}">
{{ $discussion->attributes->title }} {{ $discussion->attributes->title }}
@ -16,5 +16,5 @@ $url = app('Flarum\Forum\UrlGenerator');
@endforeach @endforeach
</ul> </ul>
<a href="{{ $url->toRoute('index') }}?page={{ $page + 1 }}">{{ $translator->trans('core.views.index.next_page_button') }} &raquo;</a> <a href="{{ $url->to('forum')->route('index') }}?page={{ $page + 1 }}">{{ $translator->trans('core.views.index.next_page_button') }} &raquo;</a>
</div> </div>

View File

@ -15,7 +15,7 @@
<p style="color:red">{{ $error }}</p> <p style="color:red">{{ $error }}</p>
@endif @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="csrfToken" value="{{ $csrfToken }}">
<input type="hidden" name="passwordToken" value="{{ $passwordToken }}"> <input type="hidden" name="passwordToken" value="{{ $passwordToken }}">