FEATURE: add registering for new search engines.
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Handlers\Contracts;
|
||||||
|
|
||||||
|
interface SearchHandler {
|
||||||
|
public function getId();
|
||||||
|
|
||||||
|
public function getName();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for given patttern/term
|
||||||
|
*
|
||||||
|
* @param string $term searched word
|
||||||
|
*
|
||||||
|
* @return array list of found objects
|
||||||
|
*/
|
||||||
|
public function search($term);
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Handlers\Contracts;
|
||||||
|
|
||||||
|
interface SearchPresenterHandler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Method for rendering list of found results
|
||||||
|
*
|
||||||
|
* @param $foundItems
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function render($foundItems);
|
||||||
|
}
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Handlers\Registrators;
|
||||||
|
|
||||||
|
use App\Enumerations\Exceptions\EnumerationExistsException;
|
||||||
|
use App\Handlers\Contracts\SearchHandler;
|
||||||
|
use App\Handlers\Contracts\SearchPresenterHandler;
|
||||||
|
use App\Handlers\Registrators\Items\SearchEngine;
|
||||||
|
|
||||||
|
class SearchEngineRegistrator
|
||||||
|
{
|
||||||
|
protected $searchEngines;
|
||||||
|
|
||||||
|
function __construct($app)
|
||||||
|
{
|
||||||
|
$this->searchEngines = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method for registering new settings extensions/parts
|
||||||
|
*
|
||||||
|
* @param SearchHandler $search
|
||||||
|
* @param SearchPresenterHandler $presenter
|
||||||
|
*
|
||||||
|
* @throws EnumerationExistsException
|
||||||
|
*/
|
||||||
|
public function register(SearchHandler $search, SearchPresenterHandler $presenter)
|
||||||
|
{
|
||||||
|
$this->checkIfRegistered($search->getId());
|
||||||
|
|
||||||
|
// register new extension
|
||||||
|
$this->searchEngines[$search->getId()] = [$search, $presenter];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Getter for all registered extensions
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function get()
|
||||||
|
{
|
||||||
|
return $this->searchEngines;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks if this section is not already defined
|
||||||
|
*
|
||||||
|
* @param $id
|
||||||
|
*
|
||||||
|
* @throws EnumerationExistsException
|
||||||
|
*/
|
||||||
|
private function checkIfRegistered($id)
|
||||||
|
{
|
||||||
|
if (isset($this->searchEngines[$id])) {
|
||||||
|
throw new EnumerationExistsException('Already existing search engine ' . $id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Handlers\Search;
|
||||||
|
|
||||||
|
use App\Handlers\Contracts\SearchHandler;
|
||||||
|
|
||||||
|
class Content implements SearchHandler
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Search for given patttern/term
|
||||||
|
*
|
||||||
|
* @param string $term searched word
|
||||||
|
*
|
||||||
|
* @return array list of found objects
|
||||||
|
*/
|
||||||
|
public function search($term)
|
||||||
|
{
|
||||||
|
$token = '%' . $term . '%';
|
||||||
|
|
||||||
|
return Content::where('content', 'LIKE', $token)
|
||||||
|
->orWhere('description', 'LIKE', $token)
|
||||||
|
->orWhere('keywords', 'LIKE', $token)
|
||||||
|
->orWhere('title', 'LIKE', $token)
|
||||||
|
->orWhere('url', 'LIKE', $token)
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getId()
|
||||||
|
{
|
||||||
|
return 'content';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName()
|
||||||
|
{
|
||||||
|
return trans('...');
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
namespace App\Handlers\Search\Presenters;
|
||||||
|
|
||||||
|
use App\Handlers\Contracts\SearchPresenterHandler;
|
||||||
|
|
||||||
|
class Content implements SearchPresenterHandler
|
||||||
|
{
|
||||||
|
public function render($foundItems)
|
||||||
|
{
|
||||||
|
var_dump($foundItems);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,9 +2,12 @@
|
|||||||
|
|
||||||
namespace App\Providers;
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use App\Handlers\Registrators\SearchEngineRegistrator;
|
||||||
use App\Handlers\Registrators\SettingsExtensionRegistrator;
|
use App\Handlers\Registrators\SettingsExtensionRegistrator;
|
||||||
use App\Handlers\Registrators\TemplateRegistrator;
|
use App\Handlers\Registrators\TemplateRegistrator;
|
||||||
use App\Handlers\Registrators\WidgetRegistrator;
|
use App\Handlers\Registrators\WidgetRegistrator;
|
||||||
|
use App\Handlers\Search\Content;
|
||||||
|
use App\Handlers\Search\Presenters\Content as ContentPresenter;
|
||||||
use App\Widgets\HtmlCode\HtmlCode;
|
use App\Widgets\HtmlCode\HtmlCode;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
@@ -20,6 +23,7 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
$this->registerWidgets();
|
$this->registerWidgets();
|
||||||
$this->registerTemplates();
|
$this->registerTemplates();
|
||||||
$this->registerSettings();
|
$this->registerSettings();
|
||||||
|
$this->registerSearch();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -51,6 +55,10 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
$this->app->singleton('registerSettingsExtension', function ($app) {
|
$this->app->singleton('registerSettingsExtension', function ($app) {
|
||||||
return new SettingsExtensionRegistrator($app);
|
return new SettingsExtensionRegistrator($app);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->app->singleton('registerSearchEngine', function ($app) {
|
||||||
|
return new SearchEngineRegistrator($app);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -91,4 +99,14 @@ class AppServiceProvider extends ServiceProvider
|
|||||||
$registrator->register('basic', 'admin/settings.basic', 'admin.settings.parts.basic');
|
$registrator->register('basic', 'admin/settings.basic', 'admin.settings.parts.basic');
|
||||||
$registrator->register('meta', 'admin/settings.meta', 'admin.settings.parts.meta');
|
$registrator->register('meta', 'admin/settings.meta', 'admin.settings.parts.meta');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register callbacks used for searching
|
||||||
|
*/
|
||||||
|
private function registerSearch()
|
||||||
|
{
|
||||||
|
$registrator = $this->app->make('registerSearchEngine');
|
||||||
|
|
||||||
|
$registrator->register(new Content(), new ContentPresenter());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user