-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathDebug.php
More file actions
180 lines (159 loc) · 4.21 KB
/
Debug.php
File metadata and controls
180 lines (159 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
/**
* This file is part of web3.php package.
*
* (c) Kuan-Cheng,Lai <alk03073135@gmail.com>
*
* @author Peter Lai <alk03073135@gmail.com>
* @license MIT
*/
namespace Web3;
use Web3\Providers\Provider;
use Web3\Providers\HttpProvider;
use Web3\RequestManagers\RequestManager;
use Web3\RequestManagers\HttpRequestManager;
class Debug
{
/**
* provider
*
* @var \Web3\Providers\Provider
*/
protected $provider;
/**
* methods
*
* @var array
*/
private $methods = [];
/**
* allowedMethods
*
* @var array
*/
private $allowedMethods = [
'debug_traceTransaction'
];
/**
* construct
*
* @param string|\Web3\Providers\Provider $provider
* @return void
*/
public function __construct($provider)
{
if (is_string($provider) && (filter_var($provider, FILTER_VALIDATE_URL) !== false)) {
// check the uri schema
if (preg_match('/^https?:\/\//', $provider) === 1) {
$requestManager = new HttpRequestManager($provider, 5);
$this->provider = new HttpProvider($requestManager);
}
} else if ($provider instanceof Provider) {
$this->provider = $provider;
}
}
/**
* call
*
* @param string $name
* @param array $arguments
* @return void
*/
public function __call($name, $arguments)
{
if (empty($this->provider)) {
throw new \RuntimeException('Please set provider first.');
}
$class = explode('\\', get_class());
if (preg_match('/^[a-zA-Z0-9]+$/', $name) === 1) {
$method = strtolower($class[1]) . '_' . $name;
if (!in_array($method, $this->allowedMethods)) {
throw new \RuntimeException('Unallowed rpc method: ' . $method);
}
if ($this->provider->isBatch) {
$callback = null;
} else {
$callback = array_pop($arguments);
if (is_callable($callback) !== true) {
throw new \InvalidArgumentException('The last param must be callback function.');
}
}
if (!array_key_exists($method, $this->methods)) {
// new the method
$methodClass = sprintf("\Web3\Methods\%s\%s", ucfirst($class[1]), ucfirst($name));
$methodObject = new $methodClass($method, $arguments);
$this->methods[$method] = $methodObject;
} else {
$methodObject = $this->methods[$method];
}
if ($methodObject->validate($arguments)) {
$inputs = $methodObject->transform($arguments, $methodObject->inputFormatters);
$methodObject->arguments = $inputs;
$this->provider->send($methodObject, $callback);
}
}
}
/**
* get
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
$method = 'get' . ucfirst($name);
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], []);
}
return false;
}
/**
* set
*
* @param string $name
* @param mixed $value
* @return mixed
*/
public function __set($name, $value)
{
$method = 'set' . ucfirst($name);
if (method_exists($this, $method)) {
return call_user_func_array([$this, $method], [$value]);
}
return false;
}
/**
* getProvider
*
* @return \Web3\Providers\Provider
*/
public function getProvider()
{
return $this->provider;
}
/**
* setProvider
*
* @param \Web3\Providers\Provider $provider
* @return bool
*/
public function setProvider($provider)
{
if ($provider instanceof Provider) {
$this->provider = $provider;
return true;
}
return false;
}
/**
* batch
*
* @param bool $status
* @return void
*/
public function batch($status)
{
$status = is_bool($status);
$this->provider->batch($status);
}
}