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.8] Upgrade Carbon to version 2 and allow CarbonImmutable #25310

Closed
wants to merge 2 commits into from
Closed
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 @@ -23,7 +23,7 @@
"erusev/parsedown": "^1.7",
"league/flysystem": "^1.0.8",
"monolog/monolog": "^1.12",
"nesbot/carbon": "^1.26.3",
"nesbot/carbon": "^2.0",
"psr/container": "^1.0",
"psr/simple-cache": "^1.0",
"ramsey/uuid": "^3.7",
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Auth/Notifications/VerifyEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Auth\Notifications;

use Illuminate\Support\Carbon;
use Carbon\Factory;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Notifications\Notification;
Expand Down Expand Up @@ -59,7 +59,7 @@ public function toMail($notifiable)
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute(
'verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]
'verification.verify', app(Factory::class)->now()->addMinutes(60), ['id' => $notifiable->getKey()]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just going to create a "60 minutes datetime from now", a plain Carbon::now()->addMinutes(60) should work fine.

);
}

Expand Down
12 changes: 8 additions & 4 deletions src/Illuminate/Auth/Passwords/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Illuminate\Auth\Passwords;

use Carbon\Factory;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Hashing\Hasher as HasherContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
Expand Down Expand Up @@ -107,7 +107,11 @@ protected function deleteExisting(CanResetPasswordContract $user)
*/
protected function getPayload($email, $token)
{
return ['email' => $email, 'token' => $this->hasher->make($token), 'created_at' => new Carbon];
return [
'email' => $email,
'token' => $this->hasher->make($token),
'created_at' => app(Factory::class)->now(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just used to store "now" as created_at for reset_passwords, Carbon::now() should be more than enough.

];
}

/**
Expand Down Expand Up @@ -136,7 +140,7 @@ public function exists(CanResetPasswordContract $user, $token)
*/
protected function tokenExpired($createdAt)
{
return Carbon::parse($createdAt)->addSeconds($this->expires)->isPast();
return app(Factory::class)->parse($createdAt)->addSeconds($this->expires)->isPast();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This just to check if the token has expired. Why do we need to make it complicated?

}

/**
Expand All @@ -157,7 +161,7 @@ public function delete(CanResetPasswordContract $user)
*/
public function deleteExpired()
{
$expiredAt = Carbon::now()->subSeconds($this->expires);
$expiredAt = app(Factory::class)->now()->subSeconds($this->expires);

$this->getTable()->where('created_at', '<', $expiredAt)->delete();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auth component doesn't load app() which is under Illuminate\Foundation, nor does it require illuminate/container.

}
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Cache/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use Closure;
use ArrayAccess;
use Carbon\Factory;
use DateTimeInterface;
use BadMethodCallException;
use Illuminate\Support\Carbon;
use Illuminate\Cache\Events\CacheHit;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Cache\Events\KeyWritten;
Expand Down Expand Up @@ -554,7 +554,8 @@ protected function getMinutes($duration)
$duration = $this->parseDateInterval($duration);

if ($duration instanceof DateTimeInterface) {
$duration = Carbon::now()->diffInSeconds(Carbon::createFromTimestamp($duration->getTimestamp()), false) / 60;
$dateFactory = app(Factory::class);
$duration = $dateFactory->now()->diffInSeconds($dateFactory->createFromTimestamp($duration->getTimestamp()), false) / 60;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache component doesn't load app() which is under Illuminate\Foundation, nor does it require illuminate/container.

}

return (int) ($duration * 60) > 0 ? $duration : null;
Expand Down
11 changes: 7 additions & 4 deletions src/Illuminate/Console/Scheduling/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Illuminate\Console\Scheduling;

use Closure;
use Carbon\Carbon;
use Carbon\Factory;
use Cron\CronExpression;
use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use GuzzleHttp\Client as HttpClient;
use Illuminate\Contracts\Mail\Mailer;
use Symfony\Component\Process\Process;
Expand Down Expand Up @@ -295,7 +297,8 @@ public function runsInMaintenanceMode()
*/
protected function expressionPasses()
{
$date = Carbon::now();
/** @var Carbon|CarbonImmutable $date */
$date = app(Factory::class)->now();

if ($this->timezone) {
$date->setTimezone($this->timezone);
Expand Down Expand Up @@ -687,11 +690,11 @@ public function getSummaryForDisplay()
* @param \DateTime|string $currentTime
* @param int $nth
* @param bool $allowCurrentDate
* @return \Illuminate\Support\Carbon
* @return \Carbon\Carbon
*/
public function nextRunDate($currentTime = 'now', $nth = 0, $allowCurrentDate = false)
{
return Carbon::instance(CronExpression::factory(
return app(Factory::class)->instance(CronExpression::factory(
$this->getExpression()
)->getNextRunDate($currentTime, $nth, $allowCurrentDate, $this->timezone));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Console component doesn't load app() which is under Illuminate\Foundation, nor does it require illuminate/container.

}
Expand Down
10 changes: 6 additions & 4 deletions src/Illuminate/Console/Scheduling/ManagesFrequencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Console\Scheduling;

use Illuminate\Support\Carbon;
use Carbon\Factory;

trait ManagesFrequencies
{
Expand Down Expand Up @@ -53,9 +53,11 @@ public function unlessBetween($startTime, $endTime)
private function inTimeInterval($startTime, $endTime)
{
return function () use ($startTime, $endTime) {
return Carbon::now($this->timezone)->between(
Carbon::parse($startTime, $this->timezone),
Carbon::parse($endTime, $this->timezone),
$dateFactory = app(Factory::class);

return $dateFactory->now($this->timezone)->between(
$dateFactory->parse($startTime, $this->timezone),
$dateFactory->parse($endTime, $this->timezone),
true
);
};
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Console/Scheduling/ScheduleRunCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Console\Scheduling;

use Illuminate\Support\Carbon;
use Carbon\Factory;
use Illuminate\Console\Command;

class ScheduleRunCommand extends Command
Expand Down Expand Up @@ -31,7 +31,7 @@ class ScheduleRunCommand extends Command
/**
* The 24 hour timestamp this scheduler command started running.
*
* @var \Illuminate\Support\Carbon;
* @var \Carbon\Carbon;
*/
protected $startedAt;

Expand All @@ -52,7 +52,7 @@ public function __construct(Schedule $schedule)
{
$this->schedule = $schedule;

$this->startedAt = Carbon::now();
$this->startedAt = app(Factory::class)->now();

parent::__construct();
}
Expand Down
19 changes: 11 additions & 8 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

namespace Illuminate\Database\Eloquent\Concerns;

use Carbon\Factory;
use LogicException;
use DateTimeInterface;
use Carbon\CarbonInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection as BaseCollection;
Expand Down Expand Up @@ -716,7 +717,7 @@ public function fromJson($value, $asObject = false)
* Return a timestamp as DateTime object with time set to 00:00:00.
*
* @param mixed $value
* @return \Illuminate\Support\Carbon
* @return \Carbon\Carbon
*/
protected function asDate($value)
{
Expand All @@ -727,22 +728,24 @@ protected function asDate($value)
* Return a timestamp as DateTime object.
*
* @param mixed $value
* @return \Illuminate\Support\Carbon
* @return \Carbon\Carbon
*/
protected function asDateTime($value)
{
// If this value is already a Carbon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Carbon instance when we know
// it already is one, which wouldn't be fulfilled by the DateTime check.
if ($value instanceof Carbon) {
if ($value instanceof CarbonInterface) {
return $value;
}

$dateFactory = app(Factory::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Database component doesn't load app() which is under Illuminate\Foundation.


// If the value is already a DateTime instance, we will just skip the rest of
// these checks since they will be a waste of time, and hinder performance
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
return new Carbon(
return $dateFactory->parse(
$value->format('Y-m-d H:i:s.u'), $value->getTimezone()
);
}
Expand All @@ -751,20 +754,20 @@ protected function asDateTime($value)
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value)) {
return Carbon::createFromTimestamp($value);
return $dateFactory->createFromTimestamp($value);
}

// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
if ($this->isStandardDateFormat($value)) {
return Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
return $dateFactory->createFromFormat('Y-m-d', $value)->startOfDay();
}

// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
return Carbon::createFromFormat(
return $dateFactory->createFromFormat(
str_replace('.v', '.u', $this->getDateFormat()), $value
);
}
Expand Down
6 changes: 3 additions & 3 deletions src/Illuminate/Database/Eloquent/Concerns/HasTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Database\Eloquent\Concerns;

use Illuminate\Support\Carbon;
use Carbon\Factory;

trait HasTimestamps
{
Expand Down Expand Up @@ -77,11 +77,11 @@ public function setUpdatedAt($value)
/**
* Get a fresh timestamp for the model.
*
* @return \Illuminate\Support\Carbon
* @return \Carbon\Carbon
*/
public function freshTimestamp()
{
return new Carbon;
return app(Factory::class)->now();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Filesystem/FilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Illuminate\Filesystem;

use Carbon\Factory;
use RuntimeException;
use Illuminate\Http\File;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Illuminate\Support\Carbon;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Collection;
use League\Flysystem\AdapterInterface;
Expand Down Expand Up @@ -548,7 +548,7 @@ public function getAwsTemporaryUrl($adapter, $path, $expiration, $options)
public function getRackspaceTemporaryUrl($adapter, $path, $expiration, $options)
{
return $adapter->getContainer()->getObject($path)->getTemporaryUrl(
Carbon::now()->diffInSeconds($expiration),
app(Factory::class)->now()->diffInSeconds($expiration),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filesystem component doesn't load app() which is under Illuminate\Foundation, nor does it require illuminate/container.

$options['method'] ?? 'GET',
$options['forcePublicUrl'] ?? true
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
namespace Illuminate\Foundation\Http\Exceptions;

use Exception;
use Illuminate\Support\Carbon;
use Carbon\Carbon;
use Carbon\Factory;
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;

class MaintenanceModeException extends ServiceUnavailableHttpException
{
/**
* When the application was put in maintenance mode.
*
* @var \Illuminate\Support\Carbon
* @var Carbon
*/
public $wentDownAt;

Expand All @@ -25,7 +26,7 @@ class MaintenanceModeException extends ServiceUnavailableHttpException
/**
* When the application should next be available.
*
* @var \Illuminate\Support\Carbon
* @var Carbon
*/
public $willBeAvailableAt;

Expand All @@ -43,12 +44,13 @@ public function __construct($time, $retryAfter = null, $message = null, Exceptio
{
parent::__construct($retryAfter, $message, $previous, $code);

$this->wentDownAt = Carbon::createFromTimestamp($time);
$dateFactory = app(Factory::class);
$this->wentDownAt = $dateFactory->createFromTimestamp($time);

if ($retryAfter) {
$this->retryAfter = $retryAfter;

$this->willBeAvailableAt = Carbon::createFromTimestamp($time)->addSeconds($this->retryAfter);
$this->willBeAvailableAt = $dateFactory->createFromTimestamp($time)->addSeconds($this->retryAfter);
}
}
}
6 changes: 5 additions & 1 deletion src/Illuminate/Foundation/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Illuminate\Foundation\Testing;

use Mockery;
use Illuminate\Support\Carbon;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Facade;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Console\Application as Artisan;
Expand Down Expand Up @@ -165,6 +166,9 @@ protected function tearDown()
if (class_exists(Carbon::class)) {
Carbon::setTestNow();
}
if (class_exists(CarbonImmutable::class)) {
CarbonImmutable::setTestNow();
}

$this->afterApplicationCreatedCallbacks = [];
$this->beforeApplicationDestroyedCallbacks = [];
Expand Down
Loading