diff --git a/src/Illuminate/Cache/DatabaseStore.php b/src/Illuminate/Cache/DatabaseStore.php index 3428a4fb738..ed26e89a93d 100755 --- a/src/Illuminate/Cache/DatabaseStore.php +++ b/src/Illuminate/Cache/DatabaseStore.php @@ -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. @@ -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; @@ -186,7 +187,7 @@ protected function incrementOrDecrement($key, $value, Closure $callback) */ protected function getTime() { - return Carbon::now()->getTimestamp(); + return $this->currentTime(); } /** diff --git a/src/Illuminate/Cache/FileStore.php b/src/Illuminate/Cache/FileStore.php index 89ba6b380bd..d910731fa32 100755 --- a/src/Illuminate/Cache/FileStore.php +++ b/src/Illuminate/Cache/FileStore.php @@ -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. @@ -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(); @@ -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'); } @@ -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; } diff --git a/src/Illuminate/Cache/Lock.php b/src/Illuminate/Cache/Lock.php index 74fcdb6fbc6..e81bee6e08e 100644 --- a/src/Illuminate/Cache/Lock.php +++ b/src/Illuminate/Cache/Lock.php @@ -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. * @@ -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; } } diff --git a/src/Illuminate/Cache/MemcachedStore.php b/src/Illuminate/Cache/MemcachedStore.php index 4311f3bcc8d..00ba8b7c941 100755 --- a/src/Illuminate/Cache/MemcachedStore.php +++ b/src/Illuminate/Cache/MemcachedStore.php @@ -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. * @@ -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; } /** diff --git a/src/Illuminate/Cache/RateLimiter.php b/src/Illuminate/Cache/RateLimiter.php index c6460b8aa09..3f1ea046a5c 100644 --- a/src/Illuminate/Cache/RateLimiter.php +++ b/src/Illuminate/Cache/RateLimiter.php @@ -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. * @@ -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 ); } @@ -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(); } } diff --git a/src/Illuminate/Cookie/CookieJar.php b/src/Illuminate/Cookie/CookieJar.php index 2dcec7cc630..c7b263134a8 100755 --- a/src/Illuminate/Cookie/CookieJar.php +++ b/src/Illuminate/Cookie/CookieJar.php @@ -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). * @@ -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); } diff --git a/src/Illuminate/Foundation/Console/DownCommand.php b/src/Illuminate/Foundation/Console/DownCommand.php index 48e021766c1..e4fa9d31bef 100644 --- a/src/Illuminate/Foundation/Console/DownCommand.php +++ b/src/Illuminate/Foundation/Console/DownCommand.php @@ -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. * @@ -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(), ]; diff --git a/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php b/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php index 2e4337c91d8..01bea90da03 100644 --- a/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php +++ b/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php @@ -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. * @@ -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 ) ); diff --git a/src/Illuminate/Queue/Console/RestartCommand.php b/src/Illuminate/Queue/Console/RestartCommand.php index 2ddd7b87be0..1b34041547e 100644 --- a/src/Illuminate/Queue/Console/RestartCommand.php +++ b/src/Illuminate/Queue/Console/RestartCommand.php @@ -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. * @@ -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.'); } diff --git a/src/Illuminate/Queue/Jobs/DatabaseJobRecord.php b/src/Illuminate/Queue/Jobs/DatabaseJobRecord.php index a0e2ac8b84f..b4b5725467e 100644 --- a/src/Illuminate/Queue/Jobs/DatabaseJobRecord.php +++ b/src/Illuminate/Queue/Jobs/DatabaseJobRecord.php @@ -2,7 +2,7 @@ namespace Illuminate\Queue\Jobs; -use Illuminate\Queue\InteractsWithTime; +use Illuminate\Support\InteractsWithTime; class DatabaseJobRecord { diff --git a/src/Illuminate/Queue/Jobs/Job.php b/src/Illuminate/Queue/Jobs/Job.php index 31b195fef7e..cf8d84b2d73 100755 --- a/src/Illuminate/Queue/Jobs/Job.php +++ b/src/Illuminate/Queue/Jobs/Job.php @@ -2,7 +2,7 @@ namespace Illuminate\Queue\Jobs; -use Illuminate\Queue\InteractsWithTime; +use Illuminate\Support\InteractsWithTime; abstract class Job { diff --git a/src/Illuminate/Queue/Queue.php b/src/Illuminate/Queue/Queue.php index 31b2b706cb9..4052a79a70b 100755 --- a/src/Illuminate/Queue/Queue.php +++ b/src/Illuminate/Queue/Queue.php @@ -3,6 +3,7 @@ namespace Illuminate\Queue; use Illuminate\Container\Container; +use Illuminate\Support\InteractsWithTime; abstract class Queue { diff --git a/src/Illuminate/Routing/Middleware/ThrottleRequests.php b/src/Illuminate/Routing/Middleware/ThrottleRequests.php index eb8b47d51e9..076989fc42e 100644 --- a/src/Illuminate/Routing/Middleware/ThrottleRequests.php +++ b/src/Illuminate/Routing/Middleware/ThrottleRequests.php @@ -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. * @@ -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; diff --git a/src/Illuminate/Session/CookieSessionHandler.php b/src/Illuminate/Session/CookieSessionHandler.php index be93e766716..e91053d0b63 100755 --- a/src/Illuminate/Session/CookieSessionHandler.php +++ b/src/Illuminate/Session/CookieSessionHandler.php @@ -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. * @@ -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']; } } @@ -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; diff --git a/src/Illuminate/Session/DatabaseSessionHandler.php b/src/Illuminate/Session/DatabaseSessionHandler.php index ae0e1ee56c7..88d0c2e993a 100644 --- a/src/Illuminate/Session/DatabaseSessionHandler.php +++ b/src/Illuminate/Session/DatabaseSessionHandler.php @@ -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. * @@ -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) { @@ -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(); } /** diff --git a/src/Illuminate/Queue/InteractsWithTime.php b/src/Illuminate/Support/InteractsWithTime.php similarity index 96% rename from src/Illuminate/Queue/InteractsWithTime.php rename to src/Illuminate/Support/InteractsWithTime.php index f1bdacc8b92..719fcad970e 100644 --- a/src/Illuminate/Queue/InteractsWithTime.php +++ b/src/Illuminate/Support/InteractsWithTime.php @@ -1,10 +1,9 @@ expects($this->any())->method('getDateFormat')->will($this->returnValue('Y-m-d H:i:s')); $model->setRawAttributes([ 'created_at' => '2012-12-04', - 'updated_at' => Carbon::now()->getTimestamp(), + 'updated_at' => $this->currentTime(), ]); $this->assertInstanceOf(\Illuminate\Support\Carbon::class, $model->created_at); @@ -408,7 +411,7 @@ public function testTimestampsAreCreatedFromStringsAndIntegers() $this->assertInstanceOf(\Illuminate\Support\Carbon::class, $model->created_at); $model = new EloquentDateModelStub; - $model->created_at = Carbon::now()->getTimestamp(); + $model->created_at = $this->currentTime(); $this->assertInstanceOf(\Illuminate\Support\Carbon::class, $model->created_at); $model = new EloquentDateModelStub; diff --git a/tests/Queue/RedisQueueIntegrationTest.php b/tests/Queue/RedisQueueIntegrationTest.php index 31566d026eb..f2187168f58 100644 --- a/tests/Queue/RedisQueueIntegrationTest.php +++ b/tests/Queue/RedisQueueIntegrationTest.php @@ -8,11 +8,12 @@ use Illuminate\Queue\RedisQueue; use Illuminate\Container\Container; use Illuminate\Queue\Jobs\RedisJob; +use Illuminate\Support\InteractsWithTime; use Illuminate\Tests\Redis\InteractsWithRedis; class RedisQueueIntegrationTest extends TestCase { - use InteractsWithRedis; + use InteractsWithRedis, InteractsWithTime; /** * @var RedisQueue @@ -78,10 +79,10 @@ public function testPopProperlyPopsJobOffOfRedis($driver) $this->queue->push($job); // Pop and check it is popped correctly - $before = Carbon::now()->getTimestamp(); + $before = $this->currentTime(); /** @var RedisJob $redisJob */ $redisJob = $this->queue->pop(); - $after = Carbon::now()->getTimestamp(); + $after = $this->currentTime(); $this->assertEquals($job, unserialize(json_decode($redisJob->getRawBody())->data->command)); $this->assertEquals(1, $redisJob->attempts()); @@ -112,9 +113,9 @@ public function testPopProperlyPopsDelayedJobOffOfRedis($driver) $this->queue->later(-10, $job); // Pop and check it is popped correctly - $before = Carbon::now()->getTimestamp(); + $before = $this->currentTime(); $this->assertEquals($job, unserialize(json_decode($this->queue->pop()->getRawBody())->data->command)); - $after = Carbon::now()->getTimestamp(); + $after = $this->currentTime(); // Check reserved queue $this->assertEquals(1, $this->redis[$driver]->connection()->zcard('queues:default:reserved')); @@ -141,9 +142,9 @@ public function testPopPopsDelayedJobOffOfRedisWhenExpireNull($driver) $this->queue->later(-10, $job); // Pop and check it is popped correctly - $before = Carbon::now()->getTimestamp(); + $before = $this->currentTime(); $this->assertEquals($job, unserialize(json_decode($this->queue->pop()->getRawBody())->data->command)); - $after = Carbon::now()->getTimestamp(); + $after = $this->currentTime(); // Check reserved queue $this->assertEquals(1, $this->redis[$driver]->connection()->zcard('queues:default:reserved')); @@ -168,18 +169,18 @@ public function testNotExpireJobsWhenExpireNull($driver) // Make an expired reserved job $failed = new RedisQueueIntegrationTestJob(-20); $this->queue->push($failed); - $beforeFailPop = Carbon::now()->getTimestamp(); + $beforeFailPop = $this->currentTime(); $this->queue->pop(); - $afterFailPop = Carbon::now()->getTimestamp(); + $afterFailPop = $this->currentTime(); // Push an item into queue $job = new RedisQueueIntegrationTestJob(10); $this->queue->push($job); // Pop and check it is popped correctly - $before = Carbon::now()->getTimestamp(); + $before = $this->currentTime(); $this->assertEquals($job, unserialize(json_decode($this->queue->pop()->getRawBody())->data->command)); - $after = Carbon::now()->getTimestamp(); + $after = $this->currentTime(); // Check reserved queue $this->assertEquals(2, $this->redis[$driver]->connection()->zcard('queues:default:reserved')); @@ -214,9 +215,9 @@ public function testExpireJobsWhenExpireSet($driver) $this->queue->push($job); // Pop and check it is popped correctly - $before = Carbon::now()->getTimestamp(); + $before = $this->currentTime(); $this->assertEquals($job, unserialize(json_decode($this->queue->pop()->getRawBody())->data->command)); - $after = Carbon::now()->getTimestamp(); + $after = $this->currentTime(); // Check reserved queue $this->assertEquals(1, $this->redis[$driver]->connection()->zcard('queues:default:reserved')); @@ -244,9 +245,9 @@ public function testRelease($driver) //pop and release the job /** @var \Illuminate\Queue\Jobs\RedisJob $redisJob */ $redisJob = $this->queue->pop(); - $before = Carbon::now()->getTimestamp(); + $before = $this->currentTime(); $redisJob->release(1000); - $after = Carbon::now()->getTimestamp(); + $after = $this->currentTime(); //check the content of delayed queue $this->assertEquals(1, $this->redis[$driver]->connection()->zcard('queues:default:delayed'));