diff --git a/src/Contract.php b/src/Contract.php index ebebb427..b75e2402 100644 --- a/src/Contract.php +++ b/src/Contract.php @@ -14,10 +14,7 @@ use InvalidArgumentException; use Web3\Providers\Provider; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Providers\WsProvider; -use Web3\RequestManagers\WsRequestManager; use Web3\Utils; use Web3\Eth; use Web3\Contracts\Ethabi; @@ -120,13 +117,9 @@ public function __construct($provider, $abi, $defaultBlock = 'latest') 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); - - $this->provider = new HttpProvider($requestManager); + $this->provider = new HttpProvider($provider); } else if (preg_match('/^wss?:\/\//', $provider) === 1) { - $requestManager = new WsRequestManager($provider); - - $this->provider = new WsProvider($requestManager); + $this->provider = new WsProvider($provider); } } else if ($provider instanceof Provider) { $this->provider = $provider; diff --git a/src/Eth.php b/src/Eth.php index 38b2cbd5..dc6f99a6 100644 --- a/src/Eth.php +++ b/src/Eth.php @@ -13,10 +13,7 @@ use Web3\Providers\Provider; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Providers\WsProvider; -use Web3\RequestManagers\WsRequestManager; class Eth { @@ -54,13 +51,9 @@ 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); - - $this->provider = new HttpProvider($requestManager); + $this->provider = new HttpProvider($provider); } else if (preg_match('/^wss?:\/\//', $provider) === 1) { - $requestManager = new WsRequestManager($provider); - - $this->provider = new WsProvider($requestManager); + $this->provider = new WsProvider($provider); } } else if ($provider instanceof Provider) { $this->provider = $provider; diff --git a/src/Net.php b/src/Net.php index 5a43ad22..eb2692c2 100644 --- a/src/Net.php +++ b/src/Net.php @@ -13,10 +13,7 @@ use Web3\Providers\Provider; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Providers\WsProvider; -use Web3\RequestManagers\WsRequestManager; class Net { @@ -54,13 +51,9 @@ 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); - - $this->provider = new HttpProvider($requestManager); + $this->provider = new HttpProvider($provider); } else if (preg_match('/^wss?:\/\//', $provider) === 1) { - $requestManager = new WsRequestManager($provider); - - $this->provider = new WsProvider($requestManager); + $this->provider = new WsProvider($provider); } } else if ($provider instanceof Provider) { $this->provider = $provider; diff --git a/src/Personal.php b/src/Personal.php index 7d53a15d..8ecaea78 100644 --- a/src/Personal.php +++ b/src/Personal.php @@ -13,10 +13,7 @@ use Web3\Providers\Provider; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Providers\WsProvider; -use Web3\RequestManagers\WsRequestManager; class Personal { @@ -54,13 +51,9 @@ 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); - - $this->provider = new HttpProvider($requestManager); + $this->provider = new HttpProvider($provider); } else if (preg_match('/^wss?:\/\//', $provider) === 1) { - $requestManager = new WsRequestManager($provider); - - $this->provider = new WsProvider($requestManager); + $this->provider = new WsProvider($provider); } } else if ($provider instanceof Provider) { $this->provider = $provider; diff --git a/src/RequestManagers/HttpAsyncRequestManager.php b/src/Providers/HttpAsyncProvider.php similarity index 57% rename from src/RequestManagers/HttpAsyncRequestManager.php rename to src/Providers/HttpAsyncProvider.php index 0c655999..580c7e39 100644 --- a/src/RequestManagers/HttpAsyncRequestManager.php +++ b/src/Providers/HttpAsyncProvider.php @@ -2,29 +2,37 @@ /** * This file is part of web3.php package. - * + * * (c) Kuan-Cheng,Lai - * + * * @author Peter Lai * @license MIT */ -namespace Web3\RequestManagers; +namespace Web3\Providers; use InvalidArgumentException; use Psr\Http\Message\StreamInterface; use RuntimeException as RPCException; use Psr\Http\Message\ResponseInterface; +use GuzzleHttp\Exception\RequestException; use React; use React\Async; use React\EventLoop\Loop; use React\Http\Browser; use React\Socket\Connector; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\IRequestManager; +use Web3\Providers\Provider; +use Web3\Providers\IProvider; -class HttpAsyncRequestManager extends RequestManager implements IRequestManager +class HttpAsyncProvider extends Provider implements IProvider { + /** + * methods + * + * @var array + */ + protected $methods = []; + /** * client * @@ -48,6 +56,92 @@ public function __construct($host, $timeout = 1) $this->client = (new Browser($connector, Loop::get()))->withRejectErrorResponse(false); } + /** + * close + * + * @return void + */ + public function close() {} + + /** + * send + * + * @param \Web3\Methods\Method $method + * @param callable $callback + * @return void + */ + public function send($method, $callback) + { + $payload = $method->toPayloadString(); + + if (!$this->isBatch) { + $proxy = function ($err, $res) use ($method, $callback) { + if ($err !== null) { + return call_user_func($callback, $err, null); + } + if (!is_array($res)) { + $res = $method->transform([$res], $method->outputFormatters); + return call_user_func($callback, null, $res[0]); + } + $res = $method->transform($res, $method->outputFormatters); + + return call_user_func($callback, null, $res); + }; + return $this->sendPayload($payload, $proxy); + } else { + $this->methods[] = $method; + $this->batch[] = $payload; + } + } + + /** + * batch + * + * @param bool $status + * @return void + */ + public function batch($status) + { + $status = is_bool($status); + + $this->isBatch = $status; + } + + /** + * execute + * + * @param callable $callback + * @return void + */ + public function execute($callback) + { + if (!$this->isBatch) { + throw new \RuntimeException('Please batch json rpc first.'); + } + $methods = $this->methods; + $proxy = function ($err, $res) use ($methods, $callback) { + if ($err !== null) { + return call_user_func($callback, $err, null); + } + foreach ($methods as $key => $method) { + if (isset($res[$key])) { + if (!is_array($res[$key])) { + $transformed = $method->transform([$res[$key]], $method->outputFormatters); + $res[$key] = $transformed[0]; + } else { + $transformed = $method->transform($res[$key], $method->outputFormatters); + $res[$key] = $transformed; + } + } + } + return call_user_func($callback, null, $res); + }; + $r = $this->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); + $this->methods = []; + $this->batch = []; + return $r; + } + /** * sendPayload * @@ -122,4 +216,4 @@ public function sendPayload($payload, $callback) return Async\coroutine($request); } -} +} \ No newline at end of file diff --git a/src/Providers/HttpProvider.php b/src/Providers/HttpProvider.php index 8c4fea66..cb24b84f 100644 --- a/src/Providers/HttpProvider.php +++ b/src/Providers/HttpProvider.php @@ -11,9 +11,14 @@ namespace Web3\Providers; +use InvalidArgumentException; +use Psr\Http\Message\StreamInterface; +use RuntimeException as RPCException; +use Psr\Http\Message\ResponseInterface; +use GuzzleHttp\Exception\RequestException; +use GuzzleHttp\Client; use Web3\Providers\Provider; use Web3\Providers\IProvider; -use Web3\RequestManagers\RequestManager; class HttpProvider extends Provider implements IProvider { @@ -24,15 +29,24 @@ class HttpProvider extends Provider implements IProvider */ protected $methods = []; + /** + * client + * + * @var \GuzzleHttp + */ + protected $client; + /** * construct - * - * @param \Web3\RequestManagers\RequestManager $requestManager + * + * @param string $host + * @param int $timeout * @return void */ - public function __construct(RequestManager $requestManager) + public function __construct($host, $timeout = 1) { - parent::__construct($requestManager); + parent::__construct($host, $timeout); + $this->client = new Client; } /** @@ -66,7 +80,7 @@ public function send($method, $callback) return call_user_func($callback, null, $res); }; - return $this->requestManager->sendPayload($payload, $proxy); + return $this->sendPayload($payload, $proxy); } else { $this->methods[] = $method; $this->batch[] = $payload; @@ -115,9 +129,79 @@ public function execute($callback) } return call_user_func($callback, null, $res); }; - $r = $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); + $r = $this->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); $this->methods = []; $this->batch = []; return $r; } + + /** + * sendPayload + * + * @param string $payload + * @param callable $callback + * @return void + */ + public function sendPayload($payload, $callback) + { + if (!is_string($payload)) { + throw new InvalidArgumentException('Payload must be string.'); + } + + try { + $res = $this->client->post($this->host, [ + 'headers' => [ + 'content-type' => 'application/json' + ], + 'body' => $payload, + 'timeout' => $this->timeout, + 'connect_timeout' => $this->timeout + ]); + /** + * @var StreamInterface $stream ; + */ + $stream = $res->getBody(); + $json = json_decode($stream); + $stream->close(); + + if (JSON_ERROR_NONE !== json_last_error()) { + call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null); + } + if (is_array($json)) { + // batch results + $results = []; + $errors = []; + + foreach ($json as $result) { + if (property_exists($result,'result')) { + $results[] = $result->result; + } else { + if (isset($json->error)) { + $error = $json->error; + $errors[] = new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code); + } else { + $results[] = null; + } + } + } + if (count($errors) > 0) { + call_user_func($callback, $errors, $results); + } else { + call_user_func($callback, null, $results); + } + } elseif (property_exists($json,'result')) { + call_user_func($callback, null, $json->result); + } else { + if (isset($json->error)) { + $error = $json->error; + + call_user_func($callback, new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code), null); + } else { + call_user_func($callback, new RPCException('Something wrong happened.'), null); + } + } + } catch (RequestException $err) { + call_user_func($callback, $err, null); + } + } } \ No newline at end of file diff --git a/src/Providers/IProvider.php b/src/Providers/IProvider.php index 575205c7..d0f15419 100644 --- a/src/Providers/IProvider.php +++ b/src/Providers/IProvider.php @@ -44,4 +44,13 @@ public function batch($status); * @return void */ public function execute($callback); + + /** + * sendPayload + * + * @param string $payload + * @param callable $callback + * @return void + */ + public function sendPayload($payload, $callback); } \ No newline at end of file diff --git a/src/Providers/Provider.php b/src/Providers/Provider.php index 00ee9152..b4ad5157 100644 --- a/src/Providers/Provider.php +++ b/src/Providers/Provider.php @@ -11,16 +11,21 @@ namespace Web3\Providers; -use Web3\RequestManagers\RequestManager; - class Provider { /** - * requestManager + * host + * + * @var string + */ + protected $host; + + /** + * timeout * - * @var \Web3\RequestManagers\RequestManager + * @var float */ - protected $requestManager; + protected $timeout; /** * isBatch @@ -53,12 +58,14 @@ class Provider /** * construct * - * @param \Web3\RequestManagers\RequestManager $requestManager + * @param string $host + * @param float $timeout * @return void */ - public function __construct(RequestManager $requestManager) + public function __construct($host, $timeout=1) { - $this->requestManager = $requestManager; + $this->host = $host; + $this->timeout = (float) $timeout; } /** @@ -95,13 +102,23 @@ public function __set($name, $value) } /** - * getRequestManager + * getHost + * + * @return string + */ + public function getHost() + { + return $this->host; + } + + /** + * getTimeout * - * @return \Web3\RequestManagers\RequestManager + * @return float */ - public function getRequestManager() + public function getTimeout() { - return $this->requestManager; + return $this->timeout; } /** diff --git a/src/RequestManagers/WsClient.php b/src/Providers/WsClient.php similarity index 99% rename from src/RequestManagers/WsClient.php rename to src/Providers/WsClient.php index 8816a6bc..25354d62 100644 --- a/src/RequestManagers/WsClient.php +++ b/src/Providers/WsClient.php @@ -10,7 +10,7 @@ * @license MIT */ - namespace Web3\RequestManagers; + namespace Web3\Providers; use Ratchet\Client\Connector; use React; diff --git a/src/Providers/WsProvider.php b/src/Providers/WsProvider.php index 4934f1ab..687678c6 100644 --- a/src/Providers/WsProvider.php +++ b/src/Providers/WsProvider.php @@ -11,9 +11,9 @@ namespace Web3\Providers; +use React\Async; use Web3\Providers\Provider; use Web3\Providers\IProvider; -use Web3\RequestManagers\RequestManager; class WsProvider extends Provider implements IProvider { @@ -24,15 +24,36 @@ class WsProvider extends Provider implements IProvider */ protected $methods = []; + /** + * client + * + * @var \Web3\Providers\WsClient + */ + protected $client; + /** * construct - * - * @param \Web3\RequestManagers\RequestManager $requestManager + * + * @param string $host + * @param int $timeout * @return void */ - public function __construct(RequestManager $requestManager) + public function __construct($host, $timeout = 1) { - parent::__construct($requestManager); + parent::__construct($host, $timeout); + $this->client = new WsClient( + $host, + function ($obj, $message) { + }, + function ($obj, $error) { + }, + function ($obj, $close) { + }, + function ($obj, $connected) { + }, + $timeout + ); + $this->client->set_ws_connector(); } /** @@ -42,7 +63,7 @@ public function __construct(RequestManager $requestManager) */ public function close() { - $this->requestManager->close(); + $this->client->close(); } /** @@ -69,7 +90,7 @@ public function send($method, $callback) return call_user_func($callback, null, $res); }; - return $this->requestManager->sendPayload($payload, $proxy); + return $this->sendPayload($payload, $proxy); } else { $this->methods[] = $method; $this->batch[] = $payload; @@ -118,9 +139,80 @@ public function execute($callback) } return call_user_func($callback, null, $res); }; - $r = $this->requestManager->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); + $r = $this->sendPayload('[' . implode(',', $this->batch) . ']', $proxy); $this->methods = []; $this->batch = []; return $r; } + + /** + * sendPayload + * + * @param string $payload + * @param callable $callback + * @return void + */ + public function sendPayload($payload, $callback) + { + if (!is_string($payload)) { + throw new \InvalidArgumentException('Payload must be string.'); + } + + if (!$this->client->isConnected) { + Async\await($this->client->connect(0)); + } + + $host = $this->host; + $request = function () use ($host, $payload, $callback) { + try { + $res = Async\await($this->client->send($payload)); + $json = json_decode($res); + + if (JSON_ERROR_NONE !== json_last_error()) { + call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null); + } + if (is_array($json)) { + // batch results + $results = []; + $errors = []; + + foreach ($json as $result) { + if (property_exists($result,'result')) { + $results[] = $result->result; + } else { + if (isset($json->error)) { + $error = $json->error; + $errors[] = new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code); + } else { + $results[] = null; + } + } + } + if (count($errors) > 0) { + call_user_func($callback, $errors, $results); + } else { + call_user_func($callback, null, $results); + } + } elseif (property_exists($json,'result')) { + call_user_func($callback, null, $json->result); + } else { + if (isset($json->error)) { + $error = $json->error; + + call_user_func($callback, new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code), null); + } else { + call_user_func($callback, new RPCException('Something wrong happened.'), null); + } + } + } catch (Exception $err) { + call_user_func($callback, $err, null); + } + }; + + if (function_exists('React\\Async\\async')) { + $request = Async\async($request); + } + + return Async\coroutine($request); + } } \ No newline at end of file diff --git a/src/RequestManagers/HttpRequestManager.php b/src/RequestManagers/HttpRequestManager.php deleted file mode 100644 index b7e4e75e..00000000 --- a/src/RequestManagers/HttpRequestManager.php +++ /dev/null @@ -1,114 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\RequestManagers; - -use InvalidArgumentException; -use Psr\Http\Message\StreamInterface; -use RuntimeException as RPCException; -use Psr\Http\Message\ResponseInterface; -use GuzzleHttp\Exception\RequestException; -use GuzzleHttp\Client; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\IRequestManager; - -class HttpRequestManager extends RequestManager implements IRequestManager -{ - /** - * client - * - * @var \GuzzleHttp - */ - protected $client; - - /** - * construct - * - * @param string $host - * @param int $timeout - * @return void - */ - public function __construct($host, $timeout = 1) - { - parent::__construct($host, $timeout); - $this->client = new Client; - } - - /** - * sendPayload - * - * @param string $payload - * @param callable $callback - * @return void - */ - public function sendPayload($payload, $callback) - { - if (!is_string($payload)) { - throw new \InvalidArgumentException('Payload must be string.'); - } - - try { - $res = $this->client->post($this->host, [ - 'headers' => [ - 'content-type' => 'application/json' - ], - 'body' => $payload, - 'timeout' => $this->timeout, - 'connect_timeout' => $this->timeout - ]); - /** - * @var StreamInterface $stream ; - */ - $stream = $res->getBody(); - $json = json_decode($stream); - $stream->close(); - - if (JSON_ERROR_NONE !== json_last_error()) { - call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null); - } - if (is_array($json)) { - // batch results - $results = []; - $errors = []; - - foreach ($json as $result) { - if (property_exists($result,'result')) { - $results[] = $result->result; - } else { - if (isset($json->error)) { - $error = $json->error; - $errors[] = new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code); - } else { - $results[] = null; - } - } - } - if (count($errors) > 0) { - call_user_func($callback, $errors, $results); - } else { - call_user_func($callback, null, $results); - } - } elseif (property_exists($json,'result')) { - call_user_func($callback, null, $json->result); - } else { - if (isset($json->error)) { - $error = $json->error; - - call_user_func($callback, new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code), null); - } else { - call_user_func($callback, new RPCException('Something wrong happened.'), null); - } - } - } catch (RequestException $err) { - call_user_func($callback, $err, null); - } - } -} diff --git a/src/RequestManagers/IRequestManager.php b/src/RequestManagers/IRequestManager.php deleted file mode 100644 index 8056051d..00000000 --- a/src/RequestManagers/IRequestManager.php +++ /dev/null @@ -1,24 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\RequestManagers; - -interface IRequestManager -{ - /** - * sendPayload - * - * @param string $payload - * @param callable $callback - * @return void - */ - public function sendPayload($payload, $callback); -} \ No newline at end of file diff --git a/src/RequestManagers/RequestManager.php b/src/RequestManagers/RequestManager.php deleted file mode 100644 index 585dae03..00000000 --- a/src/RequestManagers/RequestManager.php +++ /dev/null @@ -1,95 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\RequestManagers; - -class RequestManager -{ - /** - * host - * - * @var string - */ - protected $host; - - /** - * timeout - * - * @var float - */ - protected $timeout; - - /** - * construct - * - * @param string $host - * @param float $timeout - * @return void - */ - public function __construct($host, $timeout=1) - { - $this->host = $host; - $this->timeout = (float) $timeout; - } - - /** - * 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 bool - */ - public function __set($name, $value) - { - $method = 'set' . ucfirst($name); - - if (method_exists($this, $method)) { - return call_user_func_array([$this, $method], [$value]); - } - return false; - } - - /** - * getHost - * - * @return string - */ - public function getHost() - { - return $this->host; - } - - /** - * getTimeout - * - * @return float - */ - public function getTimeout() - { - return $this->timeout; - } -} \ No newline at end of file diff --git a/src/RequestManagers/WsRequestManager.php b/src/RequestManagers/WsRequestManager.php deleted file mode 100644 index 0c2b6a83..00000000 --- a/src/RequestManagers/WsRequestManager.php +++ /dev/null @@ -1,140 +0,0 @@ - - * - * @author Peter Lai - * @license MIT - */ - -namespace Web3\RequestManagers; - -use InvalidArgumentException; -use Psr\Http\Message\StreamInterface; -use RuntimeException as RPCException; -use Psr\Http\Message\ResponseInterface; -use React; -use React\Async; -use React\EventLoop\Loop; -use React\Http\Browser; -use React\Socket\Connector; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\IRequestManager; - -class WsRequestManager extends RequestManager implements IRequestManager -{ - /** - * client - * - * @var \Web3\RequestManagers\WsClient - */ - protected $client; - - /** - * construct - * - * @param string $host - * @param int $timeout - * @return void - */ - public function __construct($host, $timeout = 1) - { - parent::__construct($host, $timeout); - $this->client = new WsClient( - $host, - function ($obj, $message) { - }, - function ($obj, $error) { - }, - function ($obj, $close) { - }, - function ($obj, $connected) { - }, - $timeout - ); - $this->client->set_ws_connector(); - } - - /** - * close - * - * @return void - */ - public function close() - { - $this->client->close(); - } - - /** - * sendPayload - * - * @param string $payload - * @param callable $callback - * @return void - */ - public function sendPayload($payload, $callback) - { - if (!is_string($payload)) { - throw new \InvalidArgumentException('Payload must be string.'); - } - - if (!$this->client->isConnected) { - Async\await($this->client->connect(0)); - } - - $host = $this->host; - $request = function () use ($host, $payload, $callback) { - try { - $res = Async\await($this->client->send($payload)); - $json = json_decode($res); - - if (JSON_ERROR_NONE !== json_last_error()) { - call_user_func($callback, new InvalidArgumentException('json_decode error: ' . json_last_error_msg()), null); - } - if (is_array($json)) { - // batch results - $results = []; - $errors = []; - - foreach ($json as $result) { - if (property_exists($result,'result')) { - $results[] = $result->result; - } else { - if (isset($json->error)) { - $error = $json->error; - $errors[] = new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code); - } else { - $results[] = null; - } - } - } - if (count($errors) > 0) { - call_user_func($callback, $errors, $results); - } else { - call_user_func($callback, null, $results); - } - } elseif (property_exists($json,'result')) { - call_user_func($callback, null, $json->result); - } else { - if (isset($json->error)) { - $error = $json->error; - - call_user_func($callback, new RPCException(mb_ereg_replace('Error: ', '', $error->message), $error->code), null); - } else { - call_user_func($callback, new RPCException('Something wrong happened.'), null); - } - } - } catch (Exception $err) { - call_user_func($callback, $err, null); - } - }; - - if (function_exists('React\\Async\\async')) { - $request = Async\async($request); - } - - return Async\coroutine($request); - } -} diff --git a/src/Shh.php b/src/Shh.php index 0de3eedf..185de647 100644 --- a/src/Shh.php +++ b/src/Shh.php @@ -13,10 +13,7 @@ use Web3\Providers\Provider; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Providers\WsProvider; -use Web3\RequestManagers\WsRequestManager; class Shh { @@ -55,13 +52,9 @@ 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); - - $this->provider = new HttpProvider($requestManager); + $this->provider = new HttpProvider($provider); } else if (preg_match('/^wss?:\/\//', $provider) === 1) { - $requestManager = new WsRequestManager($provider); - - $this->provider = new WsProvider($requestManager); + $this->provider = new WsProvider($provider); } } else if ($provider instanceof Provider) { $this->provider = $provider; diff --git a/src/Web3.php b/src/Web3.php index f7c4c1c2..ba45fe73 100644 --- a/src/Web3.php +++ b/src/Web3.php @@ -18,10 +18,7 @@ use Web3\Utils; use Web3\Providers\Provider; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Providers\WsProvider; -use Web3\RequestManagers\WsRequestManager; class Web3 { @@ -94,13 +91,9 @@ 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); - - $this->provider = new HttpProvider($requestManager); + $this->provider = new HttpProvider($provider); } else if (preg_match('/^wss?:\/\//', $provider) === 1) { - $requestManager = new WsRequestManager($provider); - - $this->provider = new WsProvider($requestManager); + $this->provider = new WsProvider($provider); } } else if ($provider instanceof Provider) { $this->provider = $provider; diff --git a/test/TestCase.php b/test/TestCase.php index 26e18be3..6a7c3af5 100644 --- a/test/TestCase.php +++ b/test/TestCase.php @@ -4,7 +4,7 @@ use \PHPUnit\Framework\TestCase as BaseTestCase; use Web3\Web3; -use Web3\RequestManagers\HttpAsyncRequestManager; +use Web3\Providers\HttpAsyncProvider; use Web3\Providers\HttpProvider; class TestCase extends BaseTestCase @@ -59,8 +59,7 @@ public function setUp(): void $web3 = new Web3($this->testHost); $this->web3 = $web3; - $asyncRequestManager = new HttpAsyncRequestManager($this->testHost); - $asyncHttpProvider = new HttpProvider($asyncRequestManager); + $asyncHttpProvider = new HttpAsyncProvider($this->testHost); $this->asyncHttpProvider = $asyncHttpProvider; $web3->eth->coinbase(function ($err, $coinbase) { diff --git a/test/unit/ContractTest.php b/test/unit/ContractTest.php index 3d72bde4..d0234169 100644 --- a/test/unit/ContractTest.php +++ b/test/unit/ContractTest.php @@ -4,8 +4,6 @@ use Test\TestCase; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Contract; use Web3\Utils; use Web3\Contracts\Ethabi; @@ -445,7 +443,6 @@ public function testInstance() $contract = new Contract($this->testHost, $this->testAbi); $this->assertTrue($contract->provider instanceof HttpProvider); - $this->assertTrue($contract->provider->requestManager instanceof RequestManager); } /** @@ -456,14 +453,13 @@ public function testInstance() public function testSetProvider() { $contract = $this->contract; - $requestManager = new HttpRequestManager('http://localhost:8545'); - $contract->provider = new HttpProvider($requestManager); + $contract->provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($contract->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($contract->provider->host, 'http://localhost:8545'); $contract->provider = null; - $this->assertEquals($contract->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($contract->provider->host, 'http://localhost:8545'); } /** diff --git a/test/unit/EthTest.php b/test/unit/EthTest.php index 702bd6b8..c8ed449e 100644 --- a/test/unit/EthTest.php +++ b/test/unit/EthTest.php @@ -5,8 +5,6 @@ use RuntimeException; use Test\TestCase; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Eth; class EthTest extends TestCase @@ -40,7 +38,6 @@ public function testInstance() $eth = new Eth($this->testHost); $this->assertTrue($eth->provider instanceof HttpProvider); - $this->assertTrue($eth->provider->requestManager instanceof RequestManager); } /** @@ -51,14 +48,13 @@ public function testInstance() public function testSetProvider() { $eth = $this->eth; - $requestManager = new HttpRequestManager('http://localhost:8545'); - $eth->provider = new HttpProvider($requestManager); + $eth->provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($eth->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($eth->provider->host, 'http://localhost:8545'); $eth->provider = null; - $this->assertEquals($eth->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($eth->provider->host, 'http://localhost:8545'); } /** diff --git a/test/unit/HttpProviderTest.php b/test/unit/HttpProviderTest.php index 57bf0605..03b6d775 100644 --- a/test/unit/HttpProviderTest.php +++ b/test/unit/HttpProviderTest.php @@ -4,8 +4,7 @@ use RuntimeException; use Test\TestCase; -use Web3\RequestManagers\HttpRequestManager; -use Web3\RequestManagers\HttpAsyncRequestManager; +use Web3\Providers\HttpAsyncProvider; use Web3\Providers\HttpProvider; use Web3\Methods\Web3\ClientVersion; @@ -18,8 +17,7 @@ class HttpProviderTest extends TestCase */ public function testSend() { - $requestManager = new HttpRequestManager($this->testHost); - $provider = new HttpProvider($requestManager); + $provider = new HttpProvider($this->testHost); $method = new ClientVersion('web3_clientVersion', []); $provider->send($method, function ($err, $version) { @@ -37,8 +35,7 @@ public function testSend() */ public function testBatch() { - $requestManager = new HttpRequestManager($this->testHost); - $provider = new HttpProvider($requestManager); + $provider = new HttpProvider($this->testHost); $method = new ClientVersion('web3_clientVersion', []); $callback = function ($err, $data) { if ($err !== null) { @@ -66,8 +63,7 @@ public function testBatch() */ public function testSendAsync() { - $requestManager = new HttpAsyncRequestManager($this->testHost); - $provider = new HttpProvider($requestManager); + $provider = new HttpAsyncProvider($this->testHost); $method = new ClientVersion('web3_clientVersion', []); // \React\Async\await($provider->send($method, function ($err, $version) { @@ -108,8 +104,7 @@ function () use ($c) { return $c; } */ public function testBatchAsync() { - $requestManager = new HttpAsyncRequestManager($this->testHost); - $provider = new HttpProvider($requestManager); + $provider = new HttpAsyncProvider($this->testHost); $method = new ClientVersion('web3_clientVersion', []); $callback = function ($err, $data) { if ($err !== null) { diff --git a/test/unit/NetTest.php b/test/unit/NetTest.php index ef5ef33e..24d5a14f 100644 --- a/test/unit/NetTest.php +++ b/test/unit/NetTest.php @@ -5,8 +5,6 @@ use RuntimeException; use Test\TestCase; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Net; class NetTest extends TestCase @@ -40,7 +38,6 @@ public function testInstance() $net = new Net($this->testHost); $this->assertTrue($net->provider instanceof HttpProvider); - $this->assertTrue($net->provider->requestManager instanceof RequestManager); } /** @@ -51,14 +48,13 @@ public function testInstance() public function testSetProvider() { $net = $this->net; - $requestManager = new HttpRequestManager('http://localhost:8545'); - $net->provider = new HttpProvider($requestManager); + $net->provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($net->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($net->provider->host, 'http://localhost:8545'); $net->provider = null; - $this->assertEquals($net->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($net->provider->host, 'http://localhost:8545'); } /** diff --git a/test/unit/PersonalTest.php b/test/unit/PersonalTest.php index b5bab279..4f48a0cc 100644 --- a/test/unit/PersonalTest.php +++ b/test/unit/PersonalTest.php @@ -5,8 +5,6 @@ use RuntimeException; use Test\TestCase; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Personal; class PersonalTest extends TestCase @@ -40,7 +38,6 @@ public function testInstance() $personal = new Personal($this->testHost); $this->assertTrue($personal->provider instanceof HttpProvider); - $this->assertTrue($personal->provider->requestManager instanceof RequestManager); } /** @@ -51,14 +48,13 @@ public function testInstance() public function testSetProvider() { $personal = $this->personal; - $requestManager = new HttpRequestManager('http://localhost:8545'); - $personal->provider = new HttpProvider($requestManager); + $personal->provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($personal->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($personal->provider->host, 'http://localhost:8545'); $personal->provider = null; - $this->assertEquals($personal->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($personal->provider->host, 'http://localhost:8545'); } /** diff --git a/test/unit/ProviderTest.php b/test/unit/ProviderTest.php index 32af5176..f9718e88 100644 --- a/test/unit/ProviderTest.php +++ b/test/unit/ProviderTest.php @@ -3,27 +3,20 @@ namespace Test\Unit; use Test\TestCase; -use Web3\RequestManagers\RequestManager; use Web3\Providers\Provider; +use Web3\Providers\HttpProvider; class ProviderTest extends TestCase { /** - * testSetRequestManager + * testNewProvider * * @return void */ - public function testSetRequestManager() + public function testNewProvider() { - $requestManager = new RequestManager('http://localhost:8545'); - $provider = new Provider($requestManager); + $provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($provider->requestManager->host, 'http://localhost:8545'); - - $requestManager = new RequestManager($this->testHost2); - $provider->requestManager = $requestManager; - - // there is no setter for request manager - $this->assertEquals($provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($provider->host, 'http://localhost:8545'); } } \ No newline at end of file diff --git a/test/unit/RequestManagerTest.php b/test/unit/RequestManagerTest.php deleted file mode 100644 index 61dabdb2..00000000 --- a/test/unit/RequestManagerTest.php +++ /dev/null @@ -1,27 +0,0 @@ -assertEquals($requestManager->host, 'http://localhost:8545'); - $this->assertEquals($requestManager->timeout, 0.1); - - // there is no setter for host and timeout - $requestManager->host = $this->testHost2; - $requestManager->timeout = 1; - $this->assertEquals($requestManager->host, 'http://localhost:8545'); - $this->assertEquals($requestManager->timeout, 0.1); - } -} \ No newline at end of file diff --git a/test/unit/ShhTest.php b/test/unit/ShhTest.php index bcaeb8a0..61c5e88f 100644 --- a/test/unit/ShhTest.php +++ b/test/unit/ShhTest.php @@ -5,8 +5,6 @@ use RuntimeException; use Test\TestCase; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; use Web3\Shh; class ShhTest extends TestCase @@ -40,7 +38,6 @@ public function testInstance() $shh = new Shh($this->testHost); $this->assertTrue($shh->provider instanceof HttpProvider); - $this->assertTrue($shh->provider->requestManager instanceof RequestManager); } /** @@ -51,14 +48,13 @@ public function testInstance() public function testSetProvider() { $shh = $this->shh; - $requestManager = new HttpRequestManager('http://localhost:8545'); - $shh->provider = new HttpProvider($requestManager); + $shh->provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($shh->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($shh->provider->host, 'http://localhost:8545'); $shh->provider = null; - $this->assertEquals($shh->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($shh->provider->host, 'http://localhost:8545'); } /** diff --git a/test/unit/Web3Test.php b/test/unit/Web3Test.php index db3b0b9a..7e26e6f4 100644 --- a/test/unit/Web3Test.php +++ b/test/unit/Web3Test.php @@ -11,8 +11,6 @@ use Web3\Shh; use Web3\Utils; use Web3\Providers\HttpProvider; -use Web3\RequestManagers\RequestManager; -use Web3\RequestManagers\HttpRequestManager; class Web3Test extends TestCase { @@ -49,11 +47,9 @@ public function setUp(): void */ public function testInstance() { - $requestManager = new HttpRequestManager('http://localhost:8545'); - $web3 = new Web3(new HttpProvider($requestManager)); + $web3 = new Web3(new HttpProvider('http://localhost:8545')); $this->assertTrue($web3->provider instanceof HttpProvider); - $this->assertTrue($web3->provider->requestManager instanceof RequestManager); $this->assertTrue($web3->eth instanceof Eth); $this->assertTrue($web3->net instanceof Net); $this->assertTrue($web3->personal instanceof Personal); @@ -69,13 +65,12 @@ public function testInstance() public function testSetProvider() { $web3 = $this->web3; - $requestManager = new HttpRequestManager('http://localhost:8545'); - $web3->provider = new HttpProvider($requestManager); + $web3->provider = new HttpProvider('http://localhost:8545'); - $this->assertEquals($web3->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($web3->provider->host, 'http://localhost:8545'); $web3->provider = null; - $this->assertEquals($web3->provider->requestManager->host, 'http://localhost:8545'); + $this->assertEquals($web3->provider->host, 'http://localhost:8545'); } /** diff --git a/test/unit/WsProviderTest.php b/test/unit/WsProviderTest.php index b985fab3..598e0746 100644 --- a/test/unit/WsProviderTest.php +++ b/test/unit/WsProviderTest.php @@ -4,7 +4,6 @@ use RuntimeException; use Test\TestCase; -use Web3\RequestManagers\WsRequestManager; use Web3\Providers\WsProvider; use Web3\Methods\Web3\ClientVersion; @@ -17,8 +16,7 @@ class WsProviderTest extends TestCase */ public function testSend() { - $requestManager = new WsRequestManager($this->testWsHost); - $provider = new WsProvider($requestManager); + $provider = new WsProvider($this->testWsHost); $method = new ClientVersion('web3_clientVersion', []); // \React\Async\await($provider->send($method, function ($err, $version) { @@ -61,8 +59,7 @@ function () use ($c) { return $c; } */ public function testBatchAsync() { - $requestManager = new WsRequestManager($this->testWsHost); - $provider = new WsProvider($requestManager); + $provider = new WsProvider($this->testWsHost); $method = new ClientVersion('web3_clientVersion', []); $callback = function ($err, $data) { if ($err !== null) {