From 0bdb58e2a624dbc3d33cbdc3342325841186fc4e Mon Sep 17 00:00:00 2001 From: Colin Viebrock Date: Thu, 31 Aug 2017 14:31:13 -0500 Subject: [PATCH] update for Laravel 5.5, consolidate Lumen files --- .travis.yml | 16 ++++- CHANGELOG.md | 6 ++ README.md | 39 +++------- TODO.md | 4 +- composer.json | 76 ++++++++++---------- src/Facade.php | 4 +- src/Factory.php | 5 +- src/LumenManager.php | 135 ----------------------------------- src/LumenServiceProvider.php | 47 ------------ src/Manager.php | 36 ++++++---- src/ServiceProvider.php | 42 +++++++---- 11 files changed, 123 insertions(+), 287 deletions(-) delete mode 100644 src/LumenManager.php delete mode 100644 src/LumenServiceProvider.php diff --git a/.travis.yml b/.travis.yml index 3b10546..d7cc843 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,9 +3,19 @@ language: php php: - - 5.6 - 7.0 + - 7.1 + - 7.2 + +env: + - COMPOSER_FLAGS="--prefer-stable --prefer-lowest" + - COMPOSER_FLAGS="" + +matrix: + exclude: + - php: 7.2 + env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" before_script: - - curl -s http://getcomposer.org/installer | php - - php composer.phar install --dev + - travis_retry composer self-update + - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source diff --git a/CHANGELOG.md b/CHANGELOG.md index 81248a6..4df3e49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 2.1.0 - 31-Aug-2017 + +- Laravel 5.5 support, including auto-registration +- Bump ES client version to ^5.3 + + ## 2.0.1 - 14-Jul-2017 * Add alias for Lumen (thanks @matejvelikonja) diff --git a/README.md b/README.md index 69d9d06..f78a497 100644 --- a/README.md +++ b/README.md @@ -19,36 +19,24 @@ An easy way to use the [official Elastic Search client](https://github.com/elast Install the `cviebrock/laravel-elasticsearch` package via composer: -```shell +```sh composer require cviebrock/laravel-elasticsearch ``` -### Laravel +### Laravel -Add the service provider and facade to `config/app.php`: - -```php -'providers' => [ - ... - Cviebrock\LaravelElasticsearch\ServiceProvider::class, -] - -'aliases' => [ - ... - 'Elasticsearch' => Cviebrock\LaravelElasticsearch\Facade::class, -] -``` +The package will automatically register itself. Publish the configuration file: -```shell +```sh php artisan vendor:publish --provider="Cviebrock\LaravelElasticsearch\ServiceProvider" ``` ##### Alternative configuration method via .env file After you publish the configuration file as suggested above, you may configure Elastic Search -by adding the following to laravel `.env` file +by adding the following to your application's `.env` file (with appropriate values): ```ini ELASTICSEARCH_HOST=localhost @@ -60,20 +48,15 @@ ELASTICSEARCH_PASS= ### Lumen -If you work with Lumen, please register the LumenServiceProvider in `bootstrap/app.php`: - -```php -$app->register(Cviebrock\LaravelElasticsearch\LumenServiceProvider::class); -``` - -And manually copy the configuration file to your application. - -**Note:** don't forget to register your elasticsearch.php config in bootstrap/app.php +If you work with Lumen, please register the service provider and configuration in `bootstrap/app.php`: ```php +$app->register(Cviebrock\LaravelElasticsearch\ServiceProvider::class); $app->configure('elasticsearch'); ``` +Manually copy the configuration file to your application. + ## Usage @@ -114,7 +97,7 @@ in order to get the ES service object: ```php // using injection: -public function handle(\Cviebrock\LaravelElasticsearch\LumenManager $elasticsearch) +public function handle(\Cviebrock\LaravelElasticsearch\Manager $elasticsearch) { $elasticsearch->ping(); } @@ -123,7 +106,7 @@ public function handle(\Cviebrock\LaravelElasticsearch\LumenManager $elasticsear $elasticSearch = $this->app('elasticsearch'); ``` -(Of course, DI and the application container work for Laravel as well.) +Of course, DI and the application container work for Laravel as well. ## Bugs, Suggestions and Contributions diff --git a/TODO.md b/TODO.md index 8f6082f..0a5dffc 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,5 @@ -# To-do List +# Todos +- [ ] Tests! - [ ] More documentation - [ ] Allow for client "wrappers" that add functionality to the client (e.g., automatic index prefixing/suffixing, index templates configuration, etc.) -- [ ] Tests! diff --git a/composer.json b/composer.json index f8d2b66..6ab09fd 100644 --- a/composer.json +++ b/composer.json @@ -1,40 +1,40 @@ { - "name": "cviebrock/laravel-elasticsearch", - "description": "An easy way to use the official PHP ElasticSearch client in your Laravel applications.", - "keywords": [ - "laravel", - "elasticsearch", - "search", - "client" - ], - "require": { - "php": ">=5.4.0", - "elasticsearch/elasticsearch": "^5.0", - "illuminate/support": "~5", - "monolog/monolog": "~1" - }, - "license": "MIT", - "authors": [ - { - "name": "Colin Viebrock", - "email": "colin@viebrock.ca" - } - ], - "autoload": { - "psr-4": { - "Cviebrock\\LaravelElasticsearch\\": "src/" - } - }, - "minimum-stability": "stable", - "prefer-stable": true, - "extra": { - "laravel": { - "providers": [ - "Cviebrock\\LaravelElasticsearch\\ServiceProvider" - ], - "aliases": { - "Elasticsearch": "Cviebrock\\LaravelElasticsearch\\Facade" - } - } - } + "name": "cviebrock/laravel-elasticsearch", + "description": "An easy way to use the official PHP ElasticSearch client in your Laravel applications.", + "keywords": [ + "laravel", + "elasticsearch", + "search", + "client" + ], + "require": { + "php": "^7.0", + "elasticsearch/elasticsearch": "^5.3", + "illuminate/support": "~5.5.0", + "monolog/monolog": "^1.23" + }, + "license": "MIT", + "authors": [ + { + "name": "Colin Viebrock", + "email": "colin@viebrock.ca" + } + ], + "autoload": { + "psr-4": { + "Cviebrock\\LaravelElasticsearch\\": "src/" + } + }, + "extra": { + "laravel": { + "providers": [ + "Cviebrock\\LaravelElasticsearch\\ServiceProvider" + ], + "aliases": { + "Elasticsearch": "Cviebrock\\LaravelElasticsearch\\Facade" + } + } + }, + "minimum-stability": "stable", + "prefer-stable": true } diff --git a/src/Facade.php b/src/Facade.php index 2ba556d..b8f1188 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -12,9 +12,7 @@ class Facade extends BaseFacade { /** - * Get the registered name of the component. - * - * @return string + * @inheritdoc */ protected static function getFacadeAccessor() { diff --git a/src/Factory.php b/src/Factory.php index 3373ab0..696c904 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -1,5 +1,6 @@ app = $app; - $this->factory = $factory; - } - - /** - * Retrieve or build the named connection. - * - * @param null $name - * @return \Elasticsearch\Client|mixed - */ - public function connection($name = null) - { - $name = $name ?: $this->getDefaultConnection(); - - if (!isset($this->connections[$name])) { - $client = $this->makeConnection($name); - - $this->connections[$name] = $client; - } - - return $this->connections[$name]; - } - - /** - * Get the default connection. - * - * @return string - */ - public function getDefaultConnection() - { - return $this->app['config']['elasticsearch.defaultConnection']; - } - - /** - * Set the default connection. - * - * @param string $connection - */ - public function setDefaultConnection($connection) - { - $this->app['config']['elasticsearch.defaultConnection'] = $connection; - } - - /** - * Make a new connection. - * - * @param $name - * @return \Elasticsearch\Client|mixed - */ - protected function makeConnection($name) - { - $config = $this->getConfig($name); - - return $this->factory->make($config); - } - - /** - * Get the configuration for a named connection. - * - * @param $name - * @return mixed - */ - protected function getConfig($name) - { - $connections = $this->app['config']['elasticsearch.connections']; - - if (is_null($config = array_get($connections, $name))) { - throw new \InvalidArgumentException("Elasticsearch connection [$name] not configured."); - } - - return $config; - } - - /** - * Return all of the created connections. - * - * @return array - */ - public function getConnections() - { - return $this->connections; - } - - /** - * Dynamically pass methods to the default connection. - * - * @param string $method - * @param array $parameters - * @return mixed - */ - public function __call($method, $parameters) - { - return call_user_func_array([$this->connection(), $method], $parameters); - } -} diff --git a/src/LumenServiceProvider.php b/src/LumenServiceProvider.php deleted file mode 100644 index 2a7e2ac..0000000 --- a/src/LumenServiceProvider.php +++ /dev/null @@ -1,47 +0,0 @@ -app; - - $app->singleton('elasticsearch.factory', function($app) { - return new Factory(); - }); - - $app->singleton('elasticsearch', function($app) { - return new LumenManager($app, $app['elasticsearch.factory']); - }); - - $app->alias('elasticsearch', LumenManager::class); - - $this->withFacades(); - } - - protected function withFacades() - { - class_alias('\Cviebrock\LaravelElasticsearch\Facade', 'Elasticsearch'); - } -} diff --git a/src/Manager.php b/src/Manager.php index 5ebbdc9..690e542 100644 --- a/src/Manager.php +++ b/src/Manager.php @@ -1,5 +1,6 @@ getDefaultConnection(); @@ -66,7 +68,7 @@ public function connection($name = null) * * @return string */ - public function getDefaultConnection() + public function getDefaultConnection(): string { return $this->app['config']['elasticsearch.defaultConnection']; } @@ -76,7 +78,7 @@ public function getDefaultConnection() * * @param string $connection */ - public function setDefaultConnection($connection) + public function setDefaultConnection(string $connection) { $this->app['config']['elasticsearch.defaultConnection'] = $connection; } @@ -84,10 +86,11 @@ public function setDefaultConnection($connection) /** * Make a new connection. * - * @param $name - * @return \Elasticsearch\Client|mixed + * @param string $name + * + * @return \Elasticsearch\Client */ - protected function makeConnection($name) + protected function makeConnection(string $name): Client { $config = $this->getConfig($name); @@ -98,13 +101,15 @@ protected function makeConnection($name) * Get the configuration for a named connection. * * @param $name + * * @return mixed + * @throws \InvalidArgumentException */ - protected function getConfig($name) + protected function getConfig(string $name) { $connections = $this->app['config']['elasticsearch.connections']; - if (is_null($config = array_get($connections, $name))) { + if (null === $config = array_get($connections, $name)) { throw new \InvalidArgumentException("Elasticsearch connection [$name] not configured."); } @@ -116,7 +121,7 @@ protected function getConfig($name) * * @return array */ - public function getConnections() + public function getConnections(): array { return $this->connections; } @@ -126,9 +131,10 @@ public function getConnections() * * @param string $method * @param array $parameters + * * @return mixed */ - public function __call($method, $parameters) + public function __call(string $method, array $parameters) { return call_user_func_array([$this->connection(), $method], $parameters); } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 452e81c..3caa340 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,7 +1,9 @@ publishes([ - $configPath => config_path('elasticsearch.php'), - ]); + $this->setUpConfig(); } /** - * Register the service provider. + * Register the application services. * * @return void */ @@ -52,5 +42,27 @@ public function register() $app->singleton(Client::class, function($app) { return $app['elasticsearch']->connection(); }); + + if ($app instanceof LumenApplication) { + $this->withLumenFacades(); + } + } + + protected function setUpConfig() + { + $source = dirname(__DIR__) . '/config/elasticsearch.php'; + + if ($this->app instanceof LaravelApplication) { + $this->publishes([$source => config_path('elasticsearch.php')], 'config'); + } elseif ($this->app instanceof LumenApplication) { + $this->app->configure('elasticsearch'); + } + + $this->mergeConfigFrom($source, 'elasticsearch'); + } + + protected function withLumenFacades() + { + class_alias(Facade::class, 'Elasticsearch'); } }