Skip to content

Commit

Permalink
implementation DB::$defaultOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
rekryt committed Jul 25, 2022
1 parent 2399963 commit eeee472
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ REDIS_HOST=redis
REDIS_PASSWORD=
REDIS_PORT=6379
REDIS_DB=0
MYSQL_HOST=mysql
MYSQL_PORT=3306
MYSQL_USER=mysql
MYSQL_PASSWORD=mysql
MYSQL_DB=db
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,33 @@ php index.php
```

### Example
```php
/**
* Set redis options
* @see https://github.com/promphp/prometheus_client_php#usage
*/
RedisStorage::setDefaultOptions([
'host' => 'redis',
'port' => 6379,
'password' => null,
'database' => 0,
'timeout' => 0.1, // in seconds
'read_timeout' => '10', // in seconds
'persistent_connections' => false
]);

/**
* Set mysql options
*/
DB::setDefaultOptions([
'host' => 'mysql',
'port' => '3306',
'user' => 'mysql',
'password' => 'mysql',
'db' => 'db'
]);
```

```php
// Example gauge from mysql
$gauge = $registry->getOrRegisterGauge('rpe', 'testTable', 'Value from mysql');
Expand Down
11 changes: 11 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@
'persistent_connections' => false,
]);

/**
* Set mysql options
*/
DB::setDefaultOptions([
'host' => $_ENV['MYSQL_HOST'] ? $_ENV['MYSQL_HOST'] : 'mysql',
'port' => $_ENV['MYSQL_PORT'] ? $_ENV['MYSQL_PORT'] : '3306',
'user' => $_ENV['MYSQL_USER'] ? $_ENV['MYSQL_USER'] : 'mysql',
'password' => $_ENV['MYSQL_PASSWORD'] ? $_ENV['MYSQL_PASSWORD'] : 'mysql',
'db' => $_ENV['MYSQL_DB'] ? $_ENV['MYSQL_DB'] : 'db',
]);

try {
$registry = new CollectorRegistry(new RedisStorage(), false);
} catch (\Exception $e) {
Expand Down
31 changes: 29 additions & 2 deletions src/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,42 @@ class DB {
*/
private Credentials $credentials;

/**
* Credentials options
* @var string[]
*/
private static array $defaultOptions = [
'host' => 'mysql',
'port' => '3306',
'user' => 'mysql',
'password' => 'mysql',
'db' => 'db',
];

/**
* @param mixed[] $options
*/
public static function setDefaultOptions(array $options): void {
self::$defaultOptions = array_merge(self::$defaultOptions, $options);
}

/**
* DB constructor
* @param array $options
*/
public function __construct() {
public function __construct($options = []) {
$loop = ReactAdapter::get();
self::setDefaultOptions($options);

$this->driver = new MysqlDriver($loop);
$this->platform = new MySqlPlatform();
$this->credentials = new Credentials('mysql', '3306', 'mysql', 'mysql', 'db');
$this->credentials = new Credentials(
self::$defaultOptions['host'],
self::$defaultOptions['port'],
self::$defaultOptions['user'],
self::$defaultOptions['password'],
self::$defaultOptions['db']
);

$this->conn = SingleConnection::createConnected($this->driver, $this->credentials, $this->platform);

Expand Down

0 comments on commit eeee472

Please sign in to comment.