Skip to content

Commit

Permalink
Merge pull request #31 from cxbdasheng/master
Browse files Browse the repository at this point in the history
Support Lumen
  • Loading branch information
1giba authored Feb 17, 2022
2 parents 6832474 + 8d727dc commit f598992
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 20 deletions.
49 changes: 29 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,30 @@ composer require tray-labs/laravel-influxdb
"tray-labs/laravel-influxdb": "^1.0"
}
```
## Register service provider(pick one of two).


## This package use auto-discover, if using less than version laravel 5.5 you must use below settings

* Add this lines to yours config/app.php (Use only with Laravel version less than 5.5 )

```php
'providers' => [
// ...
- `Laravel`: in `config/app.php` file, `Laravel 5.5+ supports package discovery automatically, you should skip this step`
```php
'providers' => [
// ...
TrayLabs\InfluxDB\Providers\ServiceProvider::class,
]
```
]
```
```php
'aliases' => [
// ...
'InfluxDB' => TrayLabs\InfluxDB\Facades\InfluxDB::class,
]
```
- `Lumen`: in `bootstrap/app.php` file
```php
// config
$app->configure('InfluxDB');

$app->register(TrayLabs\InfluxDB\Providers\LumenServiceProvider::class);
$app->alias('InfluxDB', TrayLabs\InfluxDB\Facades\InfluxDB::class);
```

```php
'aliases' => [
// ...
'InfluxDB' => TrayLabs\InfluxDB\Facades\InfluxDB::class,
]
```

* Define env variables to connect to InfluxDB

Expand All @@ -53,10 +58,14 @@ INFLUXDB_UDP_PORT=4444 # Port for UDP
```

* Write this into your terminal inside your project

```ini
php artisan vendor:publish
```
- `Laravel`
```ini
php artisan vendor:publish
```
- `Lumen`
```ini
cp vendor/TrayLabs/lumen-influxdb/config/InfluxDB.php config/InfluxDB.php
```

## Reading Data

Expand Down
72 changes: 72 additions & 0 deletions src/Providers/LumenServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* @author ChenDasheng
* @created 2022/2/10 22:27
*/

namespace TrayLabs\InfluxDB\Providers;

use Illuminate\Support\ServiceProvider;
use InfluxDB\Client as InfluxClient;
use InfluxDB\Database as InfluxDB;
use InfluxDB\Driver\UDP;

class LumenServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = true;

/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$path = realpath(__DIR__ . '/../../config/InfluxDB.php');
$this->mergeConfigFrom($path, 'influxdb');
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->singleton(InfluxDB::class, function ($app) {
$client = new InfluxClient(
config('influxdb.host'),
config('influxdb.port'),
config('influxdb.username'),
config('influxdb.password'),
config('influxdb.ssl'),
config('influxdb.verifySSL'),
config('influxdb.timeout')
);
if (config('influxdb.udp.enabled') === true) {
$client->setDriver(new UDP(
$client->getHost(),
config('influxdb.udp.port')
));
}
return $client->selectDB(config('influxdb.dbname'));
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
InfluxDB::class,
];
}
}

0 comments on commit f598992

Please sign in to comment.