diff --git a/framework/core/src/Admin/AdminServiceProvider.php b/framework/core/src/Admin/AdminServiceProvider.php index 1166ee120..913c6493a 100644 --- a/framework/core/src/Admin/AdminServiceProvider.php +++ b/framework/core/src/Admin/AdminServiceProvider.php @@ -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 () { diff --git a/framework/core/src/Admin/UrlGenerator.php b/framework/core/src/Admin/UrlGenerator.php deleted file mode 100644 index efa1a8a45..000000000 --- a/framework/core/src/Admin/UrlGenerator.php +++ /dev/null @@ -1,22 +0,0 @@ - - * - * 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'; -} diff --git a/framework/core/src/Api/ApiServiceProvider.php b/framework/core/src/Api/ApiServiceProvider.php index c886b25ec..ed4f916f3 100644 --- a/framework/core/src/Api/ApiServiceProvider.php +++ b/framework/core/src/Api/ApiServiceProvider.php @@ -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 () { diff --git a/framework/core/src/Api/Controller/ListDiscussionsController.php b/framework/core/src/Api/Controller/ListDiscussionsController.php index d69e07de6..ee75abc60 100644 --- a/framework/core/src/Api/Controller/ListDiscussionsController.php +++ b/framework/core/src/Api/Controller/ListDiscussionsController.php @@ -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, diff --git a/framework/core/src/Api/Controller/ListUsersController.php b/framework/core/src/Api/Controller/ListUsersController.php index d27369655..679230a98 100644 --- a/framework/core/src/Api/Controller/ListUsersController.php +++ b/framework/core/src/Api/Controller/ListUsersController.php @@ -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, diff --git a/framework/core/src/Api/Controller/SendConfirmationEmailController.php b/framework/core/src/Api/Controller/SendConfirmationEmailController.php index 2718501ea..2935f0067 100644 --- a/framework/core/src/Api/Controller/SendConfirmationEmailController.php +++ b/framework/core/src/Api/Controller/SendConfirmationEmailController.php @@ -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') ]; diff --git a/framework/core/src/Api/Serializer/ForumSerializer.php b/framework/core/src/Api/Serializer/ForumSerializer.php index 852ec08a6..399fcfd83 100644 --- a/framework/core/src/Api/Serializer/ForumSerializer.php +++ b/framework/core/src/Api/Serializer/ForumSerializer.php @@ -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; } } diff --git a/framework/core/src/Api/UrlGenerator.php b/framework/core/src/Api/UrlGenerator.php deleted file mode 100644 index 71dfbfc40..000000000 --- a/framework/core/src/Api/UrlGenerator.php +++ /dev/null @@ -1,22 +0,0 @@ - - * - * 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'; -} diff --git a/framework/core/src/Forum/Controller/DiscussionController.php b/framework/core/src/Forum/Controller/DiscussionController.php index f952ba799..216f72961 100644 --- a/framework/core/src/Forum/Controller/DiscussionController.php +++ b/framework/core/src/Forum/Controller/DiscussionController.php @@ -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 : ''); }; diff --git a/framework/core/src/Forum/Controller/SavePasswordController.php b/framework/core/src/Forum/Controller/SavePasswordController.php index 541113ebd..0d1c76fa0 100644 --- a/framework/core/src/Forum/Controller/SavePasswordController.php +++ b/framework/core/src/Forum/Controller/SavePasswordController.php @@ -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()); } } diff --git a/framework/core/src/Forum/ForumServiceProvider.php b/framework/core/src/Forum/ForumServiceProvider.php index 7526ad4b7..682d8194b 100644 --- a/framework/core/src/Forum/ForumServiceProvider.php +++ b/framework/core/src/Forum/ForumServiceProvider.php @@ -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 () { diff --git a/framework/core/src/Forum/UrlGenerator.php b/framework/core/src/Forum/UrlGenerator.php deleted file mode 100644 index b18fbb42f..000000000 --- a/framework/core/src/Forum/UrlGenerator.php +++ /dev/null @@ -1,18 +0,0 @@ - - * - * 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 -{ -} diff --git a/framework/core/src/Http/AbstractUrlGenerator.php b/framework/core/src/Http/RouteCollectionUrlGenerator.php similarity index 64% rename from framework/core/src/Http/AbstractUrlGenerator.php rename to framework/core/src/Http/RouteCollectionUrlGenerator.php index ccd0d78ef..dbcb8927e 100644 --- a/framework/core/src/Http/AbstractUrlGenerator.php +++ b/framework/core/src/Http/RouteCollectionUrlGenerator.php @@ -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; } } diff --git a/framework/core/src/Http/UrlGenerator.php b/framework/core/src/Http/UrlGenerator.php new file mode 100644 index 000000000..86e814731 --- /dev/null +++ b/framework/core/src/Http/UrlGenerator.php @@ -0,0 +1,59 @@ + + * + * 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]; + } +} diff --git a/framework/core/src/User/Command/RequestPasswordResetHandler.php b/framework/core/src/User/Command/RequestPasswordResetHandler.php index ba464a698..c0ac0c7fd 100644 --- a/framework/core/src/User/Command/RequestPasswordResetHandler.php +++ b/framework/core/src/User/Command/RequestPasswordResetHandler.php @@ -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'), ]; diff --git a/framework/core/src/User/EmailConfirmationMailer.php b/framework/core/src/User/EmailConfirmationMailer.php index 9ff225dc9..3273690b1 100755 --- a/framework/core/src/User/EmailConfirmationMailer.php +++ b/framework/core/src/User/EmailConfirmationMailer.php @@ -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') ]; } diff --git a/framework/core/src/User/User.php b/framework/core/src/User/User.php index b035a58f4..8c9cce87f 100755 --- a/framework/core/src/User/User.php +++ b/framework/core/src/User/User.php @@ -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; } /** diff --git a/framework/core/views/index.blade.php b/framework/core/views/index.blade.php index cbde0cff7..ce3a54feb 100644 --- a/framework/core/views/index.blade.php +++ b/framework/core/views/index.blade.php @@ -1,5 +1,5 @@

{{ $translator->trans('core.views.index.all_discussions_heading') }}

@@ -7,7 +7,7 @@ $url = app('Flarum\Forum\UrlGenerator'); - {{ $translator->trans('core.views.index.next_page_button') }} » + {{ $translator->trans('core.views.index.next_page_button') }} »
diff --git a/framework/core/views/reset.blade.php b/framework/core/views/reset.blade.php index 671157b38..fd4a254c9 100644 --- a/framework/core/views/reset.blade.php +++ b/framework/core/views/reset.blade.php @@ -15,7 +15,7 @@

{{ $error }}

@endif -
+