Skip to content

Commit

Permalink
Fixed issue with create url and complex composition patterns. #1831
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar committed Aug 27, 2019
1 parent e2526b3 commit 1cc98d1
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 9 deletions.
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All notable changes to this project will be documented in this file. This projec

### Fixed

+ [#1831](https://github.com/luyadev/luya/issues/1831) Fixed issue with create url and complex composition patterns.
+ [#1826](https://github.com/luyadev/luya/issues/1826) Ensure arrayable implementation for Link objects.
+ [#1830](https://github.com/luyadev/luya/issues/1830) Lazyload widget asset registration issue fixed when used in nested context.

Expand Down
2 changes: 1 addition & 1 deletion core/helpers/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static function trailing($url, $slash = '/')
{
return rtrim($url, $slash) . $slash;
}

/**
* This helper method will not concern any context informations
*
Expand Down
18 changes: 14 additions & 4 deletions core/web/Composition.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,9 @@ public function getKey($key, $defaultValue = false)
/**
* Get the composition prefix path based on current provided request.
*
* An example response could be `de` or with other composition keys and patters `de/ch` or `de-CH`.
* An example response could be `de` or with other composition keys and patterns `de/ch` or `de-CH`.
*
* @return string
* @return string A prefix path like `de/ch`
* @since 1.0.5
*/
public function getPrefixPath()
Expand Down Expand Up @@ -255,7 +255,7 @@ public function createRoute(array $overrideKeys = [])
$composition = $this->getKeys();

foreach ($overrideKeys as $key => $value) {
if (isset($key, $composition)) {
if (array_key_exists($key, $composition)) {
$composition[$key] = $value;
}
}
Expand All @@ -265,6 +265,15 @@ public function createRoute(array $overrideKeys = [])
/**
* Prepend to current composition (or to provided composition prefix-route) to a given route.
*
* Assuming the current composition returns `en/gb` and the route is `foo/bar` the return
* value would be `en/gb/foo/bar`.
*
* If a trailing slash is provided from a route, this will be returned as well, assuming:
*
* ```php
* echo prepentTo('/foobar', 'en/gb'); // ouput: /en/gb/foobar
* echo prepentTo('foobar', 'en/gb'); // output: en/gb/foobar
*
* @param string $route The route where the composition prefix should be prepended.
* @param null|string $prefix Define the value you want to prepend to the route or not.
* @return string
Expand All @@ -281,7 +290,8 @@ public function prependTo($route, $prefix = null)

$prepend = '';

if (substr($route, 0, 1) == '/') {
// if it contains a prepend slash, we keep this, as long as the route is also longer then just a slash.
if (substr($route, 0, 1) == '/' && strlen($route) > 1) {
$prepend = '/';
}

Expand Down
4 changes: 2 additions & 2 deletions core/web/CompositionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CompositionResolver extends BaseObject
* Examples of how to use patterns:
*
* ```php
* 'pattern' => '<langShortCode:[a-z]{2}>.<countryShortCode:[a-z]{2}>', // de-ch; fr-ch
* 'pattern' => '<langShortCode:[a-z]{2}>-<countryShortCode:[a-z]{2}>', // de-ch; fr-ch
* ```
*/
public $pattern;
Expand Down Expand Up @@ -152,7 +152,7 @@ protected function getInternalResolverArray()
preg_match_all(static::VAR_MATCH_REGEX, $this->pattern, $patternDefinitions, PREG_SET_ORDER);

foreach ($patternDefinitions as $definition) {
$newRegex = str_replace($definition[0], "(".$definition[2].")", $newRegex);
$newRegex = str_replace($definition[0], '('.rtrim(ltrim($definition[2], '('), ')').')', $newRegex);
}

preg_match_all($newRegex, $requestPathInfo, $matches, PREG_SET_ORDER);
Expand Down
4 changes: 2 additions & 2 deletions core/web/UrlManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ public function internalCreateUrl($params, $composition = null)
{
$params = (array) $params;

$composition = (empty($composition)) ? $this->getComposition() : $composition;
$composition = empty($composition) ? $this->getComposition() : $composition;

$originalParams = $params;

Expand All @@ -282,7 +282,7 @@ public function internalCreateUrl($params, $composition = null)
$response = parent::createUrl($params);

// Check if the parsed route with the prepand composition has been found or not.
if (strpos($response, $params[0]) !== false) {
if (strpos($response, rtrim($params[0], '/')) !== false) {
// we got back the same url from the createUrl, no match against composition route.
$response = parent::createUrl($originalParams);
}
Expand Down
25 changes: 25 additions & 0 deletions tests/core/web/CompositionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,29 @@ public function testNotAllowedUnset()
$comp = new Composition($request);
unset($comp['langShortCode']);
}

public function testComplexLangShortCode()
{
$request = new Request(['hostInfo' => 'http://localhost', 'pathInfo' => 'en-GB/admin']);
$comp = new Composition($request, ['hidden' => false, 'pattern' => '<langShortCode:([a-z]{2}[\-]{1}[A-Z]{2})>', 'default' => ['langShortCode' => 'de-CH']]);

$this->assertSame('en-GB', $comp->createRoute());
$this->assertSame('de-CH', $comp->getDefaultLangShortCode());
$this->assertSame('en-GB/foobar', $comp->prependTo('foobar'));
$this->assertSame('en-GB', $comp->getPrefixPath());
$this->assertSame('en-GB/', $comp->prependTo('/'));
$this->assertSame(['langShortCode' => 'en-GB'], $comp->getKeys());

// test prepending
$this->assertSame('en-GB/', $comp->prependTo('', $comp->createRoute()));
$this->assertSame('en-GB/f', $comp->prependTo('f', $comp->createRoute()));
$this->assertSame('en-GB/f', $comp->prependTo('f'));
$this->assertSame('en-GB/', $comp->prependTo(''));

$resolver = $comp->getResolvedPathInfo($request);

$this->assertSame('admin', $resolver->resolvedPath);
$this->assertSame(['langShortCode' => 'en-GB'], $resolver->resolvedValues);
$this->assertSame(['langShortCode'], $resolver->resolvedKeys);
}
}
10 changes: 10 additions & 0 deletions tests/core/web/UrlManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,4 +306,14 @@ public function testCompositionRuleWithHiddenLanguageAsUrlCreation()
$url = $urlManager->createUrl(['/wirpre/default/imprint']);
$this->assertContains('/mentions-legales', $url);
}

public function testUrlCreationWithComplexCompositionPattern()
{
$request = new Request(['hostInfo' => 'http://localhost', 'pathInfo' => 'en-GB/admin', 'baseUrl' => '/']);
$comp = new Composition($request, ['hidden' => false, 'pattern' => '<langShortCode:([a-z]{2}[\-]{1}[A-Z]{2})>', 'default' => ['langShortCode' => 'de-CH']]);

$manager = new UrlManager();

$this->assertSame('/luya/envs/dev/public_html/en-GB/', $manager->internalCreateUrl(['/'], $comp));
}
}

0 comments on commit 1cc98d1

Please sign in to comment.