/** * Register the singletons used in the application */ protected function registerServices() { // register the app as a singleton self::$instance = $this; static::openLog('app.main'); // Get container $container = $this->getContainer(); $config = $this->config; // Register component on container $container['view'] = function ($container) { $view = new Twig(VIEWS_DIR, ['cache' => CACHE_DIR . '/views', 'auto_reload' => $this->debug]); $view->addExtension(new TwigExtension($container['router'], $container['request']->getUri())); return $view; }; $container['elasticsearch'] = function ($container) use($config) { if (!empty($config['elasticsearch_user']) && !empty($config['elasticsearch_pass'])) { $host = sprintf('http://%s:%s@%s:%d', $config['elasticsearch_user'], $config['elasticsearch_pass'], $config['elasticsearch_host'], $config['elasticsearch_port']); } else { $host = sprintf('http://%s:%s', $config['elasticsearch_host'], $config['elasticsearch_port']); } // limit the hosts to one since we access it from a ELB $client = ClientBuilder::create()->setHosts([$host])->build(); if (!Util::checkHostAvailability($config['elasticsearch_host'], $config['elasticsearch_port'])) { throw new \Exception('The Elasticsearch cluster seems to be unavailable.'); } return $client; }; /** * @return Config */ $container['config'] = function () use($config) { return $config; }; }