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):
  • MvcLite\FileTest::testTest with data set "Bad path, should not exist"
  • MvcLite\FileTest::testTest with data set "Good path, should exist"
  • MvcLite\FileTest::testTest with data set "Good path, bad file, should not exist"
  • MvcLite\FileTest::testLoad with data set "Bad path, should not exist"
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testLoad with data set "Good path, bad file, should not exist"
  • MvcLite\FileTest::testDelete with data set "file exists"
45
Covered by 3 test(s):
  • MvcLite\FileTest::testTest with data set "Good path, should exist"
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testDelete with data set "file exists"
464748
Covered by 4 test(s):
  • MvcLite\FileTest::testTest with data set "Bad path, should not exist"
  • MvcLite\FileTest::testTest with data set "Good path, bad file, should not exist"
  • MvcLite\FileTest::testLoad with data set "Bad path, should not exist"
  • MvcLite\FileTest::testLoad with data set "Good path, bad file, should not exist"
495051525354555657585960
Covered by 2 test(s):
  • MvcLite\FileTest::testSave with data set "directory exists, expect save"
  • MvcLite\FileTest::testSave with data set "directory does not exist, do not expect save"
61
Covered by 1 test(s):
  • MvcLite\FileTest::testSave with data set "directory does not exist, do not expect save"
62
Covered by 1 test(s):
  • MvcLite\FileTest::testSave with data set "directory does not exist, do not expect save"
63
Covered by 1 test(s):
  • MvcLite\FileTest::testSave with data set "directory does not exist, do not expect save"
646566
Covered by 1 test(s):
  • MvcLite\FileTest::testSave with data set "directory exists, expect save"
6768
Covered by 1 test(s):
  • MvcLite\FileTest::testSave with data set "directory exists, expect save"
697071727374757677787980
Covered by 3 test(s):
  • MvcLite\FileTest::testLoad with data set "Bad path, should not exist"
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testLoad with data set "Good path, bad file, should not exist"
8182
Covered by 1 test(s):
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
8384
Covered by 1 test(s):
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
85868788899091929394959697
Covered by 1 test(s):
  • MvcLite\FileTest::testDelete with data set "file exists"
9899
Covered by 1 test(s):
  • MvcLite\FileTest::testDelete with data set "file exists"
100101
Covered by 1 test(s):
  • MvcLite\FileTest::testDelete with data set "file exists"
102103104105106107108109110111
Covered by 3 test(s):
  • MvcLite\FileTest::testGetContents with data set "string contents"
  • MvcLite\FileTest::testGetContents with data set "whitespace contents"
  • MvcLite\FileTest::testSave with data set "directory exists, expect save"
112113114115116117118119120121122123
Covered by 3 test(s):
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testSave with data set "directory exists, expect save"
  • MvcLite\FileTest::testSave with data set "directory does not exist, do not expect save"
124125
Covered by 3 test(s):
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testSave with data set "directory exists, expect save"
  • MvcLite\FileTest::testSave with data set "directory does not exist, do not expect save"
126127128129130131132133134135
Covered by 4 test(s):
  • MvcLite\FileTest::testLoad with data set "Bad path, should not exist"
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testLoad with data set "Good path, bad file, should not exist"
  • MvcLite\FileTest::testDelete with data set "file exists"
136
Covered by 2 test(s):
  • MvcLite\FileTest::testLoad with data set "Bad path, should not exist"
  • MvcLite\FileTest::testLoad with data set "Good path, bad file, should not exist"
137138
Covered by 2 test(s):
  • MvcLite\FileTest::testLoad with data set "Good path, should exist"
  • MvcLite\FileTest::testDelete with data set "file exists"
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");
        }
    }
}