Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.4 + Adapt to Yii Router 4 #158

Merged
merged 5 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
2 changes: 1 addition & 1 deletion .github/workflows/composer-require-checker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
2 changes: 1 addition & 1 deletion .github/workflows/rector.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.3']
['8.4']
2 changes: 1 addition & 1 deletion .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
os: >-
['ubuntu-latest']
php: >-
['8.1', '8.2', '8.3']
['8.1', '8.2', '8.3', '8.4']
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# Yii Router FastRoute Adapter Change Log

## 3.1.1 under development
## 4.0.0 under development

- Enh #154: Add `UrlGenerator` host and scheme properties to package config params (@ev-gor)
- Chg #157: Bump minimal PHP version to 8.1 and minor refactoring (@vjik)
- Chg #158: Change PHP constraint in `composer.json` to `8.1 - 8.4` (@vjik)
- Chg #158: Adapt to `yiisoft/router` version `^4.0` (@vjik)
- Enh #158: Use FQN for built-in PHP functions (@vjik)

## 3.1.0 March 19, 2024

Expand Down
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
}
],
"require": {
"php": "^8.1",
"php": "8.1 - 8.4",
"nikic/fast-route": "^1.3",
"psr/http-message": "^1.0|^2.0",
"psr/simple-cache": "^2.0|^3.0",
"psr/http-message": "^1.0 || ^2.0",
"psr/simple-cache": "^2.0 || ^3.0",
"yiisoft/http": "^1.2",
"yiisoft/router": "^2.1|^3.0"
"yiisoft/router": "^4"
},
"require-dev": {
"maglnet/composer-require-checker": "^4.7.1",
Expand All @@ -42,7 +42,7 @@
"rector/rector": "^2.0.9",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26.1",
"vimeo/psalm": "^5.26.1 || ^6.8.6",
"yiisoft/di": "^1.3",
"yiisoft/test-support": "^3.0.2"
},
Expand Down
1 change: 1 addition & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
ensureOverrideAttribute="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
27 changes: 18 additions & 9 deletions src/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use function array_key_exists;
use function array_keys;
use function implode;
use function is_scalar;
use function is_string;
use function preg_match;
use function sprintf;

final class UrlGenerator implements UrlGeneratorInterface
{
Expand Down Expand Up @@ -50,8 +52,12 @@
*
* @throws RuntimeException If parameter value does not match its regex.
*/
public function generate(string $name, array $arguments = [], array $queryParameters = []): string
{
public function generate(
string $name,
array $arguments = [],
array $queryParameters = [],
?string $hash = null,
): string {
$arguments = array_map('\strval', array_merge($this->defaultArguments, $arguments));
$route = $this->routeCollection->getRoute($name);
/** @var list<list<list<string>|string>> $parsedRoutes */
Expand All @@ -72,7 +78,7 @@
continue;
}

return $this->generatePath($arguments, $queryParameters, $parsedRouteParts);
return $this->generatePath($arguments, $queryParameters, $parsedRouteParts, $hash);
}

// No valid route was found: list minimal required parameters.
Expand All @@ -90,10 +96,11 @@
string $name,
array $arguments = [],
array $queryParameters = [],
?string $hash = null,
?string $scheme = null,
?string $host = null
?string $host = null,
): string {
$url = $this->generate($name, $arguments, $queryParameters);
$url = $this->generate($name, $arguments, $queryParameters, $hash);
$route = $this->routeCollection->getRoute($name);
$uri = $this->currentRoute && $this->currentRoute->getUri() !== null ? $this->currentRoute->getUri() : null;
$lastRequestScheme = $uri?->getScheme();
Expand All @@ -114,7 +121,7 @@
$host = '//' . $host;
}

return $this->ensureScheme(rtrim($host, '/') . $url, $scheme);

Check warning on line 124 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapRtrim": --- Original +++ New @@ @@ if ($host !== '' && $isRelativeHost) { $host = '//' . $host; } - return $this->ensureScheme(rtrim($host, '/') . $url, $scheme); + return $this->ensureScheme($host . $url, $scheme); } return $uri === null ? $url : $this->generateAbsoluteFromLastMatchedRequest($url, $uri, $scheme); }
}

return $uri === null ? $url : $this->generateAbsoluteFromLastMatchedRequest($url, $uri, $scheme);
Expand All @@ -126,15 +133,16 @@
public function generateFromCurrent(
array $replacedArguments,
array $queryParameters = [],
?string $hash = null,
?string $fallbackRouteName = null,
): string {
if ($this->currentRoute === null || $this->currentRoute->getName() === null) {
if ($fallbackRouteName !== null) {
return $this->generate($fallbackRouteName, $replacedArguments);
return $this->generate($fallbackRouteName, $replacedArguments, hash: $hash);
}

if ($this->currentRoute !== null && $this->currentRoute->getUri() !== null) {
return $this->currentRoute->getUri()->getPath();
return $this->currentRoute->getUri()->getPath() . ($hash !== null ? '#' . $hash : '');
}

throw new RuntimeException('Current route is not detected.');
Expand All @@ -151,6 +159,7 @@
$this->currentRoute->getName(),
array_merge($this->currentRoute->getArguments(), $replacedArguments),
$queryParameters,
$hash,
);
}

