Created
November 27, 2014 11:16
-
-
Save fullybaked/e152910de0fef69c1b5a to your computer and use it in GitHub Desktop.
Simple wrapper for PHP's file_(get|put)_contents methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fullybaked; | |
use FileNotFound; | |
/** | |
* File class | |
* | |
* Simple wrapper for native methods file_get_contents and file_put_contents | |
* in an OOP context and with some useful methods for getting information about | |
* a given File | |
* | |
* @author Dave Baker <david@fullybaked.co.uk> | |
*/ | |
class File | |
{ | |
/** | |
* Full/absoulte path to file | |
* | |
* @var string | |
*/ | |
protected $fileLocation; | |
public function __construct($fileLocation) | |
{ | |
$this->setPath($fileLocation); | |
} | |
/** | |
* read in and return the current content of the file | |
* | |
* @return string | |
* @throws FileNotFound if file doesn't exist | |
*/ | |
public function read() | |
{ | |
if (!file_exists($this->path())) { | |
throw new FileNotFound($this->path()); | |
} | |
return file_get_contents($this->path()); | |
} | |
/** | |
* write $content to the file, file will be created or overwritten | |
* | |
* @param string $content | |
*/ | |
public function write($content) | |
{ | |
file_put_contents($this->path(), $content); | |
} | |
/** | |
* append $content to the end of an existing file, if file | |
* does not exist it will be written with $content | |
* | |
* NB: This does *not* write a new line. It literally appends to the end | |
* of the file. If you want a new line you *must* explicitly pass newline | |
* characters in your content | |
* | |
* @param string $content | |
*/ | |
public function append($content) | |
{ | |
file_put_contents($this->path(), $content, FILE_APPEND); | |
} | |
/** | |
* get the name of the current file | |
* | |
* @return string | |
*/ | |
public function name() | |
{ | |
return basename($this->path()); | |
} | |
/** | |
* get the dirname of the current file | |
* | |
* @return string | |
*/ | |
public function dir() | |
{ | |
return dirname($this->path()); | |
} | |
/** | |
* accessor - get the current file location | |
* | |
* @return string | |
*/ | |
public function path() | |
{ | |
return $this->fileLocation; | |
} | |
/** | |
* accessor - set the file location attribute | |
* | |
* @param string $fileLocation | |
* @return void | |
*/ | |
public function setPath($fileLocation) | |
{ | |
$this->fileLocation = $fileLocation; | |
} | |
/** | |
* return the filesize of the file | |
* | |
* @return int | |
* @throws FileNotFound if file doesn't exist | |
*/ | |
public function size() | |
{ | |
if (!file_exists($this->path())) { | |
throw new FileNotFound($this->path()); | |
} | |
return filesize($this->path()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Fullybaked; | |
/** | |
* FileNotFound Exception | |
* | |
* Extends the native PHP Exception class to simplify construction | |
* and allow more specific catches to be made | |
* | |
* @author Dave Baker <david@fullybaked.co.uk> | |
*/ | |
class FileNotFound extends \Exception | |
{ | |
public function __construct($fileName) | |
{ | |
parent::__construct(sprintf("File %s was not found or could not be accessed", $fileName)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment