From 1bdc10a306623c91e791f90a222febf007733849 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Wed, 22 Jul 2015 21:34:15 +0000 Subject: [PATCH 1/2] Add RandomBytesGenerator for use with PHP 7. --- src/Generator/RandomBytesGenerator.php | 24 +++++++++++++++++++ src/Generator/RandomGeneratorFactory.php | 22 +++++++++++++++++ .../Generator/RandomGeneratorFactoryTest.php | 1 + 3 files changed, 47 insertions(+) create mode 100644 src/Generator/RandomBytesGenerator.php diff --git a/src/Generator/RandomBytesGenerator.php b/src/Generator/RandomBytesGenerator.php new file mode 100644 index 0000000..aafe4a8 --- /dev/null +++ b/src/Generator/RandomBytesGenerator.php @@ -0,0 +1,24 @@ + + * @license http://opensource.org/licenses/MIT MIT + * @link https://benramsey.com/projects/ramsey-uuid/ Documentation + * @link https://packagist.org/packages/ramsey/uuid Packagist + * @link https://github.com/ramsey/uuid GitHub + */ + +namespace Ramsey\Uuid\Generator; + +class RandomBytesGenerator implements RandomGeneratorInterface +{ + + public function generate($length) + { + return random_bytes($length); + } +} diff --git a/src/Generator/RandomGeneratorFactory.php b/src/Generator/RandomGeneratorFactory.php index 3dd3883..519ce14 100644 --- a/src/Generator/RandomGeneratorFactory.php +++ b/src/Generator/RandomGeneratorFactory.php @@ -24,6 +24,14 @@ class RandomGeneratorFactory */ public static $forceNoOpensslRandomPseudoBytes = false; + /** + * For testing, random_bytes() override; if true, treat as if random_bytes() + * is not available. + * + * @var bool + */ + public static $forceNoRandomBytes = false; + /** * Returns true if the system has openssl_random_pseudo_bytes() * @@ -34,8 +42,22 @@ class RandomGeneratorFactory return (function_exists('openssl_random_pseudo_bytes') && !self::$forceNoOpensslRandomPseudoBytes); } + /** + * Returns true if the system has random_bytes() + * + * @return bool + */ + protected static function hasRandomBytes() + { + return (function_exists('random_bytes') && !self::$forceNoRandomBytes); + } + public static function getGenerator() { + if (self::hasRandomBytes()) { + return new RandomBytesGenerator(); + } + if (self::hasOpensslRandomPseudoBytes()) { return new OpenSslGenerator(); } diff --git a/tests/Generator/RandomGeneratorFactoryTest.php b/tests/Generator/RandomGeneratorFactoryTest.php index 580974d..9a3ceb7 100644 --- a/tests/Generator/RandomGeneratorFactoryTest.php +++ b/tests/Generator/RandomGeneratorFactoryTest.php @@ -8,6 +8,7 @@ class RandomGeneratorFactoryTest extends TestCase { public function testFactoryReturnsNonOpenSslGeneratorWithForceNoOpenSsl() { + RandomGeneratorFactory::$forceNoRandomBytes = true; RandomGeneratorFactory::$forceNoOpensslRandomPseudoBytes = true; $generator = RandomGeneratorFactory::getGenerator(); From 261eb8e3478a3037337c62bdda80af949c44e918 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Wed, 22 Jul 2015 16:47:56 -0500 Subject: [PATCH 2/2] Migrate to Travis CI's container-based infrastructure --- .travis.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index f66e768..899443e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,8 +7,12 @@ php: - 7.0 - hhvm -before_install: - - sudo apt-get update && sudo apt-get install uuid-dev +sudo: false + +addons: + apt: + packages: + - uuid-dev before_script: - travis_retry composer self-update