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

Use greedy search "controller(s)" in controller name extractor #120

Merged
merged 7 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Chg #115: Only a full path can now be used as a layout (@vjik)
- Chg #116, #117, #118: Rename package to `yiisoft/yii-view-renderer` (@vjik)
- Chg #119: Change package configuration parameters `viewPath` and `layout` to null (@vjik)
- Chg #64: Refactor controller name extractor, use greedy search of namespace items with "controller(s)" postfix (@vjik)

## 6.1.1 June 06, 2024

Expand Down
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@ application when you upgrade the package from one version to another.
'layout' => '@layout/main.php',
],
```

- Changed behavior of controller name extractor. Now it uses greedy search of namespace items with "controller(s)"
postfix. For example, controller namespace is `App\AllControllers\MyController\FooBar\BazController`. Previously,
result was "controller/foo-bar/baz", now it is "foo-bar/baz". You can use `ViewRenderer::withControllerName()`
instead of `ViewRenderer::withController()` to explicitly define controller name.
vjik marked this conversation as resolved.
Show resolved Hide resolved
14 changes: 9 additions & 5 deletions src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,8 @@ private function injectLinkTags(array $tags, WebView $view): void
*
* @example App\Controller\FooBar\BazController -> foo-bar/baz
* @example App\Controllers\FooBar\BazController -> foo-bar/baz
* @example App\AllControllers\MyController\FooBar\BazController -> foo-bar/baz
* @example App\AllControllers\MyController\BazController -> baz
* @example Path\To\File\BlogController -> blog
*
* @see Inflector::pascalCaseToId()
Expand All @@ -539,14 +541,16 @@ private function extractControllerName(object $controller): string
return $cache[$class];
}

$regexp = '/((?<=controller\\\|controllers\\\)(?:[\w\\\]+)|(?:[a-z\d]+))controller$/iU';
if (!preg_match($regexp, $class, $m) || empty($m[1])) {
if (preg_match('/.*controllers?\\\([\w\\\]+)controller$/iU', $class, $m) && !empty($m[1])) {
vjik marked this conversation as resolved.
Show resolved Hide resolved
$name = $m[1];
} elseif (preg_match('/(\w+)controller$/iU', $class, $m) && !empty($m[1])) {
$name = $m[1];
} else {
throw new RuntimeException('Cannot detect controller name.');
}

$inflector = new Inflector();
$name = str_replace('\\', '/', $m[1]);
return $cache[$class] = $inflector->pascalCaseToId($name);
$name = str_replace('\\', '/', $name);
return $cache[$class] = (new Inflector())->pascalCaseToId($name);
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/AllControllers/MoreController/MyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Renderer\Tests\Support\AllControllers\MoreController;

final class MyController
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Renderer\Tests\Support\AllControllers\MoreController\Nested;

final class MyController
{
}
8 changes: 8 additions & 0 deletions tests/ViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ public function dataWithController(): array
new Support\Controller\Sub8Namespace\FakeController(),
'/sub8namespace/fake',
],
'several controller in namespace' => [
new Support\AllControllers\MoreController\MyController(),
'/my',
],
'several controller in namespace, nested' => [
new Support\AllControllers\MoreController\Nested\MyController(),
'/nested/my',
],
];
}

Expand Down
Loading