|
@@ -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 |
|
@@ -14,7 +14,7 @@ discard block |
|
|
block discarded – undo |
|
14
|
14
|
|
|
15
|
15
|
if (isset($_GET['generate']) && $_GET['generate']) { |
|
16
|
16
|
// CommentManager is the "xml-rpc-unaware" class, whose methods we want to make accessible via xml-rpc calls |
|
17
|
|
-$cm = new CommentManager(sys_get_temp_dir() . "/comments.db"); |
|
|
17
|
+$cm = new CommentManager(sys_get_temp_dir()."/comments.db"); |
|
18
|
18
|
|
|
19
|
19
|
// analyze the CommentManager instance and generate both code defining stub-methods and a dispatch map for the xml-rpc Server |
|
20
|
20
|
$w = new Wrapper(); |
|
@@ -32,55 +32,55 @@ discard block |
|
|
block discarded – undo |
|
32
|
32
|
// and a controller, to be accessed from the internet. This split allows to a) hand-edit the controller code if needed, |
|
33
|
33
|
// and b) later regenerate the stub-methods-holder and dispatch map without touching the controller. |
|
34
|
34
|
// NB: good security practices dictate that none of those files should be writeable by the webserver user account |
|
35
|
|
-$targetClassFile = sys_get_temp_dir() . '/MyServerClass.php'; |
|
36
|
|
-$targetDispatchMapFile = sys_get_temp_dir() . '/myServerDispatchMap.php'; |
|
37
|
|
-$targetControllerFile = sys_get_temp_dir() . '/myServerController.php'; |
|
|
35
|
+$targetClassFile = sys_get_temp_dir().'/MyServerClass.php'; |
|
|
36
|
+$targetDispatchMapFile = sys_get_temp_dir().'/myServerDispatchMap.php'; |
|
|
37
|
+$targetControllerFile = sys_get_temp_dir().'/myServerController.php'; |
|
38
|
38
|
|
|
39
|
39
|
// generate a file with a class definition |
|
40
|
40
|
|
|
41
|
41
|
// the generated code does not have an autoloader included - we need to add in one |
|
42
|
|
-$autoloader = __DIR__ . "/_prepend.php"; |
|
|
42
|
+$autoloader = __DIR__."/_prepend.php"; |
|
43
|
43
|
|
|
44
|
44
|
file_put_contents($targetClassFile, |
|
45
|
|
- "<?php\n\n" . |
|
46
|
|
- "require_once '$autoloader';\n\n" . |
|
|
45
|
+ "<?php\n\n". |
|
|
46
|
+ "require_once '$autoloader';\n\n". |
|
47
|
47
|
"class MyServerClass\n{\n\n" |
|
48
|
48
|
) || die('uh oh'); |
|
49
|
49
|
|
|
50
|
50
|
// we mangle a bit the code we get from wrapPhpClass to turn it into a php class definition instead of a bunch of functions |
|
51
|
51
|
|
|
52
|
52
|
foreach ($code as $methodName => $methodDef) { |
|
53
|
|
- file_put_contents($targetClassFile, ' ' . str_replace(array('function ', "\n"), array('public static function ', "\n "), $methodDef['source']) . "\n\n", FILE_APPEND) || die('uh oh'); |
|
54
|
|
- $code[$methodName]['function'] = 'MyServerClass::' . $methodDef['function']; |
|
|
53
|
+ file_put_contents($targetClassFile, ' '.str_replace(array('function ', "\n"), array('public static function ', "\n "), $methodDef['source'])."\n\n", FILE_APPEND) || die('uh oh'); |
|
|
54
|
+ $code[$methodName]['function'] = 'MyServerClass::'.$methodDef['function']; |
|
55
|
55
|
unset($code[$methodName]['source']); |
|
56
|
56
|
} |
|
57
|
57
|
file_put_contents($targetClassFile, "}\n", FILE_APPEND) || die('uh oh'); |
|
58
|
58
|
|
|
59
|
59
|
// generate separate files with the xml-rpc server instantiation and its dispatch map |
|
60
|
60
|
|
|
61
|
|
-file_put_contents($targetDispatchMapFile, "<?php\n\nreturn " . var_export($code, true) . ";\n"); |
|
|
61
|
+file_put_contents($targetDispatchMapFile, "<?php\n\nreturn ".var_export($code, true).";\n"); |
|
62
|
62
|
|
|
63
|
63
|
file_put_contents($targetControllerFile, |
|
64
|
|
- "<?php\n\n" . |
|
|
64
|
+ "<?php\n\n". |
|
65
|
65
|
|
|
66
|
|
- "// autoloader\n" . |
|
67
|
|
- "require_once '$autoloader';\n\n" . |
|
|
66
|
+ "// autoloader\n". |
|
|
67
|
+ "require_once '$autoloader';\n\n". |
|
68
|
68
|
|
|
69
|
|
- "require_once '$targetClassFile';\n\n" . |
|
|
69
|
+ "require_once '$targetClassFile';\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
|
|
- "\$cm = new CommentManager(sys_get_temp_dir() . '/comments.db');\n" . |
|
76
|
|
- "\PhpXmlRpc\Wrapper::holdObject('*', \$cm);\n\n" . |
|
77
|
|
- |
|
78
|
|
- "\$dm = require_once '$targetDispatchMapFile';\n" . |
|
79
|
|
- '$s = new \PhpXmlRpc\Server($dm, false);' . "\n\n" . |
|
80
|
|
- '// NB: do not leave these 2 debug lines enabled on publicly accessible servers!' . "\n" . |
|
81
|
|
- '$s->setOption(\PhpXmlRpc\Server::OPT_DEBUG, 2);' . "\n" . |
|
82
|
|
- '$s->setOption(\PhpXmlRpc\Server::OPT_EXCEPTION_HANDLING, 1);' . "\n\n" . |
|
83
|
|
- '$s->service();' . "\n" |
|
|
75
|
+ "\$cm = new CommentManager(sys_get_temp_dir() . '/comments.db');\n". |
|
|
76
|
+ "\PhpXmlRpc\Wrapper::holdObject('*', \$cm);\n\n". |
|
|
77
|
+ |
|
|
78
|
+ "\$dm = require_once '$targetDispatchMapFile';\n". |
|
|
79
|
+ '$s = new \PhpXmlRpc\Server($dm, false);'."\n\n". |
|
|
80
|
+ '// NB: do not leave these 2 debug lines enabled on publicly accessible servers!'."\n". |
|
|
81
|
+ '$s->setOption(\PhpXmlRpc\Server::OPT_DEBUG, 2);'."\n". |
|
|
82
|
+ '$s->setOption(\PhpXmlRpc\Server::OPT_EXCEPTION_HANDLING, 1);'."\n\n". |
|
|
83
|
+ '$s->service();'."\n" |
|
84
|
84
|
) || die('uh oh'); |
|
85
|
85
|
|
|
86
|
86
|
echo "Code generated"; |
|
@@ -90,6 +90,6 @@ discard block |
|
|
block discarded – undo |
|
90
|
90
|
|
|
91
|
91
|
// *** NB do not do this in prod! The whole concept of code-generation is to do it offline using console scripts/ci/cd *** |
|
92
|
92
|
|
|
93
|
|
- $targetControllerFile = sys_get_temp_dir() . '/myServerController.php'; |
|
|
93
|
+ $targetControllerFile = sys_get_temp_dir().'/myServerController.php'; |
|
94
|
94
|
require $targetControllerFile; |
|
95
|
95
|
} |