simplify serverside codegen demo
authorgggeek <giunta.gaetano@gmail.com>
Tue, 10 Jan 2023 12:38:39 +0000 (12:38 +0000)
committergggeek <giunta.gaetano@gmail.com>
Tue, 10 Jan 2023 12:38:39 +0000 (12:38 +0000)
demo/server/codegen.php

index 103b2e6..082e7ae 100644 (file)
@@ -3,7 +3,6 @@ require_once __DIR__ . "/_prepend.php";
 
 require_once __DIR__.'/methodProviders/CommentManager.php';
 
-use PhpXmlRpc\Server;
 use PhpXmlRpc\Wrapper;
 
 $cm = new CommentManager();
@@ -17,14 +16,14 @@ $code = $w->wrapPhpClass(
     )
 );
 
-// the generated code does not have an autoloader included - we need to add in one
-$autoloader = __DIR__ . "/_prepend.php";
-
 $targetClassFile = '/tmp/MyServerClass.php';
-$targetDispatchMapFile = '/tmp/myServerDM.php';
+$targetServerFile = '/tmp/myServer.php';
 
 // generate a file with a class definition
 
+// the generated code does not have an autoloader included - we need to add in one
+$autoloader = __DIR__ . "/_prepend.php";
+
 file_put_contents($targetClassFile,
     "<?php\n\n" .
     "require_once '$autoloader';\n\n" .
@@ -40,29 +39,31 @@ foreach($code as $methodName => $methodDef) {
 }
 file_put_contents($targetClassFile, "}\n", FILE_APPEND) || die('uh oh');
 
-// and a separate file with the dispatch map
+// generate the separate file with the xml-rpc server and dispatch map
 
-file_put_contents($targetDispatchMapFile,
+file_put_contents($targetServerFile,
     "<?php\n\n" .
+
     "require_once '$autoloader';\n\n" .
-    "return " . var_export($code, true) . ";\n"
+
+    "require_once '$targetClassFile';\n\n" .
+
+    // NB: since we are running the generated code within the same script, the existing CommentManager instance will be
+    // available for usage by the methods of MyServerClass, as we keep a reference to them within the variable Wrapper::$objHolder
+    // but if you are generating a php file for later use, it is up to you to initialize that variables with a
+    // CommentManager instance:
+    //     $cm = new CommentManager();
+    //     Wrapper::$objHolder['xmlrpc_CommentManager_addComment'] = $cm;
+    //     Wrapper::$objHolder['xmlrpc_CommentManager_getComments'] = $cm;
+
+    '$dm = ' . var_export($code, true) . ";\n" .
+    '$s = new \PhpXmlRpc\Server($dm, false);' . "\n" .
+    '$s->setDebug(2);' . "\n" .
+    '$s->exception_handling = 1;' . "\n" .
+    '$s->service();' . "\n"
 ) || die('uh oh');
 
 // test that everything worked by running it in realtime
 // *** NB do not do this in prod! The whole concept of code-generation is to do it offline using console scripts/ci/cd ***
 
-include_once $targetClassFile;
-
-// NB: since we are running the generated code within the same script, the existing CommentManager instance will be
-// available for usage by the methods of MyServerClass, as we keep a reference to them within the variable Wrapper::$objHolder
-// but if you are generating a php file for later use, it is up to you to initialize that variables with a
-// CommentManager instance:
-//     $cm = new CommentManager();
-//     Wrapper::$objHolder['xmlrpc_CommentManager_addComment'] = $cm;
-//     Wrapper::$objHolder['xmlrpc_CommentManager_getComments'] = $cm;
-
-$dm = include_once $targetDispatchMapFile;
-$s = new Server($dm, false);
-$s->setDebug(2);
-$s->exception_handling = 1;
-$s->service();
+include $targetServerFile;