-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from infocyph/feature/update
Feature/update
- Loading branch information
Showing
6 changed files
with
103 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Infocyph\UID\Exceptions; | ||
|
||
class FileLockException extends \Exception | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Infocyph\UID; | ||
|
||
use DateTimeInterface; | ||
use Infocyph\UID\Exceptions\FileLockException; | ||
|
||
trait GetSequence | ||
{ | ||
private static string $fileLocation; | ||
|
||
/** | ||
* Generates a sequence number based on the current time. | ||
* | ||
* @param DateTimeInterface $dateTime The current time. | ||
* @param string $machineId The machine ID. | ||
* @return int The generated sequence number. | ||
* @throws FileLockException | ||
*/ | ||
private static function sequence(DateTimeInterface $dateTime, string $machineId, string $type): int | ||
{ | ||
self::$fileLocation = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "uid-$type-$machineId.seq"; | ||
if (!file_exists(self::$fileLocation)) { | ||
touch(self::$fileLocation); | ||
} | ||
$handle = fopen(self::$fileLocation, "r+"); | ||
if (!flock($handle, LOCK_EX)) { | ||
throw new FileLockException('Could not acquire lock on ' . self::$fileLocation); | ||
} | ||
$now = $dateTime->format('Uv'); | ||
$line = fgetcsv($handle); | ||
$sequence = 0; | ||
if ($line) { | ||
$sequence = match ($line[0]) { | ||
$now => $line[1], | ||
default => $sequence | ||
}; | ||
} | ||
ftruncate($handle, 0); | ||
rewind($handle); | ||
fputcsv($handle, [$now, ++$sequence]); | ||
flock($handle, LOCK_UN); | ||
fclose($handle); | ||
return $sequence; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.