Files
Phraseanet/lib/Alchemy/Phrasea/Core/Provider/SearchEngineServiceProvider.php

146 lines
5.1 KiB
PHP

<?php
/*
* This file is part of Phraseanet
*
* (c) 2005-2014 Alchemy
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Alchemy\Phrasea\Core\Provider;
use Alchemy\Phrasea\SearchEngine\SearchEngineLogger;
use Alchemy\Phrasea\Exception\InvalidArgumentException;
use Alchemy\Phrasea\SearchEngine\SearchEngineInterface;
use Alchemy\Phrasea\SearchEngine\Elastic\ElasticSearchEngine;
use Alchemy\Phrasea\SearchEngine\Elastic\Indexer;
use Alchemy\Phrasea\SearchEngine\Elastic\Indexer\RecordIndexer;
use Alchemy\Phrasea\SearchEngine\Elastic\Indexer\TermIndexer;
use Alchemy\Phrasea\SearchEngine\Elastic\Thesaurus;
use Alchemy\Phrasea\SearchEngine\Phrasea\PhraseaEngine;
use Alchemy\Phrasea\SearchEngine\Phrasea\PhraseaEngineSubscriber;
use Elasticsearch\Client;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use Silex\Application;
use Silex\ServiceProviderInterface;
class SearchEngineServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['phraseanet.SE'] = function ($app) {
return $app['search_engine'];
};
$app['search_engine'] = $app->share(function ($app) {
$type = $app['search_engine.type'];
switch ($type) {
case SearchEngineInterface::TYPE_ELASTICSEARCH:
return $app['elasticsearch.engine'];
case SearchEngineInterface::TYPE_PHRASEA:
return new PhraseaEngine($app);
default:
throw new InvalidArgumentException(sprintf('Invalid search engine type "%s".', $type));
}
});
$app['search_engine.type'] = function ($app) {
return $app['conf']->get(['main', 'search-engine', 'type']);
};
$app['phraseanet.SE.logger'] = $app->share(function (Application $app) {
return new SearchEngineLogger($app);
});
// Only used for Phrasea search engine
$app['phraseanet.SE.subscriber'] = $app->share(function ($app) {
return new PhraseaEngineSubscriber($app);
});
$app['elasticsearch.engine'] = $app->share(function ($app) {
return new ElasticSearchEngine(
$app,
$app['elasticsearch.client'],
$app['serializer.es-record'],
$app['elasticsearch.options']['index']
);
});
$app['elasticsearch.indexer'] = $app->share(function ($app) {
return new Indexer(
$app['elasticsearch.client'],
$app['elasticsearch.options'],
$app['elasticsearch.indexer.term_indexer'],
$app['elasticsearch.indexer.record_indexer'],
$app['monolog']
);
});
$app['elasticsearch.indexer.term_indexer'] = $app->share(function ($app) {
return new TermIndexer($app['phraseanet.appbox']);
});
$app['elasticsearch.indexer.record_indexer'] = $app->share(function ($app) {
return new RecordIndexer(
$app['elasticsearch.thesaurus'],
$app['elasticsearch.engine'],
$app['phraseanet.appbox']
);
});
$app['elasticsearch.client'] = $app->share(function($app) {
$options = $app['elasticsearch.options'];
$clientParams = ['hosts' => [sprintf('%s:%s', $options['host'], $options['port'])]];
// Create file logger for debug
if ($app['debug']) {
$logger = new $app['monolog.logger.class']('search logger');
$logger->pushHandler(new RotatingFileHandler($app['log.path'].DIRECTORY_SEPARATOR.'elasticsearch.log', 2), Logger::INFO);
$clientParams['logObject'] = $logger;
$clientParams['logging'] = true;
}
return new Client($clientParams);
});
$app['elasticsearch.options'] = $app->share(function($app) {
$options = $app['conf']->get(['main', 'search-engine', 'options']);
$defaults = [
'host' => '127.0.0.1',
'port' => 9200,
'index' => 'phraseanet',
'shards' => 3,
'replicas' => 0
];
return array_replace($defaults, $options);
});
$app['elasticsearch.thesaurus'] = $app->share(function ($app) {
return new Thesaurus(
$app['elasticsearch.client'],
$app['elasticsearch.options']['index']
);
});
}
public function boot(Application $app)
{
if (!$app['phraseanet.configuration']->isSetup()) {
return;
}
if ($app['search_engine.type'] === SearchEngineInterface::TYPE_PHRASEA) {
$app['dispatcher'] = $app->share($app->extend('dispatcher', function ($dispatcher, Application $app) {
$dispatcher->addSubscriber($app['phraseanet.SE.subscriber']);
return $dispatcher;
}));
}
}
}