-
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactoring + Tests + MSI 100% #22
Changes from 9 commits
e1aa569
8ef269f
997d16f
bacdef2
aa16eca
e2e642d
af1fc0f
226f6b4
bdd72a6
ddaa97e
79f8a2c
1a237e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,5 +12,6 @@ | |
}, | ||
"mutators": { | ||
"@default": true | ||
} | ||
}, | ||
"minMsi": 100 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -155,61 +155,57 @@ private function parseRequestPath(ServerRequestInterface $request): Generator | |
} | ||
} | ||
|
||
$possibleAction = null; | ||
|
||
if ($path === '/') { | ||
yield [ | ||
$this->cleanClassname( | ||
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $this->defaultControllerName . $this->classPostfix | ||
), | ||
$possibleAction, | ||
$this->makeClassName($this->defaultControllerName), | ||
null, | ||
]; | ||
return; | ||
} | ||
|
||
$controllerName = preg_replace_callback( | ||
'#(/.)#u', | ||
static fn(array $matches) => mb_strtoupper($matches[1]), | ||
'#/.#u', | ||
static fn(array $matches) => mb_strtoupper($matches[0]), | ||
$path, | ||
); | ||
|
||
if (!preg_match('#^(.*?)/([^/]+)/?$#', $controllerName, $matches)) { | ||
if (!preg_match('#^/?(.*?)/([^/]+)/?$#', $controllerName, $matches)) { | ||
return; | ||
} | ||
|
||
$directoryPath = $matches[1]; | ||
$controllerName = $matches[2]; | ||
[$_, $directoryPath, $controllerName] = $matches; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd leave it as it was before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replaced to |
||
|
||
yield [ | ||
$this->cleanClassname( | ||
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controllerName . $this->classPostfix | ||
), | ||
$possibleAction, | ||
$this->makeClassName($controllerName, $directoryPath), | ||
null, | ||
]; | ||
|
||
if (preg_match('#^(.*?)/([^/]+)/?$#', $directoryPath, $matches)) { | ||
$possibleAction = strtolower($controllerName); | ||
$directoryPath = $matches[1]; | ||
$controllerName = $matches[2]; | ||
if ($directoryPath === '') { | ||
yield [ | ||
$this->makeClassName($this->defaultControllerName, $controllerName), | ||
null, | ||
]; | ||
} else { | ||
$directoryPath = $controllerName; | ||
$controllerName = $this->defaultControllerName; | ||
yield [ | ||
$this->makeClassName($directoryPath), | ||
strtolower($controllerName), | ||
]; | ||
} | ||
|
||
yield [ | ||
$this->cleanClassname( | ||
$this->namespace . '\\' . $this->baseControllerDirectory . '\\' . $directoryPath . '\\' . $controllerName . $this->classPostfix | ||
), | ||
$possibleAction, | ||
]; | ||
} | ||
|
||
private function cleanClassname(string $className): string | ||
private function makeClassName(string $controllerName, string $directoryPath = ''): string | ||
samdark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
return str_replace( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I remember there were some Windows cases where such operations are necessary to make the router work correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems, path always use |
||
['\\/\\', '\\/', '\\\\'], | ||
'\\', | ||
$className, | ||
); | ||
$parts = []; | ||
if ($this->namespace !== '') { | ||
$parts[] = $this->namespace; | ||
} | ||
if ($this->baseControllerDirectory !== '') { | ||
$parts[] = $this->baseControllerDirectory; | ||
} | ||
if ($directoryPath !== '') { | ||
$parts[] = str_replace('/', '\\', $directoryPath); | ||
} | ||
$parts[] = str_replace('/', '\\', $controllerName) . $this->classPostfix; | ||
return implode('\\', $parts); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\FileRouter\Tests\Support\App1\Controller\User\Profile; | ||
|
||
use HttpSoft\Response\TextResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
final class ViewController | ||
{ | ||
public function index(): ResponseInterface | ||
{ | ||
return new TextResponse('Hello, User\Profile\IndexController!', 200); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\FileRouter\Tests\Support\App5\Модуль3\Controller\Index; | ||
|
||
use HttpSoft\Response\TextResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class IndexController | ||
{ | ||
public function index(): ResponseInterface | ||
{ | ||
return new TextResponse('Hello, модуль3!', 200); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\FileRouter\Tests\Support\App6\Controller\User; | ||
|
||
use HttpSoft\Response\TextResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class IndexController | ||
{ | ||
public function create(): ResponseInterface | ||
{ | ||
return new TextResponse('Hello, create Controller/User/IndexController!'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Yiisoft\FileRouter\Tests\Support\App6\Controller; | ||
|
||
use HttpSoft\Response\TextResponse; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class UserController | ||
{ | ||
public static array $actions = [ | ||
'GET' => 'index', | ||
]; | ||
|
||
public function index(): ResponseInterface | ||
{ | ||
return new TextResponse('Hello, index Controller/UserController!'); | ||
} | ||
|
||
public function create(): ResponseInterface | ||
{ | ||
return new TextResponse('Hello, create Controller/UserController!'); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That enables
/
prefixing controller name. Use-case for that is interesting.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here
$controllerName
contains path with uppercased first symbols. Renamed it to$pathAsNamespace
.This regexp should be valid for paths with one and more items (
/user
,/user/profile
,/user/profile/view
).