Skip to content
This repository was archived by the owner on Oct 30, 2024. It is now read-only.

Commit f06e0a8

Browse files
authored
Merge pull request repejota#89 from byrnedo/feature/random-generator-php7
Fixes random id generator for php 7.1+
2 parents cba653f + c708832 commit f06e0a8

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ php:
66
- 5.5
77
- 5.6
88
- 7
9+
- 7.1
910
cache:
1011
directories:
1112
- vendor

src/Nats/Connection.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public function getSubscriptions()
115115
private $streamSocket;
116116

117117
/**
118-
* @var Generator
118+
* @var Generator|Php71RandomGenerator
119119
*/
120120
private $randomGenerator;
121121

@@ -130,8 +130,12 @@ public function __construct(ConnectionOptions $options = null)
130130
$this->pubs = 0;
131131
$this->subscriptions = [];
132132
$this->options = $options;
133-
$randomFactory = new Factory();
134-
$this->randomGenerator = $randomFactory->getLowStrengthGenerator();
133+
if(version_compare(phpversion(), '7.0', '>')){
134+
$this->randomGenerator = new Php71RandomGenerator();
135+
} else {
136+
$randomFactory = new Factory();
137+
$this->randomGenerator = $randomFactory->getLowStrengthGenerator();
138+
}
135139

136140
if (is_null($options)) {
137141
$this->options = new ConnectionOptions();

src/Nats/Php71RandomGenerator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
4+
namespace Nats;
5+
6+
7+
8+
class Php71RandomGenerator
9+
{
10+
/**
11+
* A simple wrapper on random_bytes
12+
*
13+
* @param $len
14+
* @return string
15+
*/
16+
public function generateString($len) {
17+
return bin2hex(random_bytes($len));
18+
}
19+
}

0 commit comments

Comments
 (0)