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

Commit 04f0a21

Browse files
committed
Added fix for php 7.1+
1 parent d1fc1da commit 04f0a21

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/Nats/Connection.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace Nats;
33

4+
use PhpSpec\CodeGenerator\Generator\GeneratorInterface;
45
use RandomLib\Factory;
56
use RandomLib\Generator;
67

@@ -115,7 +116,7 @@ public function getSubscriptions()
115116
private $streamSocket;
116117

117118
/**
118-
* @var Generator
119+
* @var Generator|Php71RandomGenerator
119120
*/
120121
private $randomGenerator;
121122

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

136141
if (is_null($options)) {
137142
$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)