Browse Source

add new column to bin/cake routes command to indicate if the given controller+action combo is actually vali

Kevin Pfeifer 1 year ago
parent
commit
b5fcb5bbe0

+ 6 - 1
src/Command/RoutesCommand.php

@@ -44,7 +44,10 @@ class RoutesCommand extends Command
      */
     public function execute(Arguments $args, ConsoleIo $io): ?int
     {
-        $header = ['Route name', 'URI template', 'Plugin', 'Prefix', 'Controller', 'Action', 'Method(s)'];
+        $header = [
+            'Route name', 'URI template', 'Plugin', 'Prefix',
+            'Controller', 'Action', 'Valid Action', 'Method(s)',
+        ];
         if ($args->getOption('with-middlewares') || $args->getOption('verbose')) {
             $header[] = 'Middlewares';
         }
@@ -66,6 +69,7 @@ class RoutesCommand extends Command
                 $route->defaults['prefix'] ?? '',
                 $route->defaults['controller'] ?? '',
                 $route->defaults['action'] ?? '',
+                $route->isActionPresent() ? '' : 'X',
                 implode(', ', $methods),
             ];
 
@@ -117,6 +121,7 @@ class RoutesCommand extends Command
                         $route->defaults['prefix'] ?? '',
                         $route->defaults['controller'] ?? '',
                         $route->defaults['action'] ?? '',
+                        $route->isActionPresent() ? '' : 'X',
                         implode(', ', $methods),
                     ];
 

+ 27 - 0
src/Routing/Route/Route.php

@@ -16,6 +16,7 @@ declare(strict_types=1);
  */
 namespace Cake\Routing\Route;
 
+use Cake\Core\App;
 use Cake\Core\Exception\CakeException;
 use Cake\Http\Exception\BadRequestException;
 use InvalidArgumentException;
@@ -921,6 +922,32 @@ class Route
     }
 
     /**
+     * @return bool
+     */
+    public function isActionPresent(): bool
+    {
+        $action = $this->defaults['action'] ?? null;
+        $controller = $this->defaults['controller'] ?? null;
+        if (!$action || !$controller) {
+            // generic actions and controllers can't be checked
+            return true;
+        }
+
+        $pluginPath = '';
+        if ($this->defaults['plugin']) {
+            $pluginPath = $this->defaults['plugin'] . '.';
+        }
+        $controllerClass = App::className($pluginPath . $controller, 'Controller', 'Controller');
+
+        if (!$controllerClass) {
+            // Controller class couldn't be found for some reason
+            return true;
+        }
+
+        return method_exists($controllerClass, $action);
+    }
+
+    /**
      * Set state magic method to support var_export
      *
      * This method helps for applications that want to implement

+ 8 - 0
tests/TestCase/Command/RoutesCommandTest.php

@@ -73,6 +73,7 @@ class RoutesCommandTest extends TestCase
             '<info>Prefix</info>',
             '<info>Controller</info>',
             '<info>Action</info>',
+            '<info>Valid Action</info>',
             '<info>Method(s)</info>',
         ]);
         $this->assertOutputContainsRow([
@@ -82,6 +83,7 @@ class RoutesCommandTest extends TestCase
             '',
             'Articles',
             'index',
+            'X',
             '',
         ]);
         $this->assertOutputContainsRow([
@@ -92,6 +94,7 @@ class RoutesCommandTest extends TestCase
             '',
             'index',
             '',
+            '',
         ]);
         $this->assertOutputContainsRow([
             'testName',
@@ -101,6 +104,7 @@ class RoutesCommandTest extends TestCase
             'Tests',
             'index',
             '',
+            '',
         ]);
     }
 
@@ -118,6 +122,7 @@ class RoutesCommandTest extends TestCase
             '<info>Prefix</info>',
             '<info>Controller</info>',
             '<info>Action</info>',
+            '<info>Valid Action</info>',
             '<info>Method(s)</info>',
             '<info>Middlewares</info>',
             '<info>Defaults</info>',
@@ -129,6 +134,7 @@ class RoutesCommandTest extends TestCase
             '',
             'Articles',
             'index',
+            'X',
             '',
             'dumb, sample',
             '{"action":"index","controller":"Articles","plugin":null}',
@@ -165,6 +171,7 @@ class RoutesCommandTest extends TestCase
             '',
             'Articles',
             'index',
+            'X',
             '',
             'dumb, sample',
         ]);
@@ -346,6 +353,7 @@ class RoutesCommandTest extends TestCase
             '<info>Prefix</info>',
             '<info>Controller</info>',
             '<info>Action</info>',
+            '<info>Valid Action</info>',
             '<info>Method(s)</info>',
         ]);
         $this->assertOutputContainsRow([