Skip to content

Commit

Permalink
Fix support legacy php version with Symfony 4
Browse files Browse the repository at this point in the history
  • Loading branch information
vtsykun committed Aug 21, 2023
1 parent e868550 commit 2adaca7
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Clock/OkvpnLoopClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function sleep($seconds): void
}
}

public function withTimeZone(/*\DateTimeZone|string*/ $timezone): static
public function withTimeZone(/*\DateTimeZone|string*/ $timezone)
{
$clone = clone $this;
$clone->timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone;
Expand Down
5 changes: 3 additions & 2 deletions src/Command/CronCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Okvpn\Bundle\CronBundle\React\ReactLoopAdapter;
use Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface;
use Okvpn\Bundle\CronBundle\Runner\ScheduleRunnerInterface;
use Okvpn\Bundle\CronBundle\Utils\CronUtils;
use Psr\Clock\ClockInterface as PsrClockInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -135,7 +136,7 @@ protected function scheduler(InputInterface $input, OutputInterface $output): vo
$options['demand'] = $input->getOption('demand');
$options['dry-run'] = $input->getOption('dry-run');

$envStamp = new EnvironmentStamp($options + ['now' => new \DateTimeImmutable('@'.$roundTime, $now->getTimezone()), 'dispatch-loop' => null !== $this->dispatcher]);
$envStamp = new EnvironmentStamp($options + ['now' => CronUtils::toDate($roundTime, $now) , 'dispatch-loop' => null !== $this->dispatcher]);
$loggerStamp = $this->createLoggerStamp($output);

$this->dispatchLoopEvent(LoopEvent::LOOP_START);
Expand Down Expand Up @@ -165,7 +166,7 @@ protected function getCurrentDate(): \DateTimeImmutable
{
$now = $this->clock ? $this->clock->now() : new \DateTimeImmutable('now');
if (null !== $this->timezone) {
$now = new \DateTimeImmutable('@'.$now->format('U.u'), new \DateTimeZone($this->timezone));
$now = CronUtils::toDate($now, $this->timezone);
}

return $now;
Expand Down
3 changes: 2 additions & 1 deletion src/Cron/CronChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Okvpn\Bundle\CronBundle\Cron;

use Cron\CronExpression;
use Okvpn\Bundle\CronBundle\Utils\CronUtils;

class CronChecker
{
Expand Down Expand Up @@ -54,7 +55,7 @@ public static function isRandomNext(string $cron, $timeZone, $current): \DateTim
$current = \is_string($current) ? new \DateTime($current) : $current;
$next = $current->getTimestamp() + \random_int(0, $cron);

return new \DateTimeImmutable('@'.$next, \is_string($timeZone) ? new \DateTimeZone($timeZone) : $timeZone);
return CronUtils::toDate($next, $timeZone);
}

public static function isRandomDue(string $cron): bool
Expand Down
10 changes: 5 additions & 5 deletions src/Logger/CronConsoleLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class CronConsoleLogger extends AbstractLogger
public const INFO = 'info';
public const ERROR = 'error';

private OutputInterface $output;
private array $verbosityLevelMap = [
private $output;
private $verbosityLevelMap = [
LogLevel::EMERGENCY => OutputInterface::VERBOSITY_NORMAL,
LogLevel::ALERT => OutputInterface::VERBOSITY_NORMAL,
LogLevel::CRITICAL => OutputInterface::VERBOSITY_NORMAL,
Expand All @@ -38,7 +38,7 @@ class CronConsoleLogger extends AbstractLogger
LogLevel::INFO => OutputInterface::VERBOSITY_VERY_VERBOSE,
LogLevel::DEBUG => OutputInterface::VERBOSITY_DEBUG,
];
private array $formatLevelMap = [
private $formatLevelMap = [
LogLevel::EMERGENCY => self::ERROR,
LogLevel::ALERT => self::ERROR,
LogLevel::CRITICAL => self::ERROR,
Expand All @@ -48,7 +48,7 @@ class CronConsoleLogger extends AbstractLogger
LogLevel::INFO => self::INFO,
LogLevel::DEBUG => self::INFO,
];
private bool $errored = false;
private $errored = false;

public function __construct(OutputInterface $output, array $verbosityLevelMap = [], array $formatLevelMap = [])
{
Expand Down Expand Up @@ -106,7 +106,7 @@ private function interpolate(string $message, array $context): string
} elseif ($val instanceof \DateTimeInterface) {
$replacements["{{$key}}"] = $val->format(\DateTimeInterface::RFC3339);
} elseif (\is_object($val)) {
$replacements["{{$key}}"] = '[object '.$val::class.']';
$replacements["{{$key}}"] = '[object '.get_class($val).']';
} else {
$replacements["{{$key}}"] = '['.\gettype($val).']';
}
Expand Down
3 changes: 2 additions & 1 deletion src/Middleware/CronMiddlewareEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Okvpn\Bundle\CronBundle\Model\ScheduleStamp;
use Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface;
use Okvpn\Bundle\CronBundle\Runner\TimerStorage;
use Okvpn\Bundle\CronBundle\Utils\CronUtils;
use Psr\Clock\ClockInterface as PsrClockInterface;

final class CronMiddlewareEngine implements MiddlewareEngineInterface
Expand Down Expand Up @@ -133,7 +134,7 @@ private function handleNoDemand(\DateTimeInterface $now, ScheduleEnvelope $envel
}
} else {
$currentTime = (int)(60 * \floor($now->getTimestamp()/60));
$now = new \DateTimeImmutable('@'.($currentTime-1), $now->getTimezone());
$now = CronUtils::toDate(($currentTime-1), $now);

$nextRun = $stamp->getNextRunDate($now);
$nextRun = (int)(60 * \floor($nextRun->getTimestamp()/60));
Expand Down
4 changes: 3 additions & 1 deletion src/Model/JitterStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Okvpn\Bundle\CronBundle\Model;

use Okvpn\Bundle\CronBundle\Utils\CronUtils;

final class JitterStamp implements CommandStamp, PeriodicalStampInterface
{
private $stamp;
Expand All @@ -23,7 +25,7 @@ public function getNextRunDate(\DateTimeInterface $run): \DateTimeInterface
$nextRun = $this->stamp->getNextRunDate($run);
$nextRun = (float)$nextRun->format('U.u') + $this->maxSeconds * random_int(0, PHP_INT_MAX)/PHP_INT_MAX;

return new \DateTimeImmutable('@'. round($nextRun, 6), $run->getTimezone());
return CronUtils::toDate($nextRun, $run);
}

/**
Expand Down
12 changes: 7 additions & 5 deletions src/Model/PeriodicalScheduleStamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@

namespace Okvpn\Bundle\CronBundle\Model;

use Okvpn\Bundle\CronBundle\Utils\CronUtils;

class PeriodicalScheduleStamp implements CommandStamp, PeriodicalStampInterface
{
private $interval;
private $from;

public function __construct(
/* string|int|float|\DateInterval */ $interval,
/* string|int|float|\DateTimeImmutable */ $from = 0,
/* string|int|float|\DateTimeImmutable */ $from = 0
) {
$this->from = \is_string($from) && !\is_numeric($from) ? (new \DateTimeImmutable($from))->getTimestamp() : (int)$from;
$interval = \is_string($interval) && !\is_numeric($interval) ? (new \DateInterval($interval)) : $interval;
$interval = \is_string($interval) && !\is_numeric($interval) ? ('P' === ($interval[0] ?? '') ? new \DateInterval($interval) : \DateInterval::createFromDateString($interval)) : $interval;

if ($interval instanceof \DateInterval) {
$time1 = new \DateTime('now');
$interval = (float)(clone $time1)->add($interval)->format('U.u') - (float)$time1->format('U.u');
$time1 = new \DateTimeImmutable('now');
$interval = (float)$time1->add($interval)->format('U.u') - (float)$time1->format('U.u');
}

$this->interval = (float)$interval;
Expand All @@ -34,7 +36,7 @@ public function getNextRunDate(\DateTimeInterface $run): \DateTimeInterface

$delay = $this->interval - fmod($unix, $this->interval);

return new \DateTimeImmutable('@'.round($delay + $now, 6), $run->getTimezone());
return CronUtils::toDate($delay + $now, $run);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/React/ReactLoopAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Okvpn\Bundle\CronBundle\React;

use Okvpn\Bundle\CronBundle\Runner\ScheduleLoopInterface;
use Okvpn\Bundle\CronBundle\Utils\CronUtils;
use React\EventLoop\ExtEvLoop;
use React\EventLoop\Loop;
use React\EventLoop\LoopInterface;
Expand Down Expand Up @@ -102,7 +103,7 @@ public function now(): \DateTimeImmutable

$now = ($this->nowAccessor)();
if (null !== $this->timeZone) {
$now = new \DateTimeImmutable('@'.$now->format('U.u'), new \DateTimeZone($this->timeZone));
$now = CronUtils::toDate($now, $this->timeZone);
}

return $now;
Expand Down Expand Up @@ -133,7 +134,7 @@ private function createLoopAccessor(): \Closure
if ($this->loop instanceof ExtEvLoop && (new \ReflectionObject($this->loop))->hasProperty('loop')) {
return \Closure::bind(function (): \DateTimeImmutable {
$now = $this->loop->now();
return new \DateTimeImmutable('@'.round($now, 6));
return CronUtils::toDate($now);
}, $this->loop, ExtEvLoop::class);
}

Expand Down
23 changes: 23 additions & 0 deletions src/Utils/CronUtils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Okvpn\Bundle\CronBundle\Utils;

/**
* @internal
*/
final class CronUtils
{
public static function toDate(/*float|string|\DateTimeInterface */ $unix, /* \DateTimeInterface|\DateTimeZone|string|null */ $timezone): \DateTimeImmutable
{
$timezone = $timezone instanceof \DateTimeInterface ? $timezone->getTimezone() : $timezone;
$timezone = \is_string($timezone) ? new \DateTimeZone($timezone) : $timezone;

if ($unix instanceof \DateTimeInterface) {
$unix = $unix->format('U.u');
}

return \DateTimeImmutable::createFromFormat("U.u", sprintf("%.6f", $unix), $timezone);
}
}

0 comments on commit 2adaca7

Please sign in to comment.