Skip to content

Commit

Permalink
Support unicode files and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jan 6, 2024
1 parent b974d27 commit abf457a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/FileRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,14 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
}

/** @psalm-suppress InvalidPropertyFetch */
$actions = $controllerClass::$actions ?? [
$action = $possibleAction ?? ($controllerClass::$actions ?? [
'HEAD' => 'head',
'OPTIONS' => 'options',
'GET' => 'index',
'POST' => 'create',
'PUT' => 'update',
'DELETE' => 'delete',
];
$action = $possibleAction ?? $actions[$request->getMethod()] ?? null;
])[$request->getMethod()] ?? null;

if ($action === null) {
continue;
Expand All @@ -93,7 +92,7 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
private function parseRequestPath(ServerRequestInterface $request): iterable
{
$possibleAction = null;
$path = $request->getUri()->getPath();
$path = urldecode($request->getUri()->getPath());
if ($path === '/') {
$controllerName = 'Index';

Expand All @@ -107,8 +106,8 @@ private function parseRequestPath(ServerRequestInterface $request): iterable
}

$controllerName = preg_replace_callback(
'#(/.)#',
fn(array $matches) => strtoupper($matches[1]),
'#(/.)#u',
fn(array $matches) => mb_strtoupper($matches[1]),
$path,
);

Expand Down
33 changes: 33 additions & 0 deletions tests/FileRouterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Yiisoft\FileRouter\Tests\Support\App1;
use Yiisoft\FileRouter\Tests\Support\App2;
use Yiisoft\FileRouter\Tests\Support\App3;
use Yiisoft\FileRouter\Tests\Support\App4;
use Yiisoft\FileRouter\Tests\Support\HeaderMiddleware;
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
use Yiisoft\Middleware\Dispatcher\MiddlewareFactory;
Expand Down Expand Up @@ -267,6 +268,36 @@ public static function dataRoutesCollision(): iterable
];
}

#[DataProvider('dataUnicodeRoutes')]
public function testTestUnicodeRoutes(string $method, string $uri, string $expectedResponse): void
{
$router = $this->createRouter();
$router = $router
->withNamespace('Yiisoft\FileRouter\Tests\Support\App4')
->withClassPostfix('Контроллер')
->withBaseControllerDirectory('Контроллеры');

$handler = $this->createExceptionHandler();
$request = new ServerRequest(
method: $method,
uri: $uri,
);

$response = $router->process($request, $handler);

$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals($expectedResponse, (string) $response->getBody());
}

public static function dataUnicodeRoutes(): iterable
{
yield 'direct' => [
'GET',
'/пользователь/главный',
'Привет, Контроллеры/Пользователь/ГлавныйКонтроллер!',
];
}

private function createRouter(): FileRouter
{
$container = new SimpleContainer([
Expand All @@ -280,6 +311,8 @@ private function createRouter(): FileRouter

App3\Controller\UserController::class => new App3\Controller\UserController(),
App3\Controller\User\IndexController::class => new App3\Controller\User\IndexController(),

App4\Контроллеры\Пользователь\ГлавныйКонтроллер::class => new App4\Контроллеры\Пользователь\ГлавныйКонтроллер(),
]);

return new FileRouter(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Yiisoft\FileRouter\Tests\Support\App4\Контроллеры\Пользователь;

use HttpSoft\Response\TextResponse;
use Psr\Http\Message\ResponseInterface;
use Yiisoft\FileRouter\Tests\Support\HeaderMiddleware;

class ГлавныйКонтроллер
{
public static array $actions = [
'GET' => 'главный',
];
public static array $middlewares = [
'index' => [
HeaderMiddleware::class,
],
];

public function главный(): ResponseInterface
{
return new TextResponse('Привет, Контроллеры/Пользователь/ГлавныйКонтроллер!');
}
}

0 comments on commit abf457a

Please sign in to comment.