Issues (320)

demo/client/codegen.php (1 issue)

Labels
Severity
1
<?php
2
require_once __DIR__ . "/_prepend.php";
3
4
/**
5
 * Demoing the code-generation capabilities of the library: create a client class which exposes a bunch of methods
6
 * advertised by a remote xml-rpc server.
7
 */
8
9
/// @todo add an html header with links to view-source
10
11
use PhpXmlRpc\Client;
12
use PhpXmlRpc\Wrapper;
13
14
$w = new Wrapper();
15
$code = $w->wrapXmlrpcServer(
16
    new Client(XMLRPCSERVER),
17
    array(
18
        'return_source' => true,
19
        'new_class_name' => 'MyClient',
20
        'method_filter' => '/^examples\./',
21
        'simple_client_copy' => true,
22
        // this is used to encode php NULL values into xml-rpc <NIL/> elements. If the partner does not support that, disable it
23
        'encode_nulls' => true,
24
        'throw_on_fault' => true,
25
    )
26
);
27
28
// the generated code does not have an autoloader included - we need to add in one
29
$autoloader = __DIR__ . "/_prepend.php";
30
31
$targetFile = '/tmp/MyClient.php';
32
$generated = file_put_contents($targetFile,
33
    "<?php\n\n" .
34
    "require_once '$autoloader';\n\n" .
35
    $code['code']
36
);
37
38
if (!$generated) {
39
    die("uh oh");
40
}
41
42
// *** NB take care when doing  this in prod! ***
43
// There's a race condition here - what if someone replaces the file we just created, before we include it?
44
// You should at the very least make sure that filesystem permissions are tight, so that only the authorized user
45
// accounts can write into the folder where $targetFile is created.
46
// You might even want to disallow php code executed from the webserver from generating new php code for direct inclusion,
47
// and only allow a cli process to do that
48
49
include($targetFile);
50
51
$client = new MyClient();
0 ignored issues
show
The type MyClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
$sorted = $client->examples_sortByAge(array(
53
    array('name' => 'Dave', 'age' => 24),
54
    array('name' => 'Edd',  'age' => 45),
55
    array('name' => 'Joe',  'age' => 37),
56
    array('name' => 'Fred', 'age' => 27),
57
));
58
59
echo "Sorted array:\n";
60
print_r($sorted);
61