1
0
mirror of https://github.com/flarum/core.git synced 2025-07-30 05:00:56 +02:00

Combine URL generator classes into one

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

View File

@@ -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
src/Http/UrlGenerator.php Normal file
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];
}
}