Ethereum PHP
PHP interface to Ethereum JSON-RPC API.
generate-methods.php
Go to the documentation of this file.
1 <?php
2 
12 require_once(__DIR__ . "/generator-commons.php");
13 
20 
21 
22 // For Tests we can disable the file generation.
23 $shouldWriteToDisc = (isset($GLOBALS['argv'][1]) && $GLOBALS['argv'][1] === '--no-file-generation') ? false : true;
24 
25 
29 $conf = [
30  'interface' => [
31  'destination' => './src/Web3Interface.php',
32  'class' => 'PhpInterface',
33  'name' => 'Web3Interface',
34  'group' => "@ingroup generated\n * @ingroup interfaces"
35 
36  ],
37  'trait' => [
38  'destination' => './src/Web3Methods.php',
39  'class' => 'PhpTrait',
40  'name' => 'Web3Methods',
41  'group' => '@ingroup generated'
42  ]
43 ];
44 
45 foreach ($conf as $cnf) {
46  echo "### GENERATING ETHEREUM METHODS INTERFACE ###\n";
47  echo "# File generated " . $cnf['destination'] . "\n";
48 
49  $group = $cnf["group"];
50  $file_header = <<<EOF
51 <?php
62 EOF;
63 
65  $useStatements = [];
66 
67  if ($cnf['class'] === 'PhpTrait') {
68  $code = new PhpTrait();
69  }
70  if ($cnf['class'] === 'PhpInterface') {
71  $code = new PhpInterface();
72  }
73 
74 
75  $code->setQualifiedName('\\Ethereum\\' . $cnf['name'])
76  ->setDescription(array(
77  'Ethereum JsonRPC Methods.',
78  '',
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().',
81  ))
82  ;
83 
84  $schema = getSchema();
85 
86  foreach ($schema['methods'] as $method_name => $params) {
87 
88  # printMe("\n# Generate $method_name");
89 
90  // Params for this method.
91  $valid_params = $params[0];
92  # printMe('Valid params', $valid_params);
93 
94 
95  // Generate parameters.
96  $methodParams = [];
97  if (count($valid_params)) {
98 
99  // Get argument definition Classes.
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);
105  // Add a use statement.
106  addUseStatement("Ethereum\\DataType\\$paramType", $useStatements);
107  }
108  }
109 
110  // Generate Return value.
111  $returnType = $params[1];
112  $returnTypeDescription = '';
113  if (is_array($returnType)) {
114  if (EthD::typeMap($returnType[0])) {
115  $arrayOfType = EthD::typeMap($returnType[0]);
116  } else {
117  $arrayOfType = $returnType[0];
118  }
119  $returnType = "array";
120  $returnTypeDescription = " Array of " . $arrayOfType;
121  } else if (EthD::typeMap($returnType)) {
122  $returnType = EthD::typeMap($returnType);
123  addUseStatement("Ethereum\\DataType\\$returnType", $useStatements);
124  }
125  else {
126  addUseStatement("Ethereum\\DataType\\$returnType", $useStatements);
127  }
128 
129  # printMe('Return type', $returnTypeDescription ? $returnTypeDescription : $returnType);
130 
131  // Generate method.
132  $code->setMethod(PhpMethod::create($method_name)
133  ->setDescription(array(
134  "Generated method $method_name().",
135  "",
136  "See [Ethereum Wiki $method_name](https://github.com/ethereum/wiki/wiki/JSON-RPC#" . strtolower($method_name) . ")",
137  "",
138  ))
139  ->setParameters($methodParams)
140  ->setType('null|' . $returnType, $returnTypeDescription)
141  );
142  if ($cnf['class'] === 'PhpTrait') {
143  $code->getMethod($method_name)->setBody('return $this->__call(__FUNCTION__, func_get_args());');
144  }
145 
146  }
147 
148  $code->setUseStatements($useStatements);
149  $generator = new CodeGenerator([
150  'generateScalarTypeHints' => TRUE,
151  'generateReturnTypeHints' => TRUE,
152  'enableSorting' => FALSE,
153  ]);
154  $codeText = $generator->generate($code);
155 
156  # print $codeText;
157  if ($shouldWriteToDisc) {
158  file_put_contents($cnf['destination'] , $file_header . $codeText);
159  }
160  else {
161  echo "File is not written to disc, because file generation is disabled by '--no-file-generation'\n";
162  }
163  echo "#############################################\n";
164 
165 
166 }
167 
168 
$shouldWriteToDisc
addUseStatement($type, &$useStatements)
getSchema()