From d0ffcb2d4a667ecbe142845a7aa7bca646150d0c Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Thu, 19 Jul 2012 17:02:19 -0500 Subject: [PATCH] Adding code to detect whether this is a 32-bit or 64-bit PHP build --- README.md | 13 +++++++++++ src/Rhumsaa/Uuid/Uuid.php | 7 ++++++ tests/Rhumsaa/Uuid/EnvironmentTest.php | 30 ++++++++++++++++++++++++++ tests/Rhumsaa/Uuid/UuidTest.php | 10 +++++++++ 4 files changed, 60 insertions(+) create mode 100644 tests/Rhumsaa/Uuid/EnvironmentTest.php diff --git a/README.md b/README.md index 33925c1..f5e906d 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,18 @@ A PHP 5.3+ library for generating and working with [RFC 4122][rfc4122] version Much inspiration for this library came from the [Java][javauuid] and [Python][pyuuid] UUID libraries. +## Requirements + +Rhumsaa\Uuid works on 64-bit builds of PHP 5.3.3+. + +Since, this library deals with large integers, so you will need to run it on a +64-bit system with a 64-bit compiled version of PHP. + +**Warning:** The [Windows binaries located on PHP.net][phpwin] are 32-bit +versions of PHP. Even if you run them on a 64-bit version of Windows, this +library will not work. You will need to compile PHP on Windows yourself to +build a 64-bit version. + ## Examples ```php @@ -51,4 +63,5 @@ and install the latest version of the Uuid library into your project: [rfc4122]: http://tools.ietf.org/html/rfc4122 [javauuid]: http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html [pyuuid]: http://docs.python.org/library/uuid.html +[phpwin]: http://windows.php.net/download/ [packagist]: http://packagist.org/ diff --git a/src/Rhumsaa/Uuid/Uuid.php b/src/Rhumsaa/Uuid/Uuid.php index 1565842..84815b3 100644 --- a/src/Rhumsaa/Uuid/Uuid.php +++ b/src/Rhumsaa/Uuid/Uuid.php @@ -97,6 +97,13 @@ final class Uuid */ protected function __construct($msb, $lsb) { + if (PHP_INT_SIZE == 4) { + throw new \OverflowException( + 'Attempting to create a UUID on a 32-bit build of PHP. This ' + . 'library requires a 64-bit build of PHP.' + ); + } + $this->msb = $msb; $this->lsb = $lsb; } diff --git a/tests/Rhumsaa/Uuid/EnvironmentTest.php b/tests/Rhumsaa/Uuid/EnvironmentTest.php new file mode 100644 index 0000000..f562b5d --- /dev/null +++ b/tests/Rhumsaa/Uuid/EnvironmentTest.php @@ -0,0 +1,30 @@ +setExpectedException( + 'OverflowException', + 'Attempting to create a UUID on a 32-bit build of PHP. This library requires a 64-bit build of PHP.' + ); + + $uuid = Uuid::uuid1(); + + } else { + + $this->markTestSkipped('This test is only applicable on a 32-bit system.'); + + } + } +} diff --git a/tests/Rhumsaa/Uuid/UuidTest.php b/tests/Rhumsaa/Uuid/UuidTest.php index 83e2e0d..3485573 100644 --- a/tests/Rhumsaa/Uuid/UuidTest.php +++ b/tests/Rhumsaa/Uuid/UuidTest.php @@ -3,6 +3,16 @@ namespace Rhumsaa\Uuid; class UuidTest extends \PHPUnit_Framework_TestCase { + protected function setUp() + { + // Skip these tests if run on a 32-bit build of PHP + if (PHP_INT_SIZE == 4) { + $this->markTestSkipped( + 'Running tests on a 32-bit build of PHP; 64-bit build required.' + ); + } + } + /** * @covers Rhumsaa\Uuid\Uuid::fromString * @covers Rhumsaa\Uuid\Uuid::__construct