From 8ac89fb681d03a8ab5b62b643d0068d70f108240 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Wed, 7 Feb 2018 09:57:05 -0500
Subject: [PATCH] Add --extendfactory flag.

---
 module/VuFindConsole/config/module.config.php |  2 +-
 .../Controller/GenerateController.php         | 13 ++++++++++---
 .../Generator/GeneratorTools.php              | 19 +++++++++++++------
 3 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/module/VuFindConsole/config/module.config.php b/module/VuFindConsole/config/module.config.php
index 6debc0e4420..1cffe399803 100644
--- a/module/VuFindConsole/config/module.config.php
+++ b/module/VuFindConsole/config/module.config.php
@@ -52,7 +52,7 @@ $config = [
 $routes = [
     'compile/theme' => 'compile theme [--force] [<source>] [<target>]',
     'generate/dynamicroute' => 'generate dynamicroute [<name>] [<newController>] [<newAction>] [<module>]',
-    'generate/extendclass' => 'generate extendclass [<class>] [<target>]',
+    'generate/extendclass' => 'generate extendclass [--extendfactory] [<class>] [<target>]',
     'generate/extendservice' => 'generate extendservice [<source>] [<target>]',
     'generate/nontabrecordaction' => 'generate nontabrecordaction [<newAction>] [<module>]',
     'generate/recordroute' => 'generate recordroute [<base>] [<newController>] [<module>]',
diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php b/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
index aff4b6f8979..1ea172e5f5f 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
@@ -98,10 +98,16 @@ class GenerateController extends AbstractBase
         $request = $this->getRequest();
         $class = $request->getParam('class');
         $target = $request->getParam('target');
+        $extendFactory = $request->getParam('extendfactory');
+
         if (empty($class) || empty($target)) {
             Console::writeLine(
                 'Usage: ' . $request->getScriptName() . ' generate extendclass'
-                . ' [class_name] [target_module]'
+                . ' [--extendfactory] [class_name] [target_module]'
+            );
+            Console::writeLine(
+                "\t--extendfactory - optional switch; when set, subclass "
+                . 'the factory; otherwise, use existing factory'
             );
             Console::writeLine(
                 "\tclass_name - the name of the class you wish to extend"
@@ -113,8 +119,9 @@ class GenerateController extends AbstractBase
         }
 
         try {
-            $this->getGeneratorTools()
-                ->extendClass($this->serviceLocator, $class, $target);
+            $this->getGeneratorTools()->extendClass(
+                $this->serviceLocator, $class, $target, $extendFactory
+            );
         } catch (\Exception $e) {
             Console::writeLine($e->getMessage());
             return $this->getFailureResponse();
diff --git a/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php b/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php
index 9c22031c563..df6431668da 100644
--- a/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php
+++ b/module/VuFindConsole/src/VuFindConsole/Generator/GeneratorTools.php
@@ -66,16 +66,18 @@ class GeneratorTools
      * Extend a class defined somewhere in the service manager or its child
      * plugin managers.
      *
-     * @param ContainerInterface $container Service manager
-     * @param string             $class     Class name to extend
-     * @param string             $target    Target module in which to create new
+     * @param ContainerInterface $container     Service manager
+     * @param string             $class         Class name to extend
+     * @param string             $target        Target module in which to create new
      * service
+     * @param bool               $extendFactory Should we extend the factory?
      *
      * @return bool
      * @throws \Exception
      */
-    public function extendClass(ContainerInterface $container, $class, $target)
-    {
+    public function extendClass(ContainerInterface $container, $class, $target,
+        $extendFactory = false
+    ) {
         // Set things up differently depending on whether this is a top-level
         // service or a class in a plugin manager.
         if ($container->has($class)) {
@@ -97,10 +99,15 @@ class GeneratorTools
         // Create the custom subclass.
         $newClass = $this->createSubclassInModule($class, $target);
 
+        // Create the custom factory only if requested.
+        $newFactory = $extendFactory
+            ? $this->createSubclassInModule($factory, $target)
+            : $factory;
+
         // Finalize the local module configuration -- create a factory for the
         // new class, and set up the new class as an alias for the old class.
         $factoryPath = array_merge($configPath, ['factories', $newClass]);
-        $this->writeNewConfig($factoryPath, $factory, $target);
+        $this->writeNewConfig($factoryPath, $newFactory, $target);
         $aliasPath = array_merge($configPath, ['aliases', $class]);
         // Don't back up the config twice -- the first backup from the previous
         // write operation is sufficient.
-- 
GitLab