From c9c945f88b98f53a588cce1608a5dd6553975a6d Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Thu, 12 Mar 2015 12:57:34 -0400
Subject: [PATCH] Added command line dynamic route generator.

---
 module/VuFindConsole/Module.php               |  1 +
 .../Controller/GenerateController.php         | 47 +++++++++++++++++++
 2 files changed, 48 insertions(+)

diff --git a/module/VuFindConsole/Module.php b/module/VuFindConsole/Module.php
index 1a8f4efdfc7..0bbe894155c 100644
--- a/module/VuFindConsole/Module.php
+++ b/module/VuFindConsole/Module.php
@@ -79,6 +79,7 @@ class Module implements \Zend\ModuleManager\Feature\ConsoleUsageProviderInterfac
         return [
             'generate extendservice' => 'Override a service with a new child class',
             'generate nontabrecordaction' => 'Add routes for non-tab record action',
+            'generate dynamicroute' => 'Add a dynamic route',
             'generate recordroute' => 'Add a record route',
             'generate staticroute' => 'Add a static route',
             'harvest harvest_oai' => 'OAI-PMH harvester',
diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php b/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
index 01d630a3115..0b12e6d3c30 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
@@ -43,6 +43,53 @@ use Zend\Console\Console;
  */
 class GenerateController extends AbstractBase
 {
+    /**
+     * Add a new dynamic route definition
+     *
+     * @return \Zend\Console\Response
+     */
+    public function dynamicrouteAction()
+    {
+        $argv = $this->consoleOpts->getRemainingArgs();
+        if (!isset($argv[3])) {
+            Console::writeLine(
+                "Usage: {$_SERVER['argv'][0]} [route] [controller] [action]"
+                . " [target_module]"
+            );
+            Console::writeLine(
+                "\troute - the route name (used by router), e.g. customList"
+            );
+            Console::writeLine(
+                "\tcontroller - the controller name (used in URL), e.g. MyResearch"
+            );
+            Console::writeLine(
+                "\taction - the action and segment params, e.g. CustomList/[:id]"
+            );
+            Console::writeLine(
+                "\ttarget_module - the module where the new route will be generated"
+            );
+            return $this->getFailureResponse();
+        }
+
+        $route = $argv[0];
+        $controller = $argv[1];
+        $action = $argv[2];
+        $module = $argv[3];
+
+        // Create backup of configuration
+        $configPath = $this->getModuleConfigPath($module);
+        $this->backUpFile($configPath);
+
+        // Append the route
+        $config = include $configPath;
+        $routeGenerator = new \VuFind\Route\RouteGenerator();
+        $routeGenerator->addDynamicRoute($config, $route, $controller, $action);
+
+        // Write updated configuration
+        $this->writeModuleConfig($configPath, $config);
+        return $this->getSuccessResponse();
+    }
+
     /**
      * Extend an existing service
      *
-- 
GitLab