Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Carbon 3 #11348

Open
wants to merge 8 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"league/glide": "^2.3",
"maennchen/zipstream-php": "^3.1",
"michelf/php-smartypants": "^1.8.1",
"nesbot/carbon": "^2.62.1",
"nesbot/carbon": "^2.62.1 || ^3.0",
"pixelfear/composer-dist-plugin": "^0.1.4",
"rebing/graphql-laravel": "^9.7",
"rhukster/dom-sanitizer": "^1.0.6",
Expand Down
32 changes: 28 additions & 4 deletions src/Http/Middleware/Localize.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Carbon\Carbon;
use Closure;
use Illuminate\Support\Facades\Date;
use ReflectionClass;
use Statamic\Facades\Site;
use Statamic\Statamic;
use Statamic\Support\Arr;

class Localize
{
Expand All @@ -29,10 +31,7 @@ public function handle($request, Closure $next)
app()->setLocale($site->lang());

// Get original Carbon format so it can be restored later.
// There's no getter for it, so we'll use reflection.
$format = (new \ReflectionClass(Carbon::class))->getProperty('toStringFormat');
$format->setAccessible(true);
$originalToStringFormat = $format->getValue();
$originalToStringFormat = $this->getToStringFormat();
Date::setToStringFormat(Statamic::dateFormat());

$response = $next($request);
Expand All @@ -45,4 +44,29 @@ public function handle($request, Closure $next)

return $response;
}

/**
* This method is used to get the current toStringFormat for Carbon, in order for us
* to restore it later. There's no getter for it, so we need to use reflection.
*
* @throws \ReflectionException
*/
private function getToStringFormat(): ?string
{
$reflection = new ReflectionClass($date = Date::now());

// Carbon 2.x
if ($reflection->hasProperty('toStringFormat')) {
$format = $reflection->getProperty('toStringFormat');
$format->setAccessible(true);

return $format->getValue();
}

// Carbon 3.x
$factory = $reflection->getMethod('getFactory');
$factory->setAccessible(true);

return Arr::get($factory->invoke($date)->getSettings(), 'toStringFormat');
}
}
2 changes: 1 addition & 1 deletion src/Licensing/LicenseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function requestRateLimited()
public function failedRequestRetrySeconds()
{
return $this->requestRateLimited()
? Carbon::createFromTimestamp($this->response('expiry'))->diffInSeconds()
? (int) Carbon::createFromTimestamp($this->response('expiry'))->diffInSeconds(absolute: true)
: null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Licensing/Outpost.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ private function cacheAndReturnValidationResponse($e)

private function cacheAndReturnRateLimitResponse($e)
{
$seconds = $e->getResponse()->getHeader('Retry-After')[0];
$seconds = (int) $e->getResponse()->getHeader('Retry-After')[0];

return $this->cacheResponse(now()->addSeconds($seconds), ['error' => 429]);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Modifiers/CoreModifiers.php
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ public function dashify($value)
*/
public function daysAgo($value, $params)
{
return $this->carbon($value)->diffInDays(Arr::get($params, 0));
return (int) $this->carbon($value)->diffInDays(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -1055,7 +1055,7 @@ public function hexToRgb($value)
*/
public function hoursAgo($value, $params)
{
return $this->carbon($value)->diffInHours(Arr::get($params, 0));
return (int) $this->carbon($value)->diffInHours(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -1652,7 +1652,7 @@ public function modifyDate($value, $params)
*/
public function monthsAgo($value, $params)
{
return $this->carbon($value)->diffInMonths(Arr::get($params, 0));
return (int) $this->carbon($value)->diffInMonths(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -2234,7 +2234,7 @@ public function segment($value, $params, $context)
*/
public function secondsAgo($value, $params)
{
return $this->carbon($value)->diffInSeconds(Arr::get($params, 0));
return (int) $this->carbon($value)->diffInSeconds(Arr::get($params, 0));
}

/**
Expand Down Expand Up @@ -2933,7 +2933,7 @@ public function values($value)
*/
public function weeksAgo($value, $params)
{
return $this->carbon($value)->diffInWeeks(Arr::get($params, 0));
return (int) $this->carbon($value)->diffInWeeks(Arr::get($params, 0));
}

/**
Expand Down
14 changes: 13 additions & 1 deletion tests/Feature/GraphQL/Fieldtypes/DateFieldtypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Illuminate\Support\Carbon;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test;
use ReflectionClass;
use Statamic\Support\Arr;

#[Group('graphql')]
class DateFieldtypeTest extends FieldtypeTestCase
Expand All @@ -14,7 +16,17 @@ public function setUp(): void
parent::setUp();

Carbon::macro('getToStringFormat', function () {
return static::$toStringFormat;
// Carbon 2.x
if (property_exists(static::this(), 'toStringFormat')) {
return static::$toStringFormat;
}

// Carbon 3.x
$reflection = new ReflectionClass(self::this());
$factory = $reflection->getMethod('getFactory');
$factory->setAccessible(true);

return Arr::get($factory->invoke(self::this())->getSettings(), 'toStringFormat');
});
}

Expand Down
Loading