Passed
Push — master ( e78be9...e5dcc4 )
by Gaetano
09:29 queued 03:43
created
demo/server/codegen.php 1 patch
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-require_once __DIR__ . "/_prepend.php";
2
+require_once __DIR__."/_prepend.php";
3 3
 
4 4
 /**
5 5
  * Demoing the code-generation capabilities of the library: create all that is required to expose as xml-rpc methods
@@ -16,7 +16,7 @@  discard block
 block discarded – undo
16 16
     // *** NB do not do this in prod! The whole concept of code-generation is to do it offline using console scripts/ci/cd ***
17 17
 
18 18
     // CommentManager is the "xml-rpc-unaware" class, whose methods we want to make accessible via xml-rpc calls
19
-    $cm = new CommentManager(sys_get_temp_dir() . "/comments.db");
19
+    $cm = new CommentManager(sys_get_temp_dir()."/comments.db");
20 20
 
21 21
     // analyze the CommentManager instance and generate both code defining stub-methods and a dispatch map for the xml-rpc Server
22 22
     $w = new Wrapper();
@@ -34,59 +34,59 @@  discard block
 block discarded – undo
34 34
     // and a controller, to be accessed from the internet. This split allows to a) hand-edit the controller code if needed,
35 35
     // and b) later regenerate the stub-methods-holder and dispatch map without touching the controller.
36 36
     // NB: good security practices dictate that none of those files should be writeable by the webserver user account
37
-    $targetClassFile = sys_get_temp_dir() . '/MyServerClass.php';
38
-    $targetDispatchMapFile = sys_get_temp_dir() . '/myServerDispatchMap.php';
39
-    $targetControllerFile = sys_get_temp_dir() . '/myServerController.php';
37
+    $targetClassFile = sys_get_temp_dir().'/MyServerClass.php';
38
+    $targetDispatchMapFile = sys_get_temp_dir().'/myServerDispatchMap.php';
39
+    $targetControllerFile = sys_get_temp_dir().'/myServerController.php';
40 40
 
41 41
     // generate a file with a class definition
42 42
 
43 43
     // the generated code does not have an autoloader included - we need to add in one
44
-    $autoloader = __DIR__ . "/_prepend.php";
44
+    $autoloader = __DIR__."/_prepend.php";
45 45
 
46 46
     file_put_contents($targetClassFile,
47
-        "<?php\n\n" .
48
-        "require_once '$autoloader';\n\n" .
47
+        "<?php\n\n".
48
+        "require_once '$autoloader';\n\n".
49 49
         "class MyServerClass\n{\n\n"
50 50
     ) || die('uh oh');
51 51
 
52 52
     // we mangle a bit the code we get from wrapPhpClass to turn it into a php class definition instead of a bunch of functions
53 53
 
54 54
     foreach ($code as $methodName => $methodDef) {
55
-        file_put_contents($targetClassFile, '  ' . str_replace(array('function ', "\n"), array('public static function ', "\n  "), $methodDef['source']) . "\n\n", FILE_APPEND) || die('uh oh');
56
-        $code[$methodName]['function'] = 'MyServerClass::' . $methodDef['function'];
55
+        file_put_contents($targetClassFile, '  '.str_replace(array('function ', "\n"), array('public static function ', "\n  "), $methodDef['source'])."\n\n", FILE_APPEND) || die('uh oh');
56
+        $code[$methodName]['function'] = 'MyServerClass::'.$methodDef['function'];
57 57
         unset($code[$methodName]['source']);
58 58
     }
59 59
     file_put_contents($targetClassFile, "}\n", FILE_APPEND) || die('uh oh');
60 60
 
61 61
     // generate separate files with the xml-rpc server instantiation and its dispatch map
62 62
 
63
-    file_put_contents($targetDispatchMapFile, "<?php\n\nreturn " . var_export($code, true) . ";\n");
63
+    file_put_contents($targetDispatchMapFile, "<?php\n\nreturn ".var_export($code, true).";\n");
64 64
 
65 65
     file_put_contents($targetControllerFile,
66
-        "<?php\n\n" .
66
+        "<?php\n\n".
67 67
 
68
-        "// class autoloader\n" .
69
-        "require_once '$autoloader';\n\n" .
68
+        "// class autoloader\n".
69
+        "require_once '$autoloader';\n\n".
70 70
 
71 71
         // NB: if we were running the generated code within the same script, the existing CommentManager instance would be
72 72
         // available for usage by the methods of MyServerClass, as we keep a reference to them within the variable Wrapper::$objHolder,
73 73
         // but since we are generating a php file for later use, it is up to us to initialize that variable with a
74 74
         // CommentManager instance
75
-        "// business logic setup\n" .
76
-        "\$cm = new CommentManager(sys_get_temp_dir() . '/comments.db');\n" .
77
-        "// save reference to the biz logic object so that it can be used by the code in the generated class\n" .
78
-        "\PhpXmlRpc\Wrapper::holdObject('*', \$cm);\n\n" .
79
-
80
-        "// the generated class implementing the json-rpc methods is not namespaced, load it explicitly\n" .
81
-        "require_once '$targetClassFile';\n\n" .
82
-
83
-        "// load the dispatch map and feed it to the json-rpc server\n" .
84
-        "\$dm = require_once '$targetDispatchMapFile';\n" .
85
-        '$s = new \PhpXmlRpc\Server($dm, false);' . "\n\n" .
86
-        '// NB: do not leave these 2 debug lines enabled on publicly accessible servers!' . "\n" .
87
-        '$s->setOption(\PhpXmlRpc\Server::OPT_DEBUG, 2);' . "\n" .
88
-        '$s->setOption(\PhpXmlRpc\Server::OPT_EXCEPTION_HANDLING, 1);' . "\n\n" .
89
-        '$s->service();' . "\n"
75
+        "// business logic setup\n".
76
+        "\$cm = new CommentManager(sys_get_temp_dir() . '/comments.db');\n".
77
+        "// save reference to the biz logic object so that it can be used by the code in the generated class\n".
78
+        "\PhpXmlRpc\Wrapper::holdObject('*', \$cm);\n\n".
79
+
80
+        "// the generated class implementing the json-rpc methods is not namespaced, load it explicitly\n".
81
+        "require_once '$targetClassFile';\n\n".
82
+
83
+        "// load the dispatch map and feed it to the json-rpc server\n".
84
+        "\$dm = require_once '$targetDispatchMapFile';\n".
85
+        '$s = new \PhpXmlRpc\Server($dm, false);'."\n\n".
86
+        '// NB: do not leave these 2 debug lines enabled on publicly accessible servers!'."\n".
87
+        '$s->setOption(\PhpXmlRpc\Server::OPT_DEBUG, 2);'."\n".
88
+        '$s->setOption(\PhpXmlRpc\Server::OPT_EXCEPTION_HANDLING, 1);'."\n\n".
89
+        '$s->service();'."\n"
90 90
     ) || die('uh oh');
91 91
 
92 92
     echo "Code generated";
@@ -94,6 +94,6 @@  discard block
 block discarded – undo
94 94
     // test that everything worked by running it in realtime (note that this script will return an xml-rpc error message if
95 95
     // run from the command line, as the server will find no xml-rpc payload to operate on)
96 96
 
97
-    $targetControllerFile = sys_get_temp_dir() . '/myServerController.php';
97
+    $targetControllerFile = sys_get_temp_dir().'/myServerController.php';
98 98
     require $targetControllerFile;
99 99
 }
Please login to merge, or discard this patch.