1. Go to this page and download the library: Download godruoyi/php-snowflake library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
godruoyi / php-snowflake example snippets
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->id();
// 1537200202186752
$snowflake = new \Godruoyi\Snowflake\Snowflake($datacenterId, $workerId);
$snowflake->id();
$snowflake = new \Godruoyi\Snowflake\Snowflake;
$snowflake->setStartTimeStamp(strtotime('2019-09-09')*1000); // millisecond
$snowflake->id();
$sonyflake = new \Godruoyi\Snowflake\Sonyflake;
$sonyflake->id();
// App\Providers\AppServiceProvider
use Godruoyi\Snowflake\Snowflake;
use Godruoyi\Snowflake\LaravelSequenceResolver;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('snowflake', function ($app) {
return (new Snowflake())
->setStartTimeStamp(strtotime('2019-10-10')*1000)
->setSequenceResolver(new LaravelSequenceResolver($app->get('cache.store')));
});
}
}
class YourSequence implements SequenceResolver
{
/**
* {@inheritdoc}
*/
public function sequence(int $currentMillisecond)
{
// Just test.
return mt_rand(0, 1);
}
}
// usage
$snowflake->setSequenceResolver(new YourSequence);
$snowflake->id();