Skip to content

Commit

Permalink
Add new getElasticConfig to ensure Laravel 4 support...
Browse files Browse the repository at this point in the history
  • Loading branch information
timgws committed Jan 11, 2016
1 parent bf4474e commit 657b7eb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
26 changes: 22 additions & 4 deletions src/ElasticquentCollectionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,32 @@ public function reindex()
* @return \Elasticsearch\Client
*/
public function getElasticSearchClient()
{
$config = $this->getElasticConfig();

return new \Elasticsearch\Client($config);
}

/**
* Get the Elasticquent config
*
* @return array configuration
*/
private function getElasticConfig()
{
$config = array();

if (config()->has('elasticquent.config')) {
$config = config()->get('elasticquent.config');
// Laravel 4 support
if (!function_exists('config')) {
$config_helper = app('config');
} else {
$config_helper = config();
}

return new \Elasticsearch\Client($config);
}
if ($config_helper->has('elasticquent.config')) {
$config = $config_helper->get('elasticquent.config');
}

return $config;
}
}
35 changes: 28 additions & 7 deletions src/ElasticquentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ trait ElasticquentTrait
*/
public function getElasticSearchClient()
{
$config = array();

if (config()->has('elasticquent.config')) {
$config = config()->get('elasticquent.config');
}
$config = $this->getElasticConfig();

return new \Elasticsearch\Client($config);
}
Expand All @@ -85,8 +81,10 @@ public function getIndexName()
// The first thing we check is if there
// is an elasticquery config file and if there is a
// default index.
if (config()->has('elasticquent.default_index')) {
return config()->get('elasticquent.default_index');
$index_name = $this->getElasticConfig('elasticquent.default_index');

if (!empty($index_name)) {
return $index_name;
}

// Otherwise we will just go with 'default'
Expand Down Expand Up @@ -609,4 +607,27 @@ public function newFromHitBuilder($hit = array())

return $instance;
}

/**
* Get the Elasticquent config
*
* @return array configuration
*/
private function getElasticConfig($key = 'elasticquent.config')
{
$config = array();

// Laravel 4 support
if (!function_exists('config')) {
$config_helper = app('config');
} else {
$config_helper = config();
}

if ($config_helper->has($key)) {
$config = $config_helper->get($key);
}

return $config;
}
}

0 comments on commit 657b7eb

Please sign in to comment.