Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ada-u committed Dec 14, 2014
0 parents commit 391d0bf
Show file tree
Hide file tree
Showing 16 changed files with 747 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
src_dir: src
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
service_name: travis-ci
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/vendor
composer.phar
composer.lock
*.iml
.idea
.env.*.php
.env.php
.DS_Store
.vagrant
Thumbs.db
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: php

php:
- 5.4
- 5.5
- 5.6

matrix:
allow_failures:
- php: 5.6

before_script:
- composer self-update
- composer install

script:
- mkdir -p build/logs
- phpunit --coverage-clover build/logs/clover.xml -c test/

after_script:
- php vendor/bin/coveralls -v
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2014 ada-u

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# PHP IdGen

### 64bit ID Generator for PHP

`PHP IdGen` is an implementation of twitter Snowflake concept. This provides generating IDs based on time in a distributed environment.

### ID Specification

The IDs consist of four elements:

- timestamp
- region id
- server id
- sequence

You can specify any bit length to each element.

## Usage

### Prerequisites

- PHP 5.4 or later

### Installation

### Sample

```php

// example:
// 41 bit for timestamp
// 5 bit for region id
// 5 bit for server id
// 12 bit for sequence per milliseconds
// 1414334507356 - service start epoch (unix timestamp)
$config = new IdValueConfig(41, 5, 5, 12, 1414334507356);

$service = new IdGenService($config);

$worker = $service->createIdWorker(new RegionId(1), new ServerId(1));

$id = $worker->generate();
// string(10) "4194439168"

```
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "ada-u/php-idgen",
"type": "library",
"description": "Id Generator for PHP",
"keywords": [ "php-idgen", "id", "generator" ],
"homepage": "https://github.com/ada-u/php-idgen",
"authors": [
{ "name": "ada-u", "email": "[email protected]" }
],
"license": "MIT",
"require-dev": {
"phpunit/phpunit": "4.4.*",
"mockery/mockery": "0.9.*",
"satooshi/php-coveralls": "dev-master"
},
"require": {
"php": ">=5.4.0"
},
"autoload": {
"psr-4": {
"Adachi\\IdGen\\": "src/IdGen/"
}
}
}
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8" ?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.github.com/sebastianbergmann/phpunit/master/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
>
<testsuites>
<testsuite name="tests">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="../build/logs/clover.xml"/>
</logging>
</phpunit>
41 changes: 41 additions & 0 deletions src/IdGen/Foundation/IdValue/Element/RegionId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Adachi\IdGen\Foundation\IdValue\Element;

/**
* Class RegionId
*
* @package Adachi\IdGen\Foundation\IdValue\Element
*/
class RegionId
{
/**
* @var int
*/
public $value;

/**
* @param int $value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* @param RegionId $target
* @return bool
*/
public function equals(RegionId $target)
{
return $this->value === $target->value;
}

/**
* @return string
*/
public function __toString()
{
return (String) $this->value;
}
}
41 changes: 41 additions & 0 deletions src/IdGen/Foundation/IdValue/Element/ServerId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Adachi\IdGen\Foundation\IdValue\Element;

/**
* Class ServerId
*
* @package Adachi\IdGen\Foundation\IdValue\Element
*/
class ServerId
{
/**
* @var int
*/
public $value;

/**
* @param int $value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* @param ServerId $target
* @return bool
*/
public function equals(ServerId $target)
{
return $this->value === $target->value;
}

/**
* @return string
*/
public function __toString()
{
return (String) $this->value;
}
}
41 changes: 41 additions & 0 deletions src/IdGen/Foundation/IdValue/Element/Timestamp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Adachi\IdGen\Foundation\IdValue\Element;

/**
* Class Timestamp
*
* @package Adachi\IdGen\Foundation\IdValue\Element
*/
class Timestamp
{
/**
* @var int
*/
public $value;

/**
* @param int $value
*/
public function __construct($value)
{
$this->value = $value;
}

/**
* @param Timestamp $target
* @return bool
*/
public function equals(Timestamp $target)
{
return $this->value === $target->value;
}

/**
* @return string
*/
public function __toString()
{
return (String) $this->value;
}
}
81 changes: 81 additions & 0 deletions src/IdGen/Foundation/IdValue/IdValue.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Adachi\IdGen\Foundation\IdValue;

use Adachi\IdGen\Foundation\IdValue\Element\ServerId;
use Adachi\IdGen\Foundation\IdValue\Element\Timestamp;
use Adachi\IdGen\Foundation\IdValue\Element\RegionId;

/**
* Class IdValue
*
* @package Adachi\IdGen\Foundation\IdValue
*/
class IdValue
{

/**
* @var \Adachi\IdGen\Foundation\IdValue\Element\Timestamp
*/
public $timestamp;

/**
* @var \Adachi\IdGen\Foundation\IdValue\Element\RegionId
*/
public $regionId;

/**
* @var \Adachi\IdGen\Foundation\IdValue\Element\ServerId
*/
public $serverId;

/**
* @var int
*/
public $sequence;

/**
* @var int
*/
protected $value;

/**
* @param Timestamp $timestamp
* @param RegionId $regionId
* @param ServerId $serverId
* @param int $sequence
* @param int $value
*/
public function __construct(Timestamp $timestamp, RegionId $regionId, ServerId $serverId, $sequence, $value)
{
$this->timestamp = $timestamp;
$this->regionId = $regionId;
$this->serverId = $serverId;
$this->sequence = $sequence;
$this->value = $value;
}

/**
* @return int
*/
public function toInt()
{
return $this->value;
}

/**
* @return string
*/
public function asString()
{
return (string) $this->value;
}

/**
* @return string
*/
public function __toString()
{
return $this->asString();
}
}
Loading

0 comments on commit 391d0bf

Please sign in to comment.