Updating the plugin to restore functionality with Laravel 5. Tomorrow... decoupling.
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
<?php namespace TomLingham\Searchy;
|
<?php namespace TomLingham\Searchy;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\Config;
|
use Illuminate\Config\Repository;
|
||||||
use TomLingham\Searchy\SearchDrivers\FuzzySearchDriver;
|
use TomLingham\Searchy\SearchDrivers\FuzzySearchDriver;
|
||||||
|
|
||||||
|
|
||||||
@ -25,6 +25,14 @@ class SearchBuilder {
|
|||||||
*/
|
*/
|
||||||
private $driverName;
|
private $driverName;
|
||||||
|
|
||||||
|
|
||||||
|
private $config;
|
||||||
|
|
||||||
|
public function __construct( Repository $config )
|
||||||
|
{
|
||||||
|
$this->config = $config;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return $this
|
* @return $this
|
||||||
@ -84,13 +92,11 @@ class SearchBuilder {
|
|||||||
if ( $this->driverName ){
|
if ( $this->driverName ){
|
||||||
$driverName = $this->driverName;
|
$driverName = $this->driverName;
|
||||||
} else {
|
} else {
|
||||||
$driverName = Config::get('searchy::default');
|
$driverName = $this->config->get('searchy.default');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Gets the details for the selected driver from the configuration file
|
// Gets the details for the selected driver from the configuration file
|
||||||
$driverMap = Config::get("searchy::drivers.$driverName");
|
$driverMap = $this->config->get("searchy.drivers.$driverName");
|
||||||
|
|
||||||
|
|
||||||
// Create a new instance of the selected drivers 'class' and pass
|
// Create a new instance of the selected drivers 'class' and pass
|
||||||
// through table and fields to search
|
// through table and fields to search
|
||||||
|
@ -23,32 +23,37 @@ abstract class BaseSearchDriver implements SearchDriverInterface {
|
|||||||
*/
|
*/
|
||||||
protected $table;
|
protected $table;
|
||||||
|
|
||||||
|
protected $columns;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null $table
|
* @param null $table
|
||||||
* @param array $searchFields
|
* @param array $searchFields
|
||||||
|
* @param array $columns
|
||||||
*/
|
*/
|
||||||
public function __construct( $table = null, $searchFields = [] )
|
public function __construct( $table = null, $searchFields = [], $columns = ['*'] )
|
||||||
{
|
{
|
||||||
$this->searchFields = $searchFields;
|
$this->searchFields = $searchFields;
|
||||||
$this->table = $table;
|
$this->table = $table;
|
||||||
|
$this->columns = $columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $searchString
|
* @param $searchString
|
||||||
* @return \Illuminate\Database\Query\Builder|mixed|static
|
* @return \Illuminate\Database\Query\Builder|mixed|static
|
||||||
* @throws \Whoops\Example\Exception
|
|
||||||
*/
|
*/
|
||||||
public function query( $searchString )
|
public function query( $searchString )
|
||||||
{
|
{
|
||||||
|
|
||||||
if(\Config::get('searchy::sanitize'))
|
if(\Config::get('searchy.sanitize'))
|
||||||
$this->searchString = $this->sanitize($searchString);
|
$this->searchString = $this->sanitize($searchString);
|
||||||
|
|
||||||
$results = \DB::table($this->table)
|
$results = \DB::table($this->table)
|
||||||
->select('*')
|
->select( implode($this->columns, ', ') )
|
||||||
->addSelect($this->buildSelectQuery( $this->searchFields ))
|
->addSelect($this->buildSelectQuery( $this->searchFields ))
|
||||||
->orderBy(\Config::get('searchy::fieldName'), 'desc')
|
->orderBy(\Config::get('searchy.fieldName'), 'desc')
|
||||||
->having(\Config::get('searchy::fieldName'),'>', 0);
|
->having(\Config::get('searchy.fieldName'),'>', 0);
|
||||||
|
|
||||||
|
die($results->toSQL());
|
||||||
|
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
@ -71,7 +76,7 @@ abstract class BaseSearchDriver implements SearchDriverInterface {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return \DB::raw(implode(' + ', $query) . ' AS ' . \Config::get('searchy::fieldName'));
|
return \DB::raw(implode(' + ', $query) . ' AS ' . \Config::get('searchy.fieldName'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,7 +118,7 @@ abstract class BaseSearchDriver implements SearchDriverInterface {
|
|||||||
*/
|
*/
|
||||||
private function sanitize( $searchString )
|
private function sanitize( $searchString )
|
||||||
{
|
{
|
||||||
return preg_replace(\Config::get('searchy::sanitizeRegEx'), '', $searchString );
|
return preg_replace(\Config::get('searchy.sanitizeRegEx'), '', $searchString );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,5 +1,6 @@
|
|||||||
<?php namespace TomLingham\Searchy;
|
<?php namespace TomLingham\Searchy;
|
||||||
|
|
||||||
|
use Illuminate\Config\Repository;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
class SearchyServiceProvider extends ServiceProvider {
|
class SearchyServiceProvider extends ServiceProvider {
|
||||||
@ -18,9 +19,9 @@ class SearchyServiceProvider extends ServiceProvider {
|
|||||||
*/
|
*/
|
||||||
public function register()
|
public function register()
|
||||||
{
|
{
|
||||||
$this->app->bindShared('searchy', function($app)
|
$this->app->bindShared('searchy', function( $app )
|
||||||
{
|
{
|
||||||
return new SearchBuilder();
|
return new SearchBuilder( $app['config'] );
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +30,9 @@ class SearchyServiceProvider extends ServiceProvider {
|
|||||||
*/
|
*/
|
||||||
public function boot()
|
public function boot()
|
||||||
{
|
{
|
||||||
$this->package('tom-lingham/searchy');
|
$this->publishes([
|
||||||
|
__DIR__.'/../../config/config.php' => config_path('searchy.php'),
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user