Skip to content

Commit

Permalink
service添加enable属性,当设置enable为false的时候,则这个service将会不可用
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyecommerce committed Aug 21, 2017
1 parent 2af0be0 commit f809f3a
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
2 changes: 2 additions & 0 deletions config/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'childService' => [
'mongoSearch' => [
'class' => 'fecshop\services\search\MongoSearch',
'enable' => true,
'searchIndexConfig' => [
'name' => 10,
'description' => 5,
Expand All @@ -34,6 +35,7 @@
],
'xunSearch' => [
'class' => 'fecshop\services\search\XunSearch',
'enable' => true,
/*
'fuzzy' => true, # 是否开启模糊查询
Expand Down
7 changes: 6 additions & 1 deletion services/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,16 @@ public function __construct($config = [])
*/
public function getChildService($childServiceName)
{

if (!$this->_childService[$childServiceName]) {
$childService = $this->childService;
if (isset($childService[$childServiceName])) {
$service = $childService[$childServiceName];
$this->_childService[$childServiceName] = Yii::createObject($service);
if(!isset($service['enable']) || $service['enable']){
$this->_childService[$childServiceName] = Yii::createObject($service);
}else{
throw new InvalidConfigException('Child Service ['.$childServiceName.'] is disable in '.get_called_class().', you must config it! ');
}
} else {
throw new InvalidConfigException('Child Service ['.$childServiceName.'] is not find in '.get_called_class().', you must config it! ');
}
Expand Down
28 changes: 23 additions & 5 deletions services/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,26 +57,44 @@ public function __call($originMethod, $arguments)
/**
* 得到services 里面配置的子服务childService的实例.
*/
protected function getChildService($childServiceName)
public function getChildService($childServiceName)
{
//var_dump($this->childService['xunSearch']);exit;
if (!$this->_childService[$childServiceName]) {

//var_dump($this->_childService['xunSearch']);exit;
$childService = $this->childService;
if (isset($childService[$childServiceName])) {
$service = $childService[$childServiceName];
$this->_childService[$childServiceName] = Yii::createObject($service);
if(!isset($service['enable']) || $service['enable']){
$this->_childService[$childServiceName] = Yii::createObject($service);
}else{
throw new InvalidConfigException('Child Service ['.$childServiceName.'] is disable in '.get_called_class().', you must config it! ');
}
} else {
throw new InvalidConfigException('Child Service ['.$childServiceName.'] is not find in '.get_called_class().', you must config it! ');
}
}

return $this->_childService[$childServiceName];
}

/**
* 得到所有的子服务
* 如果子服务含有enable字段,并且设置成false,则该子服务会被判定为关闭
*/
public function getAllChildServiceName()
{
$childService = $this->childService;

return array_keys($childService);
$arr = [];
if(is_array($childService) && !empty($childService)){
foreach($childService as $childName => $service){
if(!isset($service['enable']) || $service['enable']){
$arr[] = $childName;
}
}
}

return $arr;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions services/search/MongoSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MongoSearch extends Service implements SearchInterface
{
public $searchIndexConfig;
public $searchLang;

public $enable;
protected $_productModelName = '\fecshop\models\mongodb\Product';
protected $_productModel;
protected $_searchModelName = '\fecshop\models\mongodb\Search';
Expand Down Expand Up @@ -145,7 +145,8 @@ protected function actionSyncProductInfo($product_ids, $numPerPage)
}
}
}

echo "MongoSearch sync done ... \n";

return true;
}

Expand Down
5 changes: 3 additions & 2 deletions services/search/XunSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class XunSearch extends Service implements SearchInterface
public $searchLang;
public $fuzzy = false;
public $synonyms = false;

public $enable;
protected $_productModelName = '\fecshop\models\mongodb\Product';
protected $_productModel;
protected $_searchModelName = '\fecshop\models\xunsearch\Search';
Expand Down Expand Up @@ -84,7 +84,8 @@ protected function actionSyncProductInfo($product_ids, $numPerPage)
}
}
}

echo "XunSearch sync done ... \n";

return true;
}

Expand Down

0 comments on commit f809f3a

Please sign in to comment.