Skip to content

Commit

Permalink
Cleaning up naming issues.
Browse files Browse the repository at this point in the history
Renamed URL default parameter method to have a more terse name. Wrote
test for feature.
  • Loading branch information
taylorotwell committed Dec 11, 2016
1 parent f72ac7f commit ce4d86b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 40 deletions.
63 changes: 23 additions & 40 deletions src/Illuminate/Routing/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ class UrlGenerator implements UrlGeneratorContract
*/
protected $request;

/**
* The named parameter defaults.
*
* @var array
*/
protected $defaultParameters = [];

/**
* The forced URL root.
*
Expand Down Expand Up @@ -108,13 +115,6 @@ class UrlGenerator implements UrlGeneratorContract
'%25' => '%',
];

/**
* Default named parameters.
*
* @var array
*/
protected $defaultNamedParameters = [];

/**
* Create a new URL Generator instance.
*
Expand Down Expand Up @@ -304,34 +304,6 @@ protected function getScheme($secure)
return $secure ? 'https://' : 'http://';
}

/**
* Get the default named parameters.
*
* @return array
*/
public function getDefaultNamedParameters()
{
return $this->defaultNamedParameters;
}

/**
* Set the default named parameters.
*
* @param string|array $key
* @param mixed $value
* @return void
*/
public function setDefaultNamedParameters($key, $value = null)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->defaultNamedParameters[$k] = $v;
}
} else {
$this->defaultNamedParameters[$key] = $value;
}
}

/**
* Force the schema for URLs.
*
Expand Down Expand Up @@ -438,13 +410,11 @@ protected function replaceRouteParameters($path, array &$parameters)
*/
protected function replaceNamedParameters($path, &$parameters)
{
$defaultNamedParameters = $this->getDefaultNamedParameters();

return preg_replace_callback('/\{(.*?)\??\}/', function ($m) use (&$parameters, $defaultNamedParameters) {
return preg_replace_callback('/\{(.*?)\??\}/', function ($m) use (&$parameters) {
if (isset($parameters[$m[1]])) {
return Arr::pull($parameters, $m[1]);
} elseif (isset($defaultNamedParameters[$m[1]])) {
return $defaultNamedParameters[$m[1]];
} elseif (isset($this->defaultParameters[$m[1]])) {
return $this->defaultParameters[$m[1]];
} else {
return $m[0];
}
Expand Down Expand Up @@ -736,6 +706,19 @@ protected function buildCompleteUrl($root, $path, $tail = '')
return trim($root.$path, '/');
}

/**
* Set the default named parameters used by the URL generator.
*
* @param array $defaults
* @return void
*/
public function defaults(array $defaults)
{
$this->defaultParameters = array_merge(
$this->defaultParameters, $defaults
);
}

/**
* Set a callback to be used to format the host of generated URLs.
*
Expand Down
8 changes: 8 additions & 0 deletions tests/Routing/RoutingUrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ public function testBasicRouteGeneration()
$route = new Route(['GET'], 'foo/invoke', ['controller' => 'InvokableActionStub']);
$routes->add($route);

/*
* With Default Parameter
*/
$url->defaults(['locale' => 'en']);
$route = new Route(['GET'], 'foo', ['as' => 'defaults', 'domain' => '{locale}.example.com', function () {}]);
$routes->add($route);

$this->assertEquals('/', $url->route('plain', [], false));
$this->assertEquals('/?foo=bar', $url->route('plain', ['foo' => 'bar'], false));
$this->assertEquals('http://www.foo.com/foo/bar', $url->route('foo'));
Expand All @@ -143,6 +150,7 @@ public function testBasicRouteGeneration()
$this->assertEquals('/foo/bar#derp', $url->route('fragment', [], false));
$this->assertEquals('/foo/bar?foo=bar#derp', $url->route('fragment', ['foo' => 'bar'], false));
$this->assertEquals('/foo/bar?baz=%C3%A5%CE%B1%D1%84#derp', $url->route('fragment', ['baz' => 'åαф'], false));
$this->assertEquals('http://en.example.com/foo', $url->route('defaults'));
}

public function testFluentRouteNameDefinitions()
Expand Down

0 comments on commit ce4d86b

Please sign in to comment.