Carsten Brandt
11 years ago
9 changed files with 167 additions and 80 deletions
@ -0,0 +1,43 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\elasticsearch; |
||||
|
||||
/** |
||||
* Exception represents an exception that is caused by elasticsearch-related operations. |
||||
* |
||||
* @author Carsten Brandt <mail@cebe.cc> |
||||
* @since 2.0 |
||||
*/ |
||||
class Exception extends \yii\db\Exception |
||||
{ |
||||
/** |
||||
* @var array additional information about the http request that caused the error. |
||||
*/ |
||||
public $errorInfo = []; |
||||
|
||||
/** |
||||
* Constructor. |
||||
* @param string $message error message |
||||
* @param array $errorInfo error info |
||||
* @param integer $code error code |
||||
* @param \Exception $previous The previous exception used for the exception chaining. |
||||
*/ |
||||
public function __construct($message, $errorInfo = [], $code = 0, \Exception $previous = null) |
||||
{ |
||||
$this->errorInfo = $errorInfo; |
||||
parent::__construct($message, $code, $previous); |
||||
} |
||||
|
||||
/** |
||||
* @return string the user-friendly name of this exception |
||||
*/ |
||||
public function getName() |
||||
{ |
||||
return \Yii::t('yii', 'Elasticsearch Database Exception'); |
||||
} |
||||
} |
@ -1,62 +0,0 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yii\elasticsearch; |
||||
|
||||
use Guzzle\Http\Exception\ClientErrorResponseException; |
||||
use yii\base\Exception; |
||||
use yii\helpers\Json; |
||||
|
||||
/** |
||||
* Class GuzzleConnection |
||||
* |
||||
* @author Carsten Brandt <mail@cebe.cc> |
||||
* @since 2.0 |
||||
*/ |
||||
class GuzzleConnection extends Connection |
||||
{ |
||||
/** |
||||
* @var \Guzzle\Http\Client |
||||
*/ |
||||
private $_http; |
||||
|
||||
protected function httpRequest($type, $url, $body = null) |
||||
{ |
||||
if ($this->_http === null) { |
||||
$this->_http = new \Guzzle\Http\Client('http://localhost:9200/');// TODO use active node |
||||
//$guzzle->setDefaultOption() |
||||
} |
||||
$requestOptions = []; |
||||
if ($type == 'head') { |
||||
$requestOptions['exceptions'] = false; |
||||
} |
||||
if ($type == 'get' && $body !== null) { |
||||
$type = 'post'; |
||||
} |
||||
try{ |
||||
$response = $this->_http->createRequest( |
||||
strtoupper($type) |
||||
, $url, |
||||
null, |
||||
$body, |
||||
$requestOptions |
||||
)->send(); |
||||
} catch(ClientErrorResponseException $e) { |
||||
if ($e->getResponse()->getStatusCode() == 404) { |
||||
return false; |
||||
} |
||||
throw new Exception("elasticsearch error:\n\n" |
||||
. $body . "\n\n" . $e->getMessage() |
||||
. print_r(Json::decode($e->getResponse()->getBody(true)), true), 0, $e); |
||||
} |
||||
if ($type == 'head') { |
||||
return $response->getStatusCode() == 200; |
||||
} |
||||
return Json::decode($response->getBody(true)); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue