smrealms /
smr
We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| 1 | <?php declare(strict_types=1); |
||||
| 2 | |||||
| 3 | namespace Smr; |
||||
| 4 | |||||
| 5 | use Exception; |
||||
| 6 | use Smr\Container\DiContainer; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * Stores the current time as a fixed value. |
||||
| 10 | * This is useful for ensuring that times are used consistently within the |
||||
| 11 | * context of an individual page request. It does not, however, represent |
||||
| 12 | * the start time of the request. |
||||
| 13 | * |
||||
| 14 | * The static methods are a convenience wrapper around the instance of this |
||||
| 15 | * class stored in the DI container (which is set on the first query, and |
||||
| 16 | * then remains unchanged in subsequent calls). |
||||
| 17 | */ |
||||
| 18 | class Epoch { |
||||
| 19 | |||||
| 20 | private readonly float $microtime; |
||||
| 21 | private readonly int $time; |
||||
| 22 | |||||
| 23 | public function __construct() { |
||||
| 24 | $this->microtime = microtime(true); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 25 | $this->time = IFloor($this->microtime); |
||||
|
0 ignored issues
–
show
The function
IFloor was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 26 | } |
||||
| 27 | |||||
| 28 | public function getMicrotime(): float { |
||||
| 29 | return $this->microtime; |
||||
| 30 | } |
||||
| 31 | |||||
| 32 | public function getTime(): int { |
||||
| 33 | return $this->time; |
||||
| 34 | } |
||||
| 35 | |||||
| 36 | /** |
||||
| 37 | * Returns the instance of this class from the DI container. |
||||
| 38 | * The first time this is called, it will populate the DI container, |
||||
| 39 | * and this will be the time associated with the page request. |
||||
| 40 | */ |
||||
| 41 | private static function getInstance(): self { |
||||
| 42 | return DiContainer::get(self::class); |
||||
| 43 | } |
||||
| 44 | |||||
| 45 | /** |
||||
| 46 | * Return the time (in seconds, with microsecond-level precision) |
||||
| 47 | * associated with a page request (i.e. stored in the DI container). |
||||
| 48 | */ |
||||
| 49 | public static function microtime(): float { |
||||
| 50 | return self::getInstance()->getMicrotime(); |
||||
| 51 | } |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * Return the time (in seconds) associated with a page request |
||||
| 55 | * (i.e. stored in the DI container). |
||||
| 56 | */ |
||||
| 57 | public static function time(): int { |
||||
| 58 | return self::getInstance()->getTime(); |
||||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Update the time associated with this page request |
||||
| 63 | * (i.e. stored in the DI container). |
||||
| 64 | * |
||||
| 65 | * NOTE: This should never be called by normal page requests, and should |
||||
| 66 | * only be used by the CLI programs that run continuously. |
||||
| 67 | */ |
||||
| 68 | public static function update(): void { |
||||
| 69 | if (DiContainer::get('NPC_SCRIPT') === false) { |
||||
| 70 | throw new Exception('Only call this function from CLI programs!'); |
||||
| 71 | } |
||||
| 72 | DiContainer::getContainer()->set(self::class, new self()); |
||||
| 73 | } |
||||
| 74 | |||||
| 75 | } |
||||
| 76 |