Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 11, 2020
1 parent 6cb85b2 commit a345185
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Cache/CacheLock.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function release()
}

/**
* Releases this lock in disregard of ownership.
* Releases this lock regardless of ownership.
*
* @return void
*/
Expand Down
5 changes: 3 additions & 2 deletions src/Illuminate/Cache/FileStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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();
Expand Down
50 changes: 25 additions & 25 deletions src/Illuminate/Cache/NullStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
}
}
20 changes: 0 additions & 20 deletions src/Illuminate/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Contracts\Filesystem\LockTimeoutException;

class File
class LockableFile
{
/**
* The file resource.
Expand All @@ -13,13 +13,6 @@ class File
*/
protected $handle;

/**
* The Illuminate Filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;

/**
* The file path.
*
Expand All @@ -28,7 +21,7 @@ class File
protected $path;

/**
* Determine if the file is locked.
* Indicates if the file is locked.
*
* @var bool
*/
Expand All @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit a345185

Please sign in to comment.