share(function ($app) { $engineOptions = $app['conf']->get(['main', 'search-engine', 'options']); return $app['phraseanet.SE.engine-class']::create($app, $engineOptions); }); $app['phraseanet.SE.logger'] = $app->share(function (Application $app) { return new SearchEngineLogger($app); }); $app['phraseanet.SE.engine-class'] = $app->share(function ($app) { $engineClass = $app['conf']->get(['main', 'search-engine', 'type']); if (!class_exists($engineClass) || $engineClass instanceof SearchEngineInterface) { throw new InvalidArgumentException(sprintf('%s is not valid SearchEngineInterface', $engineClass)); } return $engineClass; }); $app['phraseanet.SE.subscriber'] = $app->share(function ($app) { return $app['phraseanet.SE.engine-class']::createSubscriber($app); }); $app['elasticsearch.indexer'] = $app->share(function ($app) { return new Indexer( $app['elasticsearch.client'], $app['elasticsearch.options'], $app['monolog'], $app['phraseanet.appbox'] ); }); $app['elasticsearch.client'] = $app->share(function($app) { $options = $app['elasticsearch.options']; $host = sprintf('%s:%s', $options['host'], $options['port']); return new Client(array('hosts' => array($host))); }); $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); }); } public function boot(Application $app) { if ($app['phraseanet.configuration']->isSetup()) { $app['dispatcher'] = $app->share( $app->extend('dispatcher', function ($dispatcher, Application $app) { $dispatcher->addSubscriber($app['phraseanet.SE.subscriber']); return $dispatcher; }) ); } } }