welcome back to dyb-tech

This commit is contained in:
Daniel Guzman
2024-05-18 02:28:01 +02:00
parent 9513cdba09
commit 9f30bc98c7
6149 changed files with 668407 additions and 0 deletions
+13
View File
@@ -0,0 +1,13 @@
CHANGELOG
=========
6.3
---
* Add `ClockAwareTrait` to help write time-sensitive classes
* Add `Clock` class and `now()` function
6.2
---
* Add the component
+72
View File
@@ -0,0 +1,72 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
use Psr\Clock\ClockInterface as PsrClockInterface;
/**
* A global clock.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class Clock implements ClockInterface
{
private static ClockInterface $globalClock;
public function __construct(
private readonly ?PsrClockInterface $clock = null,
private ?\DateTimeZone $timezone = null,
) {
}
/**
* Returns the current global clock.
*
* Note that you should prefer injecting a ClockInterface or using
* ClockAwareTrait when possible instead of using this method.
*/
public static function get(): ClockInterface
{
return self::$globalClock ??= new NativeClock();
}
public static function set(PsrClockInterface $clock): void
{
self::$globalClock = $clock instanceof ClockInterface ? $clock : new self($clock);
}
public function now(): \DateTimeImmutable
{
$now = ($this->clock ?? self::get())->now();
return isset($this->timezone) ? $now->setTimezone($this->timezone) : $now;
}
public function sleep(float|int $seconds): void
{
$clock = $this->clock ?? self::get();
if ($clock instanceof ClockInterface) {
$clock->sleep($seconds);
} else {
(new NativeClock())->sleep($seconds);
}
}
public function withTimeZone(\DateTimeZone|string $timezone): static
{
$clone = clone $this;
$clone->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone;
return $clone;
}
}
+36
View File
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
use Psr\Clock\ClockInterface;
use Symfony\Contracts\Service\Attribute\Required;
/**
* A trait to help write time-sensitive classes.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait ClockAwareTrait
{
private readonly ClockInterface $clock;
#[Required]
public function setClock(ClockInterface $clock): void
{
$this->clock = $clock;
}
protected function now(): \DateTimeImmutable
{
return ($this->clock ??= new Clock())->now();
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
use Psr\Clock\ClockInterface as PsrClockInterface;
/**
* @author Nicolas Grekas <p@tchwork.com>
*/
interface ClockInterface extends PsrClockInterface
{
public function sleep(float|int $seconds): void;
public function withTimeZone(\DateTimeZone|string $timezone): static;
}
+19
View File
@@ -0,0 +1,19 @@
Copyright (c) 2022-present Fabien Potencier
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.
+73
View File
@@ -0,0 +1,73 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
/**
* A clock that always returns the same date, suitable for testing time-sensitive logic.
*
* Consider using ClockSensitiveTrait in your test cases instead of using this class directly.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class MockClock implements ClockInterface
{
private \DateTimeImmutable $now;
public function __construct(\DateTimeImmutable|string $now = 'now', \DateTimeZone|string|null $timezone = null)
{
if (\is_string($timezone)) {
$timezone = new \DateTimeZone($timezone);
}
if (\is_string($now)) {
$now = new \DateTimeImmutable($now, $timezone ?? new \DateTimeZone('UTC'));
}
$this->now = null !== $timezone ? $now->setTimezone($timezone) : $now;
}
public function now(): \DateTimeImmutable
{
return clone $this->now;
}
public function sleep(float|int $seconds): void
{
$now = (float) $this->now->format('Uu') + $seconds * 1e6;
$now = substr_replace(sprintf('@%07.0F', $now), '.', -6, 0);
$timezone = $this->now->getTimezone();
$this->now = (new \DateTimeImmutable($now, $timezone))->setTimezone($timezone);
}
public function modify(string $modifier): void
{
try {
$modifiedNow = @$this->now->modify($modifier);
} catch (\DateMalformedStringException) {
$modifiedNow = false;
}
if (false === $modifiedNow) {
throw new \InvalidArgumentException(sprintf('Invalid modifier: "%s". Could not modify MockClock.', $modifier));
}
$this->now = $modifiedNow;
}
public function withTimeZone(\DateTimeZone|string $timezone): static
{
$clone = clone $this;
$clone->now = $clone->now->setTimezone(\is_string($timezone) ? new \DateTimeZone($timezone) : $timezone);
return $clone;
}
}
+81
View File
@@ -0,0 +1,81 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
/**
* A monotonic clock suitable for performance profiling.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class MonotonicClock implements ClockInterface
{
private int $sOffset;
private int $usOffset;
private \DateTimeZone $timezone;
public function __construct(\DateTimeZone|string|null $timezone = null)
{
if (false === $offset = hrtime()) {
throw new \RuntimeException('hrtime() returned false: the runtime environment does not provide access to a monotonic timer.');
}
$time = explode(' ', microtime(), 2);
$this->sOffset = $time[1] - $offset[0];
$this->usOffset = (int) ($time[0] * 1000000) - (int) ($offset[1] / 1000);
if (\is_string($timezone ??= date_default_timezone_get())) {
$this->timezone = new \DateTimeZone($timezone);
} else {
$this->timezone = $timezone;
}
}
public function now(): \DateTimeImmutable
{
[$s, $us] = hrtime();
if (1000000 <= $us = (int) ($us / 1000) + $this->usOffset) {
++$s;
$us -= 1000000;
} elseif (0 > $us) {
--$s;
$us += 1000000;
}
if (6 !== \strlen($now = (string) $us)) {
$now = str_pad($now, 6, '0', \STR_PAD_LEFT);
}
$now = '@'.($s + $this->sOffset).'.'.$now;
return (new \DateTimeImmutable($now, $this->timezone))->setTimezone($this->timezone);
}
public function sleep(float|int $seconds): void
{
if (0 < $s = (int) $seconds) {
sleep($s);
}
if (0 < $us = $seconds - $s) {
usleep((int) ($us * 1E6));
}
}
public function withTimeZone(\DateTimeZone|string $timezone): static
{
$clone = clone $this;
$clone->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone;
return $clone;
}
}
+55
View File
@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
/**
* A clock that relies the system time.
*
* @author Nicolas Grekas <p@tchwork.com>
*/
final class NativeClock implements ClockInterface
{
private \DateTimeZone $timezone;
public function __construct(\DateTimeZone|string|null $timezone = null)
{
if (\is_string($timezone ??= date_default_timezone_get())) {
$this->timezone = new \DateTimeZone($timezone);
} else {
$this->timezone = $timezone;
}
}
public function now(): \DateTimeImmutable
{
return new \DateTimeImmutable('now', $this->timezone);
}
public function sleep(float|int $seconds): void
{
if (0 < $s = (int) $seconds) {
sleep($s);
}
if (0 < $us = $seconds - $s) {
usleep((int) ($us * 1E6));
}
}
public function withTimeZone(\DateTimeZone|string $timezone): static
{
$clone = clone $this;
$clone->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone;
return $clone;
}
}
+47
View File
@@ -0,0 +1,47 @@
Clock Component
===============
Symfony Clock decouples applications from the system clock.
Getting Started
---------------
```
$ composer require symfony/clock
```
```php
use Symfony\Component\Clock\NativeClock;
use Symfony\Component\Clock\ClockInterface;
class MyClockSensitiveClass
{
public function __construct(
private ClockInterface $clock,
) {
// Only if you need to force a timezone:
//$this->clock = $clock->withTimeZone('UTC');
}
public function doSomething()
{
$now = $this->clock->now();
// [...] do something with $now, which is a \DateTimeImmutable object
$this->clock->sleep(2.5); // Pause execution for 2.5 seconds
}
}
$clock = new NativeClock();
$service = new MyClockSensitiveClass($clock);
$service->doSomething();
```
Resources
---------
* [Documentation](https://symfony.com/doc/current/components/clock.html)
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
* [Report issues](https://github.com/symfony/symfony/issues) and
[send Pull Requests](https://github.com/symfony/symfony/pulls)
in the [main Symfony repository](https://github.com/symfony/symfony)
+25
View File
@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock;
if (!\function_exists(now::class)) {
/**
* Returns the current time as a DateTimeImmutable.
*
* Note that you should prefer injecting a ClockInterface or using
* ClockAwareTrait when possible instead of using this function.
*/
function now(): \DateTimeImmutable
{
return Clock::get()->now();
}
}
+71
View File
@@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Clock\Test;
use Psr\Clock\ClockInterface;
use Symfony\Component\Clock\Clock;
use Symfony\Component\Clock\MockClock;
use function Symfony\Component\Clock\now;
/**
* Helps with mocking the time in your test cases.
*
* This trait provides one self::mockTime() method that freezes the time.
* It restores the global clock after each test case.
* self::mockTime() accepts either a string (eg '+1 days' or '2022-12-22'),
* a DateTimeImmutable, or a boolean (to freeze/restore the global clock).
*
* @author Nicolas Grekas <p@tchwork.com>
*/
trait ClockSensitiveTrait
{
public static function mockTime(string|\DateTimeImmutable|bool $when = true): ClockInterface
{
Clock::set(match (true) {
false === $when => self::saveClockBeforeTest(false),
true === $when => new MockClock(),
$when instanceof \DateTimeImmutable => new MockClock($when),
default => new MockClock(now()->modify($when)),
});
return Clock::get();
}
/**
* @beforeClass
*
* @before
*
* @internal
*/
public static function saveClockBeforeTest(bool $save = true): ClockInterface
{
static $originalClock;
if ($save && $originalClock) {
self::restoreClockAfterTest();
}
return $save ? $originalClock = Clock::get() : $originalClock;
}
/**
* @after
*
* @internal
*/
protected static function restoreClockAfterTest(): void
{
Clock::set(self::saveClockBeforeTest(false));
}
}
+33
View File
@@ -0,0 +1,33 @@
{
"name": "symfony/clock",
"type": "library",
"description": "Decouples applications from the system clock",
"keywords": ["clock", "time", "psr20"],
"homepage": "https://symfony.com",
"license": "MIT",
"authors": [
{
"name": "Nicolas Grekas",
"email": "p@tchwork.com"
},
{
"name": "Symfony Community",
"homepage": "https://symfony.com/contributors"
}
],
"provide": {
"psr/clock-implementation": "1.0"
},
"require": {
"php": ">=8.1",
"psr/clock": "^1.0"
},
"autoload": {
"files": [ "Resources/now.php" ],
"psr-4": { "Symfony\\Component\\Clock\\": "" },
"exclude-from-classmap": [
"/Tests/"
]
},
"minimum-stability": "dev"
}