Expand All @@ -169,7 +178,7 @@
{
$port = '';
$uriPort = $uri->getPort();
if ($uriPort !== 80 && $uriPort !== null) {

Check warning on line 181 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ { $port = ''; $uriPort = $uri->getPort(); - if ($uriPort !== 80 && $uriPort !== null) { + if ($uriPort !== 79 && $uriPort !== null) { $port = ':' . $uriPort; } return $this->ensureScheme('://' . $uri->getHost() . $port . $url, $scheme ?? $uri->getScheme());

Check warning on line 181 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- Original +++ New @@ @@ { $port = ''; $uriPort = $uri->getPort(); - if ($uriPort !== 80 && $uriPort !== null) { + if ($uriPort !== 81 && $uriPort !== null) { $port = ':' . $uriPort; } return $this->ensureScheme('://' . $uri->getHost() . $port . $url, $scheme ?? $uri->getScheme());
$port = ':' . $uriPort;
}

Expand Down Expand Up @@ -219,7 +228,7 @@
*/
private function isRelative(string $url): bool
{
return strncmp($url, '//', 2) && !str_contains($url, '://');

Check warning on line 231 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ */ private function isRelative(string $url) : bool { - return strncmp($url, '//', 2) && !str_contains($url, '://'); + return strncmp($url, '//', 1) && !str_contains($url, '://'); } public function getUriPrefix() : string {
}

public function getUriPrefix(): string
Expand Down Expand Up @@ -274,7 +283,7 @@
* @param array<string,string> $arguments
* @param list<list<string>|string> $parts
*/
private function generatePath(array $arguments, array $queryParameters, array $parts): string
private function generatePath(array $arguments, array $queryParameters, array $parts, ?string $hash): string
{
$path = $this->getUriPrefix();

Expand Down Expand Up @@ -305,13 +314,13 @@
}
}

$path = str_replace('//', '/', $path);

Check warning on line 317 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapStrReplace": --- Original +++ New @@ @@ $path .= $this->encodeRaw ? rawurlencode($arguments[$part[0]]) : urlencode($arguments[$part[0]]); } } - $path = str_replace('//', '/', $path); + $path = $path; $queryString = ''; if (!empty($queryParameters)) { $queryString = http_build_query($queryParameters);

$queryString = '';
if (!empty($queryParameters)) {
$queryString = http_build_query($queryParameters);
}

return $path . (!empty($queryString) ? '?' . $queryString : '');
return $path . (!empty($queryString) ? '?' . $queryString : '') . ($hash !== null ? '#' . $hash : '');

Check warning on line 324 in src/UrlGenerator.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ if (!empty($queryParameters)) { $queryString = http_build_query($queryParameters); } - return $path . (!empty($queryString) ? '?' . $queryString : '') . ($hash !== null ? '#' . $hash : ''); + return $path . ($hash !== null ? '#' . $hash : '') . (!empty($queryString) ? '?' . $queryString : ''); } }
}
}
1 change: 1 addition & 0 deletions src/UrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Yiisoft\Router\UrlMatcherInterface;

use function array_merge;
use function count;

/**
* @psalm-type ResultNotFound = array{0:0}
Expand Down Expand Up @@ -82,7 +83,7 @@
) {
$this->fastRouteCollector = $fastRouteCollector ?? $this->createRouteCollector();
$this->dispatcherCallback = $dispatcherFactory;
$this->loadConfig($config);

Check warning on line 86 in src/UrlMatcher.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MethodCallRemoval": --- Original +++ New @@ @@ { $this->fastRouteCollector = $fastRouteCollector ?? $this->createRouteCollector(); $this->dispatcherCallback = $dispatcherFactory; - $this->loadConfig($config); + $this->loadDispatchData(); } public function match(ServerRequestInterface $request) : MatchingResult

$this->loadDispatchData();
}
Expand Down Expand Up @@ -121,12 +122,12 @@
*/
private function loadConfig(?array $config): void
{
if ($config === null) {

Check warning on line 125 in src/UrlMatcher.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Identical": --- Original +++ New @@ @@ */ private function loadConfig(?array $config) : void { - if ($config === null) { + if ($config !== null) { return; } if (isset($config[self::CONFIG_CACHE_KEY])) {
return;
}

if (isset($config[self::CONFIG_CACHE_KEY])) {
$this->cacheKey = (string) $config[self::CONFIG_CACHE_KEY];

Check warning on line 130 in src/UrlMatcher.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "CastString": --- Original +++ New @@ @@ return; } if (isset($config[self::CONFIG_CACHE_KEY])) { - $this->cacheKey = (string) $config[self::CONFIG_CACHE_KEY]; + $this->cacheKey = $config[self::CONFIG_CACHE_KEY]; } } /**
}
}

Expand Down Expand Up @@ -213,7 +214,7 @@
{
foreach ($this->routeCollection->getRoutes() as $route) {
if (!$route->getData('hasMiddlewares')) {
continue;

Check warning on line 217 in src/UrlMatcher.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Continue_": --- Original +++ New @@ @@ { foreach ($this->routeCollection->getRoutes() as $route) { if (!$route->getData('hasMiddlewares')) { - continue; + break; } $hosts = $route->getData('hosts'); $count = count($hosts);
}

$hosts = $route->getData('hosts');
Expand Down
Loading
Loading