Skip to content

Commit

Permalink
Tweak to actionRoutes
Browse files Browse the repository at this point in the history
  • Loading branch information
rotimi committed May 8, 2024
1 parent 8ca1708 commit aa06831
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/controllers/BaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,11 @@ public function actionIndex(): ResponseInterface|string {
*
* @param bool $onlyPublicMethodsPrefixedWithAction true to include only public methods prefixed with `action`
* or false to include all public methods
* @param bool $stripActionPrefixFromMethodName true to strip the `action-` prefix from methods displayed or false to leave the `action-` prefix
*
* @psalm-suppress PossiblyUnusedMethod
*/
public function actionRoutes($onlyPublicMethodsPrefixedWithAction=true): ResponseInterface|string {
public function actionRoutes($onlyPublicMethodsPrefixedWithAction=true, $stripActionPrefixFromMethodName=true): ResponseInterface|string {

$resp = $this->getResponseObjForLoginRedirectionIfNotLoggedIn();

Expand All @@ -544,7 +546,10 @@ public function actionRoutes($onlyPublicMethodsPrefixedWithAction=true): Respons
/** @psalm-suppress RedundantCastGivenDocblockType */
$view_str = $this->renderView(
'controller-classes-by-action-methods-report.php',
['onlyPublicMethodsPrefixedWithAction'=> ((bool)$onlyPublicMethodsPrefixedWithAction)]
[
'onlyPublicMethodsPrefixedWithAction'=> ((bool)$onlyPublicMethodsPrefixedWithAction),
'stripActionPrefixFromMethodName'=> ((bool)$stripActionPrefixFromMethodName),
]
);

return $this->renderLayout( $this->layout_template_file_name, ['content'=>$view_str] );
Expand Down
6 changes: 6 additions & 0 deletions tests/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,12 @@ public function testThat_actionRoutes_WorksAsExpected2() {
file_get_contents("{$path}action-routes-output.txt"),
$result
);

$result2 = $controller->actionRoutes(1, 0);
self::assertEquals(
file_get_contents("{$path}action-routes-output-2.txt"),
$result2
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ function(\ReflectionMethod $current_method)use($functionStrStartsWith, $rfclass,
.
(
$onlyPublicMethodsPrefixedWithAction
? camelToDashes(str_replace('action', '', $ref_meth_obj->getName()))
? camelToDashes(
$stripActionPrefixFromMethodName
? str_replace('action', '', $ref_meth_obj->getName())
: $ref_meth_obj->getName()
)
: camelToDashes($ref_meth_obj->getName())
);

Expand Down
37 changes: 37 additions & 0 deletions tests/test-template-output/action-routes-output-2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Layout: View: <h1>App Routes</h1>
<table>
<thead>
<tr>
<th>Controller Class Name</th>
<th>Action Method Name</th>
<th>Route</th>
</tr>
</thead>
<tbody>
<tr>
<td>SlimMvcTools\Controllers\BaseController</td>
<td>actionIndex</td>
<td>base-controller/action-index</td>
</tr>
<tr>
<td>SlimMvcTools\Controllers\BaseController</td>
<td>actionLogin</td>
<td>base-controller/action-login</td>
</tr>
<tr>
<td>SlimMvcTools\Controllers\BaseController</td>
<td>actionLoginStatus</td>
<td>base-controller/action-login-status</td>
</tr>
<tr>
<td>SlimMvcTools\Controllers\BaseController</td>
<td>actionLogout</td>
<td>base-controller/action-logout[/show_status_on_completion=false]</td>
</tr>
<tr>
<td>SlimMvcTools\Controllers\BaseController</td>
<td>actionRoutes</td>
<td>base-controller/action-routes[/onlyPublicMethodsPrefixedWithAction=true][/stripActionPrefixFromMethodName=true]</td>
</tr>
</tbody>
</table>
2 changes: 1 addition & 1 deletion tests/test-template-output/action-routes-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Layout: View: <h1>App Routes</h1>
<tr>
<td>SlimMvcTools\Controllers\BaseController</td>
<td>actionRoutes</td>
<td>base-controller/routes[/onlyPublicMethodsPrefixedWithAction=true]</td>
<td>base-controller/routes[/onlyPublicMethodsPrefixedWithAction=true][/stripActionPrefixFromMethodName=true]</td>
</tr>
</tbody>
</table>

0 comments on commit aa06831

Please sign in to comment.