Skip to content

Commit

Permalink
Use greedy search "controller(s)" in controller name extractor (#120)
Browse files Browse the repository at this point in the history
Co-authored-by: Alexander Makarov <[email protected]>
  • Loading branch information
vjik and samdark authored Jun 22, 2024
1 parent 2b194d5 commit 37b5050
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ In this case the name is determined as follows:
```text
App\Controller\FooBar\BazController -> foo-bar/baz
App\Controllers\FooBar\BazController -> foo-bar/baz
App\AllControllers\MyController\FooBar\BazController -> foo-bar/baz
App\AllControllers\MyController\BazController -> baz
Path\To\File\BlogController -> blog
```

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

- Controller name extractor now uses greedy search of namespace items with "controller(s)" postfix. For example, for controller namespace `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.
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('/(?:.*controller\\\|.*controllers\\\)([\w\\\]+)controller$/iU', $class, $m) && !empty($m[1])) {

Check warning on line 544 in src/ViewRenderer.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "PregMatchRemoveFlags": --- Original +++ New @@ @@ if (array_key_exists($class, $cache)) { return $cache[$class]; } - if (preg_match('/(?:.*controller\\\\|.*controllers\\\\)([\\w\\\\]+)controller$/iU', $class, $m) && !empty($m[1])) { + if (preg_match('/(?:.*controller\\\\|.*controllers\\\\)([\\w\\\\]+)controller$/i', $class, $m) && !empty($m[1])) { $name = $m[1]; } elseif (preg_match('/(\\w+)controller$/iU', $class, $m) && !empty($m[1])) { $name = $m[1];
$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

0 comments on commit 37b5050

Please sign in to comment.