12 require_once(__DIR__ .
"/generator-commons.php");
23 $shouldWriteToDisc = (isset($GLOBALS[
'argv'][1]) && $GLOBALS[
'argv'][1] ===
'--no-file-generation') ?
false :
true;
31 'destination' =>
'./src/Web3Interface.php',
32 'class' =>
'PhpInterface',
33 'name' =>
'Web3Interface',
34 'group' =>
"@ingroup generated\n * @ingroup interfaces" 38 'destination' =>
'./src/Web3Methods.php',
39 'class' =>
'PhpTrait',
40 'name' =>
'Web3Methods',
41 'group' =>
'@ingroup generated' 45 foreach (
$conf as $cnf) {
46 echo
"### GENERATING ETHEREUM METHODS INTERFACE ###\n";
47 echo
"# File generated " . $cnf[
'destination'] .
"\n";
49 $group = $cnf[
"group"];
67 if ($cnf[
'class'] ===
'PhpTrait') {
68 $code =
new PhpTrait();
70 if ($cnf[
'class'] ===
'PhpInterface') {
71 $code =
new PhpInterface();
75 $code->setQualifiedName(
'\\Ethereum\\' . $cnf[
'name'])
76 ->setDescription(array(
77 'Ethereum JsonRPC Methods.',
79 'Interface is generated by scripts/generate-methods.php based on resources/ethjs-schema.json.',
80 'Methods are actually implemented with [method overloading](http://php.net/manual/en/language.oop5.overloading.php#object.call) using __call().',
86 foreach (
$schema[
'methods'] as $method_name => $params) {
88 # printMe("\n# Generate $method_name"); 91 $valid_params = $params[0];
92 # printMe('Valid params', $valid_params); 97 if (count($valid_params)) {
100 foreach ($valid_params as $i => $type) {
101 $primitiveType = EthD::typeMap($type);
102 $paramType = $primitiveType ? $primitiveType : $type;
103 $methodParams[] = PhpParameter::create(
"arg" . ($i + 1))
104 ->setType($paramType);
111 $returnType = $params[1];
112 $returnTypeDescription =
'';
113 if (is_array($returnType)) {
114 if (EthD::typeMap($returnType[0])) {
115 $arrayOfType = EthD::typeMap($returnType[0]);
117 $arrayOfType = $returnType[0];
119 $returnType =
"array";
120 $returnTypeDescription =
" Array of " . $arrayOfType;
121 }
else if (EthD::typeMap($returnType)) {
122 $returnType = EthD::typeMap($returnType);
129 # printMe('Return type', $returnTypeDescription ? $returnTypeDescription : $returnType); 132 $code->setMethod(PhpMethod::create($method_name)
133 ->setDescription(array(
134 "Generated method $method_name().",
136 "See [Ethereum Wiki $method_name](https://github.com/ethereum/wiki/wiki/JSON-RPC#" . strtolower($method_name) .
")",
139 ->setParameters($methodParams)
140 ->setType(
'null|' . $returnType, $returnTypeDescription)
142 if ($cnf[
'class'] ===
'PhpTrait') {
143 $code->getMethod($method_name)->setBody(
'return $this->__call(__FUNCTION__, func_get_args());');
148 $code->setUseStatements($useStatements);
149 $generator =
new CodeGenerator([
150 'generateScalarTypeHints' => TRUE,
151 'generateReturnTypeHints' => TRUE,
152 'enableSorting' => FALSE,
154 $codeText = $generator->generate($code);
158 file_put_contents($cnf[
'destination'] ,
$file_header . $codeText);
161 echo
"File is not written to disc, because file generation is disabled by '--no-file-generation'\n";
163 echo
"#############################################\n";
addUseStatement($type, &$useStatements)