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.5] Move InteractsWithTime to Support #20119

Merged
merged 2 commits into from
Jul 18, 2017
Merged
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
7 changes: 4 additions & 3 deletions src/Illuminate/Cache/DatabaseStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Database\ConnectionInterface;

class DatabaseStore implements Store
{
use RetrievesMultipleKeys;
use InteractsWithTime, RetrievesMultipleKeys;

/**
* The database connection instance.
Expand Down Expand Up @@ -72,7 +73,7 @@ public function get($key)
// If this cache expiration date is past the current time, we will remove this
// item from the cache. Then we will return a null value since the cache is
// expired. We will use "Carbon" to make this comparison with the column.
if (Carbon::now()->getTimestamp() >= $cache->expiration) {
if ($this->currentTime() >= $cache->expiration) {
$this->forget($key);

return;
Expand Down Expand Up @@ -186,7 +187,7 @@ protected function incrementOrDecrement($key, $value, Closure $callback)
*/
protected function getTime()
{
return Carbon::now()->getTimestamp();
return $this->currentTime();
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
namespace Illuminate\Cache;

use Exception;
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\InteractsWithTime;

class FileStore implements Store
{
use RetrievesMultipleKeys;
use InteractsWithTime, RetrievesMultipleKeys;

/**
* The Illuminate Filesystem instance.
Expand Down Expand Up @@ -178,7 +178,7 @@ protected function getPayload($key)
// If the current time is greater than expiration timestamps we will delete
// the file and return null. This helps clean up the old files and keeps
// this directory much cleaner for us as old files aren't hanging out.
if (Carbon::now()->getTimestamp() >= $expire) {
if ($this->currentTime() >= $expire) {
$this->forget($key);

return $this->emptyPayload();
Expand All @@ -189,7 +189,7 @@ protected function getPayload($key)
// Next, we'll extract the number of minutes that are remaining for a cache
// so that we can properly retain the time for things like the increment
// operation that may be performed on this cache on a later operation.
$time = ($expire - Carbon::now()->getTimestamp()) / 60;
$time = ($expire - $this->currentTime()) / 60;

return compact('data', 'time');
}
Expand Down Expand Up @@ -225,7 +225,7 @@ protected function path($key)
*/
protected function expiration($minutes)
{
$time = Carbon::now()->getTimestamp() + (int) ($minutes * 60);
$time = $this->availableAt((int) ($minutes * 60));

return $minutes === 0 || $time > 9999999999 ? 9999999999 : (int) $time;
}
Expand Down
7 changes: 5 additions & 2 deletions src/Illuminate/Cache/Lock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Illuminate\Cache;

use Illuminate\Support\InteractsWithTime;
use Illuminate\Contracts\Cache\LockTimeoutException;

abstract class Lock
{
use InteractsWithTime;

/**
* The name of the lock.
*
Expand Down Expand Up @@ -69,12 +72,12 @@ public function get($callback = null)
*/
public function block($seconds, $callback = null)
{
$starting = time();
$starting = $this->currentTime();

while (! $this->acquire()) {
usleep(250 * 1000);

if (time() - $seconds >= $starting) {
if ($this->currentTime() - $seconds >= $starting) {
throw new LockTimeoutException;
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Cache/MemcachedStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

use Memcached;
use ReflectionMethod;
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Cache\Store;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Contracts\Cache\LockProvider;

class MemcachedStore extends TaggableStore implements LockProvider, Store
{
use InteractsWithTime;

/**
* The Memcached instance.
*
Expand Down Expand Up @@ -212,7 +214,7 @@ public function flush()
*/
protected function toTimestamp($minutes)
{
return $minutes > 0 ? Carbon::now()->addSeconds($minutes * 60)->getTimestamp() : 0;
return $minutes > 0 ? $this->availableAt($minutes * 60) : 0;
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Cache/RateLimiter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Cache;

use Illuminate\Support\Carbon;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Contracts\Cache\Repository as Cache;

class RateLimiter
{
use InteractsWithTime;

/**
* The cache store implementation.
*
Expand Down Expand Up @@ -60,7 +62,7 @@ public function tooManyAttempts($key, $maxAttempts, $decayMinutes = 1)
protected function lockout($key, $decayMinutes)
{
$this->cache->add(
$key.':lockout', Carbon::now()->getTimestamp() + ($decayMinutes * 60), $decayMinutes
$key.':lockout', $this->availableAt($decayMinutes * 60), $decayMinutes
);
}

Expand Down Expand Up @@ -135,6 +137,6 @@ public function clear($key)
*/
public function availableIn($key)
{
return $this->cache->get($key.':lockout') - Carbon::now()->getTimestamp();
return $this->cache->get($key.':lockout') - $this->currentTime();
}
}
6 changes: 4 additions & 2 deletions src/Illuminate/Cookie/CookieJar.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Illuminate\Cookie;

use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\InteractsWithTime;
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Contracts\Cookie\QueueingFactory as JarContract;

class CookieJar implements JarContract
{
use InteractsWithTime;

/**
* The default path (if specified).
*
Expand Down Expand Up @@ -62,7 +64,7 @@ public function make($name, $value, $minutes = 0, $path = null, $domain = null,
{
list($path, $domain, $secure, $sameSite) = $this->getPathAndDomain($path, $domain, $secure, $sameSite);

$time = ($minutes == 0) ? 0 : Carbon::now()->getTimestamp() + ($minutes * 60);
$time = ($minutes == 0) ? 0 : $this->availableAt($minutes * 60);

return new Cookie($name, $value, $time, $path, $domain, $secure, $httpOnly, $raw, $sameSite);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Console/DownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Foundation\Console;

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

class DownCommand extends Command
{
use InteractsWithTime;

/**
* The console command signature.
*
Expand Down Expand Up @@ -45,7 +47,7 @@ public function handle()
protected function getDownFilePayload()
{
return [
'time' => Carbon::now()->getTimestamp(),
'time' => $this->currentTime(),
'message' => $this->option('message'),
'retry' => $this->getRetryTime(),
];
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
namespace Illuminate\Foundation\Http\Middleware;

use Closure;
use Illuminate\Support\Carbon;
use Illuminate\Foundation\Application;
use Illuminate\Support\InteractsWithTime;
use Symfony\Component\HttpFoundation\Cookie;
use Illuminate\Contracts\Encryption\Encrypter;
use Illuminate\Session\TokenMismatchException;

class VerifyCsrfToken
{
use InteractsWithTime;

/**
* The application instance.
*
Expand Down Expand Up @@ -155,7 +157,7 @@ protected function addCookieToResponse($request, $response)

$response->headers->setCookie(
new Cookie(
'XSRF-TOKEN', $request->session()->token(), Carbon::now()->getTimestamp() + 60 * $config['lifetime'],
'XSRF-TOKEN', $request->session()->token(), $this->availableAt(60 * $config['lifetime']),
$config['path'], $config['domain'], $config['secure'], false, false, $config['same_site'] ?? null
)
);
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Queue/Console/RestartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Illuminate\Queue\Console;

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

class RestartCommand extends Command
{
use InteractsWithTime;

/**
* The console command name.
*
Expand All @@ -28,7 +30,7 @@ class RestartCommand extends Command
*/
public function handle()
{
$this->laravel['cache']->forever('illuminate:queue:restart', Carbon::now()->getTimestamp());
$this->laravel['cache']->forever('illuminate:queue:restart', $this->currentTime());

$this->info('Broadcasting queue restart signal.');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Jobs/DatabaseJobRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Queue\Jobs;

use Illuminate\Queue\InteractsWithTime;
use Illuminate\Support\InteractsWithTime;

class DatabaseJobRecord
{
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Queue/Jobs/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Illuminate\Queue\Jobs;

use Illuminate\Queue\InteractsWithTime;
use Illuminate\Support\InteractsWithTime;

abstract class Job
{
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Queue/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Queue;

use Illuminate\Container\Container;
use Illuminate\Support\InteractsWithTime;

abstract class Queue
{
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Routing/Middleware/ThrottleRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Closure;
use RuntimeException;
use Illuminate\Support\Str;
use Illuminate\Support\Carbon;
use Illuminate\Cache\RateLimiter;
use Illuminate\Support\InteractsWithTime;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

class ThrottleRequests
{
use InteractsWithTime;

/**
* The rate limiter instance.
*
Expand Down Expand Up @@ -151,7 +153,7 @@ protected function getHeaders($maxAttempts, $remainingAttempts, $retryAfter = nu

if (! is_null($retryAfter)) {
$headers['Retry-After'] = $retryAfter;
$headers['X-RateLimit-Reset'] = Carbon::now()->getTimestamp() + $retryAfter;
$headers['X-RateLimit-Reset'] = $this->availableAt($retryAfter);
}

return $headers;
Expand Down
8 changes: 5 additions & 3 deletions src/Illuminate/Session/CookieSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Illuminate\Session;

use SessionHandlerInterface;
use Illuminate\Support\Carbon;
use Illuminate\Support\InteractsWithTime;
use Symfony\Component\HttpFoundation\Request;
use Illuminate\Contracts\Cookie\QueueingFactory as CookieJar;

class CookieSessionHandler implements SessionHandlerInterface
{
use InteractsWithTime;

/**
* The cookie jar instance.
*
Expand Down Expand Up @@ -67,7 +69,7 @@ public function read($sessionId)
$value = $this->request->cookies->get($sessionId) ?: '';

if (! is_null($decoded = json_decode($value, true)) && is_array($decoded)) {
if (isset($decoded['expires']) && Carbon::now()->getTimestamp() <= $decoded['expires']) {
if (isset($decoded['expires']) && $this->currentTime() <= $decoded['expires']) {
return $decoded['data'];
}
}
Expand All @@ -82,7 +84,7 @@ public function write($sessionId, $data)
{
$this->cookie->queue($sessionId, json_encode([
'data' => $data,
'expires' => Carbon::now()->addMinutes($this->minutes)->getTimestamp(),
'expires' => $this->availableAt($this->minutes * 60),
]), $this->minutes);

return true;
Expand Down
7 changes: 5 additions & 2 deletions src/Illuminate/Session/DatabaseSessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
use Illuminate\Support\Carbon;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Database\QueryException;
use Illuminate\Support\InteractsWithTime;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Contracts\Container\Container;

class DatabaseSessionHandler implements SessionHandlerInterface, ExistenceAwareInterface
{
use InteractsWithTime;

/**
* The database connection instance.
*
Expand Down Expand Up @@ -170,7 +173,7 @@ protected function getDefaultPayload($data)
{
$payload = [
'payload' => base64_encode($data),
'last_activity' => Carbon::now()->getTimestamp(),
'last_activity' => $this->currentTime(),
];

if (! $this->container) {
Expand Down Expand Up @@ -261,7 +264,7 @@ public function destroy($sessionId)
*/
public function gc($lifetime)
{
$this->getQuery()->where('last_activity', '<=', Carbon::now()->getTimestamp() - $lifetime)->delete();
$this->getQuery()->where('last_activity', '<=', $this->currentTime() - $lifetime)->delete();
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
<?php

namespace Illuminate\Queue;
namespace Illuminate\Support;

use DateInterval;
use DateTimeInterface;
use Illuminate\Support\Carbon;

trait InteractsWithTime
{
Expand Down
Loading