Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"license": "MIT",
"authors": [
{
"name": "Ivo Verberk"
"version":"0.8.1-alpha1427726533","name": "Ivo Verberk"
}
],
"require": {
"php": ">=5.4.0",
"illuminate/support": "~4.2",
"illuminate/database": "~4.2",
"illuminate/console": "~4.2",
"illuminate/support": "~4.2|~5.0",
"illuminate/database": "~4.2|~5.0",
"illuminate/console": "~4.2|~5.0",
"doctrine/dbal": "~2.3",
"elasticsearch/elasticsearch": "~1.0",
"nikic/php-parser": "*"
Expand Down
43 changes: 31 additions & 12 deletions src/Iverberk/Larasearch/Commands/PathsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,40 @@ private function writeConfig()
{
if ($this->option('write-config'))
{
$configDir = app_path() . '/config/packages/iverberk/larasearch';
if (version_compare(constant('Illuminate\\Foundation\\Application::VERSION'), '5.0.0', '>='))
{
$configFile = config_path() . '/larasearch.php';

if (!File::exists($configDir))
{
if ($this->confirm('It appears that you have not yet published the larasearch config. Would you like to do this now?', false))
{
$this->call('config:publish', ['package' => 'iverberk/larasearch']);
}
else
{
return;
}
if (!File::exists($configFile))
{
if ($this->confirm('It appears that you have not yet published the larasearch config. Would you like to do this now?', false))
{
$this->call('vendor:publish', ['--provider' => 'Iverberk\\Larasearch\\Providers\\L5ServiceProvider', '--tag' => 'config' ]);
}
else
{
return;
}
}
}
else
{
$configFile = app_path() . '/config/packages/iverberk/larasearch/config.php';

if (!File::exists($configFile))
{
if ($this->confirm('It appears that you have not yet published the larasearch config. Would you like to do this now?', false))
{
$this->call('config:publish', ['package' => 'iverberk/larasearch']);
}
else
{
return;
}
}
}

File::put("${configDir}/paths.json", json_encode(['paths' => $this->paths, 'reversedPaths' => $this->reversedPaths], JSON_PRETTY_PRINT));
File::put(dirname($configFile) . "/paths.json", json_encode(['paths' => $this->paths, 'reversedPaths' => $this->reversedPaths], JSON_PRETTY_PRINT));

$this->info('Paths file written to local package configuration');
}
Expand Down
59 changes: 59 additions & 0 deletions src/Iverberk/Larasearch/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php namespace Iverberk\Larasearch;

use Illuminate\Config\Repository;

class Config {

/**
* Laravel's configuation repository
*
* @var Illuminate\Config\Repository
*/
protected $config;

/**
* Root configuration namespace
*
* @var string
*/
protected $packageName;

/**
* String delimiter between package name and config setting key.
*
* @var string
*/
protected $delimiter = '.';

public function __construct(Repository $config, $packageName = 'larasearch')
{
$this->config = $config;
$this->setPackageName($packageName);

if (version_compare(constant('Illuminate\\Foundation\\Application::VERSION'), '5.0.0', '<')) $this->delimiter = '::';
}

public function setPackageName($packageName)
{
if ($packageName)
{
$this->packageName = (string) $packageName;
}
}

public function getPackageName()
{
return $this->packageName;
}

public function get($name, $default = null)
{
return $this->config->get("{$this->packageName}{$this->delimiter}{$name}", $default);
}

public function set($name, $value = null)
{
return $this->config->set("{$this->packageName}{$this->delimiter}{$name}", $value);
}

}
30 changes: 21 additions & 9 deletions src/Iverberk/Larasearch/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Str;
use Iverberk\Larasearch\Exceptions\ImportException;

Expand All @@ -22,6 +21,13 @@ class Index {
*/
private static $client;

/**
* Laravel config repository abstraction
*
* @var \Iverberk\Larasearch\Config
*/
private static $config;

/**
* Index parameters
*
Expand Down Expand Up @@ -54,11 +60,17 @@ private static function getClient()
public function __construct(Proxy $proxy, $name = '')
{
self::$client = App::make('Elasticsearch');
self::$config = App::make('iverberk.larasearch.config');

$this->setProxy($proxy);
$this->setName($name ?: $proxy->getModel()->getTable());
}

protected static function getConfig()
{
return self::$config;
}

/**
* Import an Eloquent
*
Expand Down Expand Up @@ -120,7 +132,7 @@ public function import(Model $model, $relations = [], $batchSize = 750, Callable
*/
public function setName($name)
{
$index_prefix = Config::get('larasearch::elasticsearch.index_prefix', '');
$index_prefix = self::getConfig()->get('elasticsearch.index_prefix', '');
if ($index_prefix && ! Str::startsWith($name, $index_prefix)) $name = $index_prefix . $name;

$this->name = $name;
Expand Down Expand Up @@ -200,7 +212,7 @@ public function exists()
*/
public function aliasExists($alias)
{
$index_prefix = Config::get('larasearch::elasticsearch.index_prefix', '');
$index_prefix = self::getConfig()->get('elasticsearch.index_prefix', '');
if ($index_prefix && ! Str::startsWith($alias, $index_prefix)) $alias = $index_prefix . $alias;

return self::getClient()->indices()->existsAlias(['name' => $alias]);
Expand Down Expand Up @@ -311,7 +323,7 @@ public function bulk($records)
*/
public static function clean($name)
{
$index_prefix = Config::get('larasearch::elasticsearch.index_prefix', '');
$index_prefix = self::getConfig()->get('elasticsearch.index_prefix', '');
if ($index_prefix && ! Str::startsWith($name, $index_prefix)) $name = $index_prefix . $name;

$indices = self::getClient()->indices()->getAliases();
Expand All @@ -332,7 +344,7 @@ public static function clean($name)
*/
public static function getAlias($name)
{
$index_prefix = Config::get('larasearch::elasticsearch.index_prefix', '');
$index_prefix = self::getConfig()->get('elasticsearch.index_prefix', '');
if ($index_prefix && ! Str::startsWith($name, $index_prefix)) $name = $index_prefix . $name;

return self::getClient()->indices()->getAlias(['name' => $name]);
Expand All @@ -344,7 +356,7 @@ public static function getAlias($name)
*/
public static function updateAliases(array $actions)
{
if (isset($actions['actions']) && ($index_prefix = Config::get('larasearch::elasticsearch.index_prefix', '')))
if (isset($actions['actions']) && ($index_prefix = self::getConfig()->get('elasticsearch.index_prefix', '')))
{
foreach ($actions['actions'] as &$action)
{
Expand All @@ -365,7 +377,7 @@ public static function updateAliases(array $actions)
*/
public static function refresh($index)
{
$index_prefix = Config::get('larasearch::elasticsearch.index_prefix', '');
$index_prefix = self::getConfig()->get('elasticsearch.index_prefix', '');
if ($index_prefix && ! Str::startsWith($index, $index_prefix)) $index = $index_prefix . $index;

return self::getClient()->indices()->refresh(['index' => $index]);
Expand All @@ -378,8 +390,8 @@ public static function refresh($index)
*/
private function getDefaultIndexParams()
{
$analyzers = Config::get('larasearch::elasticsearch.analyzers');
$params = Config::get('larasearch::elasticsearch.defaults.index');
$analyzers = self::getConfig()->get('elasticsearch.analyzers');
$params = self::getConfig()->get('elasticsearch.defaults.index');
$mapping = [];

$mapping_options = array_combine(
Expand Down
10 changes: 5 additions & 5 deletions src/Iverberk/Larasearch/Jobs/DeleteJob.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Iverberk\Larasearch\Jobs;

use Illuminate\Config\Repository;
use Iverberk\Larasearch\Config;
use Illuminate\Foundation\Application;
use Illuminate\Queue\Jobs\Job;

Expand All @@ -17,15 +17,15 @@ class DeleteJob {
private $app;

/**
* @var Repository
* @var Config
*/
private $config;

/**
* @param Application $app
* @param Repository $config
* @param Config
*/
public function __construct(Application $app, Repository $config)
public function __construct(Application $app, Config $config)
{
$this->app = $app;
$this->config = $config;
Expand All @@ -37,7 +37,7 @@ public function __construct(Application $app, Repository $config)
*/
public function fire(Job $job, $models)
{
$loggerContainerBinding = $this->config->get('larasearch::logger', 'iverberk.larasearch.logger');
$loggerContainerBinding = $this->config->get('logger', 'iverberk.larasearch.logger');
$logger = $this->app->make($loggerContainerBinding);

foreach ($models as $model)
Expand Down
10 changes: 5 additions & 5 deletions src/Iverberk/Larasearch/Jobs/ReindexJob.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php namespace Iverberk\Larasearch\Jobs;

use Illuminate\Config\Repository;
use Iverberk\Larasearch\Config;
use Illuminate\Foundation\Application;
use Illuminate\Queue\Jobs\Job;
use Exception;
Expand All @@ -18,23 +18,23 @@ class ReindexJob {
private $app;

/**
* @var Repository
* @var Config
*/
private $config;

/**
* @param Application $app
* @param Repository $config
* @param Config
*/
public function __construct(Application $app, Repository $config)
public function __construct(Application $app, Config $config)
{
$this->app = $app;
$this->config = $config;
}

public function fire(Job $job, $models)
{
$loggerContainerBinding = $this->config->get('larasearch::logger', 'iverberk.larasearch.logger');
$loggerContainerBinding = $this->config->get('logger', 'iverberk.larasearch.logger');
$logger = $this->app->make($loggerContainerBinding);

foreach ($models as $model)
Expand Down
Loading