From c5d41015dae180d59b62ceccfa339a4cbae521ad Mon Sep 17 00:00:00 2001 From: zerox Date: Wed, 21 Oct 2020 14:22:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9E=D0=BF=D1=82=D0=B8=D0=BC=D0=B8=D0=B7?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Client.php | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/Client.php b/src/Client.php index 21b2247..737317f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -7,9 +7,11 @@ class Client { private $resource = null; private $error_func = null; + private $is_init = false; + private $id_resource = null; public function __construct($id_resource) { - $this->resource = msg_get_queue($id_resource); + $this->id_resource = $id_resource; } public static function create($id_resource) { @@ -20,15 +22,27 @@ public function errorHandler($func) { $this->error_func = $func; } + private function error($error) { + if (is_callable($this->error_func)) + call_user_func($this->error_func, $error); + else + throw new \Exception($error); + } + public function send($module, $msg) { + if (!$this->is_init) { + try { + $this->resource = msg_get_queue($this->id_resource); + } catch (\Throwable $e) { + $this->error($e); + return $this; + } + $this->is_init = true; + } if (!is_array($msg)) $msg = [$msg]; - if (!msg_send($this->resource, 1, [$module, $msg], true, true, $error)) { - if (is_callable($this->error_func)) - call_user_func($this->error_func, $error); - else - throw new \Exception($error); - } + if (!msg_send($this->resource, 1, [$module, $msg], true, true, $error)) + $this->error($error); return $this; }