Skip to content

Commit

Permalink
Add HTTP methods to route list
Browse files Browse the repository at this point in the history
Related to netz98#1411
  • Loading branch information
cmuench committed Apr 27, 2024
1 parent 3e9fef0 commit fd4418d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 11 deletions.
71 changes: 69 additions & 2 deletions src/N98/Magento/Command/Route/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

namespace N98\Magento\Command\Route;

use Magento\Framework\App\Action\HttpConnectActionInterface;
use Magento\Framework\App\Action\HttpDeleteActionInterface;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\Action\HttpOptionsActionInterface;
use Magento\Framework\App\Action\HttpPatchActionInterface;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\Action\HttpPropfindActionInterface;
use Magento\Framework\App\Action\HttpPutActionInterface;
use Magento\Framework\App\Action\HttpTraceActionInterface;
use Magento\Framework\App\ActionInterface;
use Magento\Framework\App\AreaList;
use Magento\Framework\App\Route\Config;
Expand Down Expand Up @@ -125,7 +134,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
$actionClass = substr($actionClass, 0, -6);
}

$moduleActions[$moduleName][$area][] = strtolower(implode('/', $actionPath) . '/' . $actionClass);
$moduleActions[$moduleName][$area][] = [
'file' => strtolower(implode('/', $actionPath) . '/' . $actionClass),
'class' => $fullActionPath,
];
}

$areas = $input->getOption('area') ? [$input->getOption('area')] : $this->areaList->getCodes();
Expand All @@ -142,6 +154,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
'Frontname',
'Module',
'Route',
'Methods'
]
)
->renderByFormat($output, $table, $input->getOption('format'))
Expand Down Expand Up @@ -195,12 +208,66 @@ protected function processSingleRoute($area, $route, $moduleOption, array $modul
if (isset($moduleActions[$module][$area])) {
foreach ($moduleActions[$module][$area] as $action) {
$moduleRouteAction = $moduleRoute;
$moduleRouteAction[] = $route['frontName'] . '/' . ActionPathFormatter::format($action);
$moduleRouteAction[] = $route['frontName'] . '/' . ActionPathFormatter::format($action['file']);
$moduleRouteAction[] = implode(',', $this->getSupportedHttpVerbs($action['class']));

$table[] = $moduleRouteAction;
}
}
}
return $table;
}

/**
* Returns the HTTP verb of route by class of action
*
* @param string $actionClass
* @return string[]
*/
private function getSupportedHttpVerbs(string $actionClass): array
{
$verbs = [];

if (is_a($actionClass, HttpConnectActionInterface::class, true)) {
$verbs[] = 'CONNECT';
}

if (is_a($actionClass, HttpDeleteActionInterface::class, true)) {
$verbs[] = 'DELETE';
}

if (is_a($actionClass, HttpGetActionInterface::class, true)) {
$verbs[] = 'GET';
}

if (is_a($actionClass, HttpOptionsActionInterface::class, true)) {
$verbs[] = 'OPTIONS';
}

if (is_a($actionClass, HttpPatchActionInterface::class, true)) {
$verbs[] = 'PATCH';
}

if (is_a($actionClass, HttpPostActionInterface::class, true)) {
$verbs[] = 'POST';
}

if (is_a($actionClass, HttpPropfindActionInterface::class, true)) {
$verbs[] = 'PROPFIND';
}

if (is_a($actionClass, HttpPutActionInterface::class, true)) {
$verbs[] = 'PUT';
}

if (is_a($actionClass, HttpTraceActionInterface::class, true)) {
$verbs[] = 'TRACE';
}

if (count($verbs) === 0) {
return ['GET', 'POST'];
}

return $verbs;
}
}
9 changes: 0 additions & 9 deletions tests/bats/functional_core_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,3 @@ setup() {
assert_output --partial "vcl 4.0"
}

@test "Command: route:list -m Magento_Backend -a adminhtml" {
run $BIN "route:list" -m Magento_Backend -a adminhtml
assert_output --partial "admin/dashboard/index"
}

@test "Command: route:list -m Magento_Multishipping -a frontend" {
run $BIN "route:list" -m Magento_Multishipping -a frontend
assert_output --partial "multishipping/checkout_address/editaddress"
}
12 changes: 12 additions & 0 deletions tests/bats/functional_magerun_commands.bats
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,18 @@ function cleanup_files_in_magento() {
assert_output --partial "Compress"
}

@test "Command: route:list -m Magento_Backend -a adminhtml" {
run $BIN "route:list" -m Magento_Backend -a adminhtml
assert_output --partial "admin/dashboard/index"
assert_output --partial "GET,POST"
}

@test "Command: route:list -m Magento_Multishipping -a frontend" {
run $BIN "route:list" -m Magento_Multishipping -a frontend
assert_output --partial "multishipping/checkout_address/editaddress"
assert_output --partial "GET,POST"
}

@test "Command: script:repo:list" {
run $BIN "script:repo:list"
assert_output --partial "Script"
Expand Down

0 comments on commit fd4418d

Please sign in to comment.