From 9dcf812914208adc3f8920e65560413332f13e37 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Fri, 13 Feb 2015 13:05:51 -0500
Subject: [PATCH] Smarter namespace handling in factory generation. - Progress
 on VUFIND-1035.

---
 .../VuFindConsole/Controller/GenerateController.php  | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php b/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
index ba5d86ba2f4..43856b21b71 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/GenerateController.php
@@ -152,7 +152,9 @@ class GenerateController extends AbstractBase
         $method = MethodGenerator::fromReflection(
             $oldReflection->getMethod($factoryMethod)
         );
-        $this->createServiceClassAndUpdateFactory($method, $module);
+        $this->createServiceClassAndUpdateFactory(
+            $method, $oldReflection->getNamespaceName(), $module
+        );
         $generator->addMethodFromGenerator($method);
         $this->writeClass($generator, $module, true, $skipBackup);
 
@@ -164,13 +166,14 @@ class GenerateController extends AbstractBase
      * a new factory for the subclass.
      *
      * @param MethodGenerator $method Method to modify
+     * @param string          $ns     Namespace of old factory
      * @param string          $module Module in which to make changes
      *
      * @return void
      * @throws \Exception
      */
     protected function createServiceClassAndUpdateFactory(MethodGenerator $method,
-        $module
+        $ns, $module
     ) {
         $body = $method->getBody();
         $regex = '/new\s+([\w\\\\]*)\s*\(/m';
@@ -181,7 +184,10 @@ class GenerateController extends AbstractBase
             throw new \Exception("Found $count class names; expected 1.");
         }
         $className = $classNames[0];
-        $newClass = $this->createSubclassInModule($className, $module);
+        // Figure out fully qualified name for purposes of createSubclassInModule():
+        $fqClassName = (substr($className, 0, 1) != '\\')
+            ? "$ns\\$className" : $className;
+        $newClass = $this->createSubclassInModule($fqClassName, $module);
         $body = preg_replace(
             '/new\s+' . addslashes($className) . '\s*\(/m',
             'new \\' . $newClass . '(',
-- 
GitLab