From 40da649b1078d34b9a99d7a42fc3471e9a188eaf Mon Sep 17 00:00:00 2001 From: Kode Date: Fri, 18 Jan 2019 18:21:44 +0000 Subject: [PATCH] Changes to search --- app/Http/Controllers/SearchController.php | 8 +-- app/Search.php | 61 ++++++++++++----------- app/SearchInterface.php | 2 +- 3 files changed, 37 insertions(+), 34 deletions(-) diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 5b0b048a..0accb89c 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -15,10 +15,12 @@ class SearchController extends Controller $provider = Search::providerDetails($requestprovider); - if($provider->type == 'external') { + if($provider->type == 'standard') { return redirect($provider->url.'?'.$provider->var.'='.urlencode($query)); - } else { - // get results + } elseif($provider->type == 'external') { + $class = new $provider->class; + //print_r($provider); + return $class->getResults($query, $provider); } //print_r($provider); diff --git a/app/Search.php b/app/Search.php index 9924d72b..13437bd7 100644 --- a/app/Search.php +++ b/app/Search.php @@ -10,21 +10,35 @@ use Cache; abstract class Search { + /** + * List of all search providers + * + * @return Array + */ public static function providers() { $providers = self::standardProviders(); $providers = $providers + self::appProviders(); - // Need something to add in none standard providers - //die(print_r($providers)); return $providers; } + /** + * Gets details for a single provider + * + * @return Object + */ public static function providerDetails($provider) { $providers = self::providers(); + if(!isset($providers[$provider])) return false; return (object)$providers[$provider] ?? false; } + /** + * Array of the standard providers + * + * @return Array + */ public static function standardProviders() { return [ @@ -32,23 +46,29 @@ abstract class Search 'url' => 'https://www.google.com/search', 'var' => 'q', 'method' => 'get', - 'type' => 'external', + 'type' => 'standard', ], 'ddg' => [ 'url' => 'https://duckduckgo.com/', 'var' => 'q', 'method' => 'get', - 'type' => 'external', + 'type' => 'standard', ], 'bing' => [ 'url' => 'https://www.bing.com/search', 'var' => 'q', 'method' => 'get', - 'type' => 'external', + 'type' => 'standard', ], ]; } + /** + * Loops through users apps to see if app is a search provider, might be worth + * looking into caching this at some point + * + * @return Array + */ public static function appProviders() { $providers = []; @@ -59,6 +79,8 @@ abstract class Search $name = Item::nameFromClass($app->class); $providers[strtolower($name)] = [ 'type' => $provider->type, + 'class' => $app->class, + 'url' => $app->url, ]; } @@ -66,27 +88,9 @@ abstract class Search return $providers; } - public static function storeSearchProvider($class, $app) - { - if(!empty($class)) { - if(($provider = Item::isSearchProvider($class)) !== false) { - $providers = Cache::get('search_providers', []); - $name = Item::nameFromClass($class); - - $search = new $class; - - $providers[strtolower($name)] = [ - 'url' => '', - 'var' => '', - 'type' => $search->type, - - ]; - } - } - } - - - /** + /** + * Outputs the search form + * * @return html */ public static function form() @@ -100,10 +104,7 @@ abstract class Search //die(var_dump($user_search_provider)); // return early if search isn't applicable if((bool)$homepage_search !== true) return $output; - if($user_search_provider === 'none') return $output; - if(empty($user_search_provider)) return $output; - if(is_null($user_search_provider)) return $output; - + $user_search_provider = $user_search_provider ?? 'none'; if((bool)$homepage_search && (bool)$search_provider) { diff --git a/app/SearchInterface.php b/app/SearchInterface.php index 5ea3b5fd..62c5e4dc 100644 --- a/app/SearchInterface.php +++ b/app/SearchInterface.php @@ -5,6 +5,6 @@ use GuzzleHttp\Client; interface SearchInterface { - public function getResults(); + public function getResults($query, $providerdetails); } \ No newline at end of file