Source of file File.php
Size: 2,976 Bytes - Last Modified: 2015-12-22T09:12:14-05:00
../src/File.php
1234567891011121314151617181920212223242526272829303132333435363738394041424344
Covered by 7 test(s):
45
Covered by 3 test(s):
464748
Covered by 4 test(s):
495051525354555657585960
Covered by 2 test(s):
61
Covered by 1 test(s):
62
Covered by 1 test(s):
63
Covered by 1 test(s):
646566
Covered by 1 test(s):
6768
Covered by 1 test(s):
697071727374757677787980
Covered by 3 test(s):
8182
Covered by 1 test(s):
8384
Covered by 1 test(s):
85868788899091929394959697
Covered by 1 test(s):
9899
Covered by 1 test(s):
100101
Covered by 1 test(s):
102103104105106107108109110111
Covered by 3 test(s):
112113114115116117118119120121122123
Covered by 3 test(s):
124125
Covered by 3 test(s):
126127128129130131132133134135
Covered by 4 test(s):
136
Covered by 2 test(s):
137138
Covered by 2 test(s):
139140
| <?php /** * Class to contain logic for accessing files * * @category PHP * @package MvcLite * @subpackage File * @since File available since release 2.0.0 * @author Cory Collier <corycollier@corycollier.com> */ namespace MvcLite; use MvcLite\Traits\Filepath as FilepathTrait; /** * Class to contain logic for accessing files * * @category PHP * @package MvcLite * @subpackage File * @since File available since release 2.0.0 * @author Cory Collier <corycollier@corycollier.com> */ class File extends ObjectAbstract { use FilepathTrait; /** * The contents of the instance's file. * * @var string $_contents */ protected $contents = ''; /** * tests the existance of a given filename * * @param string $filename * @return boolean */ public function test($filename) { if (file_exists($filename)) { return true; } return false; } /** * Saves the content of the file to a provided filename. * * @param string $filename The full path and name of the file. * * @return MvcLite\File $this for object-chaining. */ public function save($filename) { if (! file_exists(dirname($filename))) { throw new Exception( "Directory [{$filename}] does not exist. Cannot save file" ); } file_put_contents($filename, $this->getContents()); return $this; } /** * loads file information to the file instance * * @param string $filename The full path and name of the file. * * @return MvcLite\File $this for object-chaining. */ public function load($filename) { $this->checkFileExists($filename); $this->setContents(file_get_contents($filename)); return $this; } /** * Deletes a file by filename. * * @param string $filename The full path and name of the file. * * @return MvcLite\File $this for object-chaining. */ public function delete($filename) { $this->checkFileExists($filename); unlink($filename); return $this; } /** * Getter for the _contents property * * @return string */ public function getContents() { return $this->contents; } /** * Setter for the contents property. * * @param string|null $contents * * @return MvcLite\File $this for object-chaining. */ public function setContents($contents = null) { $this->contents = $contents; return $this; } /** * method to throw an exception if a given filename doesn't exist * * @param string $filename */ protected function checkFileExists($filename) { if (! $this->test($filename)) { throw new Exception("File doesn't exist"); } } } |