From a3451859d1cff45fba423cf577d00f5b2b648c7a Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Wed, 11 Nov 2020 08:50:19 -0600 Subject: [PATCH] formatting --- src/Illuminate/Cache/CacheLock.php | 2 +- src/Illuminate/Cache/FileStore.php | 5 +- src/Illuminate/Cache/NullStore.php | 50 +++++------ src/Illuminate/Filesystem/Filesystem.php | 20 ----- .../Filesystem/{File.php => LockableFile.php} | 88 +++++++++---------- 5 files changed, 71 insertions(+), 94 deletions(-) rename src/Illuminate/Filesystem/{File.php => LockableFile.php} (85%) diff --git a/src/Illuminate/Cache/CacheLock.php b/src/Illuminate/Cache/CacheLock.php index bb3202b11e70..f4a1407c1cbf 100644 --- a/src/Illuminate/Cache/CacheLock.php +++ b/src/Illuminate/Cache/CacheLock.php @@ -64,7 +64,7 @@ public function release() } /** - * Releases this lock in disregard of ownership. + * Releases this lock regardless of ownership. * * @return void */ diff --git a/src/Illuminate/Cache/FileStore.php b/src/Illuminate/Cache/FileStore.php index 2f72d978b630..c3d0bc9c34c8 100755 --- a/src/Illuminate/Cache/FileStore.php +++ b/src/Illuminate/Cache/FileStore.php @@ -7,11 +7,12 @@ use Illuminate\Contracts\Cache\Store; use Illuminate\Contracts\Filesystem\LockTimeoutException; use Illuminate\Filesystem\Filesystem; +use Illuminate\Filesystem\LockableFile; use Illuminate\Support\InteractsWithTime; class FileStore implements Store, LockProvider { - use InteractsWithTime, RetrievesMultipleKeys, HasCacheLock; + use InteractsWithTime, HasCacheLock, RetrievesMultipleKeys; /** * The Illuminate Filesystem instance. @@ -97,7 +98,7 @@ public function add($key, $value, $seconds) { $this->ensureCacheDirectoryExists($path = $this->path($key)); - $file = $this->files->newFileForReadWrite($path); + $file = new LockableFile($path, 'c+'); try { $file->getExclusiveLock(); diff --git a/src/Illuminate/Cache/NullStore.php b/src/Illuminate/Cache/NullStore.php index d19f0a6f16af..0fe4268f7a3c 100755 --- a/src/Illuminate/Cache/NullStore.php +++ b/src/Illuminate/Cache/NullStore.php @@ -68,6 +68,31 @@ public function forever($key, $value) return false; } + /** + * Get a lock instance. + * + * @param string $name + * @param int $seconds + * @param string|null $owner + * @return \Illuminate\Contracts\Cache\Lock + */ + public function lock($name, $seconds = 0, $owner = null) + { + return new NoLock($name, $seconds, $owner); + } + + /** + * Restore a lock instance using the owner identifier. + * + * @param string $name + * @param string $owner + * @return \Illuminate\Contracts\Cache\Lock + */ + public function restoreLock($name, $owner) + { + return $this->lock($name, 0, $owner); + } + /** * Remove an item from the cache. * @@ -98,29 +123,4 @@ public function getPrefix() { return ''; } - - /** - * Get a lock instance. - * - * @param string $name - * @param int $seconds - * @param string|null $owner - * @return \Illuminate\Contracts\Cache\Lock - */ - public function lock($name, $seconds = 0, $owner = null) - { - return new NoLock($name, $seconds, $owner); - } - - /** - * Restore a lock instance using the owner identifier. - * - * @param string $name - * @param string $owner - * @return \Illuminate\Contracts\Cache\Lock - */ - public function restoreLock($name, $owner) - { - return $this->lock($name, 0, $owner); - } } diff --git a/src/Illuminate/Filesystem/Filesystem.php b/src/Illuminate/Filesystem/Filesystem.php index e977a5f63197..2076f4ffe93e 100644 --- a/src/Illuminate/Filesystem/Filesystem.php +++ b/src/Illuminate/Filesystem/Filesystem.php @@ -185,26 +185,6 @@ public function put($path, $contents, $lock = false) return file_put_contents($path, $contents, $lock ? LOCK_EX : 0); } - /** - * Create a new File instance for read and write. - * - * @return \Illuminate\Filesystem\File - */ - public function newFileForReadWrite($path) - { - return new File($this, $path, 'c+'); - } - - /** - * Create a new File instance for read and write. - * - * @return \Illuminate\Filesystem\File - */ - public function newFileForRead($path) - { - return new File($this, $path, 'r'); - } - /** * Write the contents of a file, replacing it atomically if it already exists. * diff --git a/src/Illuminate/Filesystem/File.php b/src/Illuminate/Filesystem/LockableFile.php similarity index 85% rename from src/Illuminate/Filesystem/File.php rename to src/Illuminate/Filesystem/LockableFile.php index 65d115bd375e..c9dbdfbf491c 100644 --- a/src/Illuminate/Filesystem/File.php +++ b/src/Illuminate/Filesystem/LockableFile.php @@ -4,7 +4,7 @@ use Illuminate\Contracts\Filesystem\LockTimeoutException; -class File +class LockableFile { /** * The file resource. @@ -13,13 +13,6 @@ class File */ protected $handle; - /** - * The Illuminate Filesystem instance. - * - * @var \Illuminate\Filesystem\Filesystem - */ - protected $files; - /** * The file path. * @@ -28,7 +21,7 @@ class File protected $path; /** - * Determine if the file is locked. + * Indicates if the file is locked. * * @var bool */ @@ -41,15 +34,39 @@ class File * @param string $mode * @return void */ - public function __construct(Filesystem $files, $path, $mode) + public function __construct($path, $mode) { - $this->files = $files; $this->path = $path; $this->ensureDirectoryExists($path); $this->createResource($path, $mode); } + /** + * Create the file's directory if necessary. + * + * @param string $path + * @return void + */ + protected function ensureDirectoryExists($path) + { + if (! file_exists(dirname($path))) { + @mkdir(dirname($path), 0777, true); + } + } + + /** + * Create the file resource. + * + * @param string $path + * @param string $mode + * @return void + */ + protected function createResource($path, $mode) + { + $this->handle = @fopen($path, $mode); + } + /** * Read the file contents. * @@ -63,6 +80,16 @@ public function read($length = null) return fread($this->handle, $length ?? ($this->size() ?: 1)); } + /** + * Get the file size. + * + * @return int + */ + public function size() + { + return filesize($this->path); + } + /** * Write to the file. * @@ -95,12 +122,13 @@ public function truncate() /** * Get a shared lock on the file. * + * @param bool $block * @return $this */ public function getSharedLock($block = false) { if (! flock($this->handle, LOCK_SH | ($block ? 0 : LOCK_NB))) { - throw new LockTimeoutException("Unable to acquire file lock at path {$path}."); + throw new LockTimeoutException("Unable to acquire file lock at path [{$path}]."); } $this->isLocked = true; @@ -111,12 +139,13 @@ public function getSharedLock($block = false) /** * Get an exclusive lock on the file. * + * @param bool $block * @return bool */ public function getExclusiveLock($block = false) { if (! flock($this->handle, LOCK_EX | ($block ? 0 : LOCK_NB))) { - throw new LockTimeoutException("Unable to acquire file lock at path {$path}."); + throw new LockTimeoutException("Unable to acquire file lock at path [{$path}]."); } $this->isLocked = true; @@ -151,37 +180,4 @@ public function close() return fclose($this->handle); } - - /** - * Get the file size. - * - * @return int - */ - public function size() - { - return filesize($this->path); - } - - /** - * Create the file resource. - * - * @return void - */ - protected function createResource($path, $mode) - { - $this->handle = @fopen($path, $mode); - } - - /** - * Create the file directory if necessary. - * - * @param string $path - * @return void - */ - protected function ensureDirectoryExists($path) - { - if (! $this->files->exists(dirname($path))) { - $this->files->makeDirectory(dirname($path), 0777, true, true); - } - } }