Skip to content

Commit

Permalink
Merge pull request #534 from flightphp/alias-bug
Browse files Browse the repository at this point in the history
fixed bug with root alias
  • Loading branch information
n0nag0n authored Jan 28, 2024
2 parents a3541a1 + 4c60454 commit 19e40b9
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 24 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"test-coverage": "rm clover.xml && XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-html=coverage --coverage-clover=clover.xml && vendor/bin/coverage-check clover.xml 100",
"lint": "phpstan --no-progress -cphpstan.neon",
"beautify": "phpcbf --standard=phpcs.xml",
"phpcs": "phpcs --standard=phpcs.xml"
"phpcs": "phpcs --standard=phpcs.xml -n"
},
"suggest": {
"latte/latte": "Latte template engine",
Expand Down
44 changes: 22 additions & 22 deletions flight/database/PdoWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,36 +106,36 @@ public function fetchAll(string $sql, array $params = []): array
protected function processInStatementSql(string $sql, array $params = []): array
{
// Replace "IN(?)" with "IN(?,?,?)"
$sql = preg_replace('/IN\s*\(\s*\?\s*\)/i', 'IN(?)', $sql);
$sql = preg_replace('/IN\s*\(\s*\?\s*\)/i', 'IN(?)', $sql);

$current_index = 0;
while (($current_index = strpos($sql, 'IN(?)', $current_index)) !== false) {
$preceeding_count = substr_count($sql, '?', 0, $current_index - 1);
$current_index = 0;
while (($current_index = strpos($sql, 'IN(?)', $current_index)) !== false) {
$preceeding_count = substr_count($sql, '?', 0, $current_index - 1);

$param = $params[$preceeding_count];
$question_marks = '?';
$param = $params[$preceeding_count];
$question_marks = '?';

if (is_string($param) || is_array($param)) {
$params_to_use = $param;
if (is_string($param)) {
$params_to_use = explode(',', $param);
}
if (is_string($param) || is_array($param)) {
$params_to_use = $param;
if (is_string($param)) {
$params_to_use = explode(',', $param);
}

foreach ($params_to_use as $key => $value) {
if (is_string($value)) {
$params_to_use[$key] = trim($value);
foreach ($params_to_use as $key => $value) {
if (is_string($value)) {
$params_to_use[$key] = trim($value);
}
}
}

$question_marks = join(',', array_fill(0, count($params_to_use), '?'));
$sql = substr_replace($sql, $question_marks, $current_index + 3, 1);
$question_marks = join(',', array_fill(0, count($params_to_use), '?'));
$sql = substr_replace($sql, $question_marks, $current_index + 3, 1);

array_splice($params, $preceeding_count, 1, $params_to_use);
}
array_splice($params, $preceeding_count, 1, $params_to_use);
}

$current_index += strlen($question_marks) + 4;
}
$current_index += strlen($question_marks) + 4;
}

return [ 'sql' => $sql, 'params' => $params ];
return [ 'sql' => $sql, 'params' => $params ];
}
}
4 changes: 3 additions & 1 deletion flight/net/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,9 @@ public function hydrateUrl(array $params = []): string
// catches potential optional parameter
$url = str_replace('(/', '/', $url);
// trim any trailing slashes
$url = rtrim($url, '/');
if ($url !== '/') {
$url = rtrim($url, '/');
}
return $url;
}

Expand Down
7 changes: 7 additions & 0 deletions tests/RouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,13 @@ public function testRewindAndValid()
$this->assertTrue($result);
}

public function testGetRootUrlByAlias()
{
$this->router->map('/', [$this, 'ok'], false, 'path1');
$url = $this->router->getUrlByAlias('path1');
$this->assertEquals('/', $url);
}

public function testGetUrlByAliasNoMatches()
{
$this->router->map('/path1', [$this, 'ok'], false, 'path1');
Expand Down

0 comments on commit 19e40b9

Please sign in to comment.