From b56c938a91c411fb062045a7664100b03baa8c3c Mon Sep 17 00:00:00 2001 From: Pavle Lee <523260513@qq.com> Date: Tue, 8 Mar 2016 13:23:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E4=BA=8B=E4=BB=B6=E6=9D=A5?= =?UTF-8?q?=E5=A4=84=E7=90=86response=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Connection.php | 30 +++++++++++------------------- CurlEvent.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+), 19 deletions(-) create mode 100644 CurlEvent.php diff --git a/Connection.php b/Connection.php index 7fd4ed2..68749b8 100644 --- a/Connection.php +++ b/Connection.php @@ -10,7 +10,7 @@ use Curl\Curl; use yii\base\Component; -use yii\base\ErrorException; +use yii\base\Event; use yii\helpers\ArrayHelper; class Connection extends Component @@ -30,29 +30,21 @@ class Connection extends Component */ protected $curl; - /** - * @var callable - */ - public $success; - - /** - * @var callable - */ - public $error; + const EVENT_CURL_SUCCESS = 'curlSuccess'; + const EVENT_CURL_ERROR = 'curlError'; public function init() { parent::init(); $this->curl = new Curl(); - !$this->success && $this->success = function (Curl $curl) { - $curl->response = ArrayHelper::toArray($curl->response); - }; - !$this->error && $this->error = function (Curl $curl) { - \Yii::trace(serialize($curl->response), 'rest.connect'); - throw new ErrorException($curl->errorMessage); - }; - $this->curl->success($this->success); - $this->curl->error($this->error); + $this->curl->success(function(Curl $curl){ + $event = new CurlEvent(['curl' => $curl]); + Connection::trigger('curlSuccess', $event); + }); + $this->curl->error(function(Curl $curl){ + $event = new CurlEvent(['curl' => $curl]); + Connection::trigger('curlError', $event); + }); } diff --git a/CurlEvent.php b/CurlEvent.php new file mode 100644 index 0000000..74b3953 --- /dev/null +++ b/CurlEvent.php @@ -0,0 +1,16 @@ + + * Date: 2016/3/8 + * Time: 13:13 + */ + +namespace pavle\yii2\rest; + + +use yii\base\Event; + +class CurlEvent extends Event +{ + public $curl; +} \ No newline at end of file