Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/glynnforrest/temping
Work with temporary files and folders easily across OSes.
https://github.com/glynnforrest/temping
Last synced: 14 days ago
JSON representation
Work with temporary files and folders easily across OSes.
- Host: GitHub
- URL: https://github.com/glynnforrest/temping
- Owner: glynnforrest
- License: mit
- Created: 2013-07-18T22:12:07.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2015-04-24T07:08:58.000Z (over 9 years ago)
- Last Synced: 2024-12-05T13:48:19.699Z (about 1 month ago)
- Language: PHP
- Size: 328 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Temping
## Work with temporary files and folders easily across OSes.[![Build Status](https://travis-ci.org/glynnforrest/temping.png)](https://travis-ci.org/glynnforrest/temping)
## Motivation
Working with temporary files in your tests can be a pain. All good
tests should clean up after themselves, but trying to do this when
working with temporary files can be difficult and error-prone.create('my-file.txt');
//automatically create subdirectories too
$temp->create('file/in/sub/directory.php')Creating an empty directory
$temp->createDirectory('storage');
//automatically create subdirectories too
$temp->createDirectory('storage/with/sub/directories')Creating a file with contents
$temp->create('my/file.txt', 'Hello, world!');
Work with the contents of the file after creation using setContents()
and getContents().$filename = 'file.txt';
$temp->create($filename);
$temp->setContents($filename, 'Hello, world!');
echo $temp->getContents($filename);
//Hello, world!Files that are created, modified or deleted outside of the Temping
class are still accessible.file_put_contents($temp->getDirectory() . 'foo.txt', 'Hello, world!');
echo $temp->getContents('foo.txt');
//Hello, world!Get the full path name of a file
$filename = 'my-file.php';
$temp->create($filename);
echo $temp->getPathname($filename);
// /tmp/php-temping/my-file.phpGet the full path to the Temping directory
echo $temp->getDirectory();
// /tmp/php-temping/To do other fancy things with your temporary files, you can grab a
SplFileObject instance.$filename = 'my-file.php';
$temp->create($filename);
$obj = $temp->getFileObject($filename);
echo $obj->getExtension();
//phpThe default mode of the SplFileObject is read-only, 'r'. Pass any
accepted parameter to fopen() as the second argument to get a
different mode.$filename = 'my-file.txt';
$temp->create($filename);
$obj = $temp->getFileObject($filename, 'w');
//Now able to write to my-file.txtDelete a file or directory
$temp->delete('my-file.txt');
$temp->delete('my-directory/');If a directory is not empty, the deletion will fail. Pass true as a
second argument to delete a non-empty directory too.$temp->delete('non-empty-directory/', true);
To check if a file or directory has been created, use exists().
$this->temp->createDirectory('some/dir');
$this->temp->exists('some/dir');
//true$this->temp->create('foo/bar.txt');
$this->temp->exists('foo/bar.txt');
//true$this->temp->exists('something');
//falseLeave the $path argument blank to check if the temping directory
exists.$this->temp->exists();
//true
$this->temp->reset();
$this->temp->exists();
//falseExists will also check for files that weren't created by Temping
explicitly, but are still present in the temporary directory. This is
useful for testing code that is expected to create files.touch($temp->getDirectory() . 'foo.txt');
$temp->exists('foo.txt');
//true//In a test. Assume MyLogger takes the log directory in the
//constructor
$obj = new MyLogger($temp->getDirectory());
$obj->log('testing');
$this->assertTrue($temp->exists('log.log'));To check if a directory is empty, use isEmpty(). This function will
also return true if the directory doesn't exist.$this->temp->createDirectory('foo/bar');
$this->temp->isEmpty('foo/bar');
//true
$this->temp->create('foo/bar/baz.txt');
$this->temp->isEmpty('foo/bar');
//falseLeave the $dir argument blank to check if the temping directory is
empty.$this->temp->isEmpty();
//true
$this->temp->create('foo.php');
$this->temp->isEmpty();
//falseTo obliterate all the files in the temporary directory, plus the
directory itself, call reset().$temp->create('file1.txt')->create('file2.txt')->create('file3.txt');
$temp->reset();All files inside the temporary directory will be deleted, including
those that weren't created by Temping explicitly.If you need to recreate the temporary directory after calling reset(),
use init(). By default, init() is called by all Temping methods that
modify the filesystem.$temp->reset();
//temporary directory doesn't exist any more
$temp->create('foo');
//temporary directory recreated automatically$obj = new MyLogger($temp->getDirectory());
$temp->reset();
//temporary directory doesn't exist any more
$temp->init();
//temporary directory recreated for MyLogger to useAlso, if you want to use the temporary directory as the location to
test something, but not actually call any Temping methods, call init()
to create the directory manually.$obj = new MyLogger($temp->getDirectory());
//no methods called on $temp yet, so temporary directory doesn't exist
$temp->init();
//temporary directory recreated for MyLogger to useNow armed with Temping, MyFilesUsingTestCase can be refactored.
temp = new Temping\Temping();
$this->temp->create('file.txt', 'Hello, world!')
->create('file2.txt', 'Hello, again!);
}public function tearDown() {
$this->temp->reset();
}public function testSomething() {
$this->temp->create('another-file');
}}
Much better!
### Chainable methods
Methods return the Temping instance where it makes sense. This makes
it easy to do stuff like this:$temp->create('foo')
->create('bar', 'Hello world')
->setContents('foo', 'bar')
->delete('bar')
->reset();The methods init(), reset(), create(),
createDirectory(), delete() and setContents() are chainable.### Where are the files stored?
If you don't specify a directory in the constructor, Temping uses the
output of sys\_get\_temp_dir() to decide where to store the temporary
files.For example:
'file.txt' => '/tmp/php-temping/file.txt'
'dir/subdir/file.txt' => '/tmp/php-temping/dir/subdir/file.txt'