task/filesystem
, (*1)
Example
use Task\Plugin\FilesystemPlugin;
use Symfony\Component\Finder\Finder;
$project->inject(function ($container) {
$container['fs'] = new FilesystemPlugin;
});
$project->addTask('write', ['fs', function ($fs) {
$fs->open('/tmp/foo')->write('wow');
}]);
$project->addTask('read', ['fs', function ($fs) {
$fs->read('/tmp/foo')->pipe($this->getOutput());
}]);
$project->addTask('copy', ['fs', function ($fs) {
$fs->copy('/tmp/foo', '/tmp/bar');
# OR
$fs->read('/tmp/foo')->pipe($fs->touch('/tmp/bar'));
}]);
$project->addTask('copyTree', ['fs', function ($fs) {
$finder = new Finder;
$finder->name('foo')->in('/tmp/source');
$fs->copyTree('/tmp'source', '/tmp/target', $finder);
}]);
Installation
Add to composer.json
:, (*2)
...
"require-dev": {
"task/filesystem": "~0.2"
}
...
Usage
Task\Plugin\FilesystemPlugin
extends Symfony's Filesystem
component object, overring some methods and providing some new ones. Many of these methods return streams which can be piped to other plugins., (*3)
open
open($filename, $mode = 'r+')
, (*4)
Returns Task\Plugin\Filesystem\File
, opened with the specified mode., (*5)
touch
FilesystemPlugin::touch($filename, $time = null, $atime = null)
, (*6)
See Symfony's Filesystem::touch
documentation for argument description. Returns Task\Plugin\Filesystem\File
, opened with r+
., (*7)
ls
ls($dir)
, (*8)
Returns Task\Plugin\Filesystem\FilesystemIterator
., (*9)
copy
copy($source, $target, $override = false)
, (*10)
Supports multiple operations, e.g., (*11)
Given:, (*12)
use Task\Plugin\FilesystemPlugin;
$fs = new FilesystemPlugin;
File to file:, (*13)
/
foo
# @return File('bar')
$fs->copy('foo', 'bar')
/
foo
bar
File to directory:, (*14)
/
foo
bar/
# @return File('bar/foo')
$fs->copy('foo', 'bar')
/
foo
bar/
foo
Link to link:, (*15)
/
foo
bar -> foo
# @return File('wow')
$fs->copy('foo', 'wow')
/
foo
bar -> foo
wow -> foo
Directory to directory:, (*16)
/
foo/
bar
# @return FilesystemIterator('wow')
$fs->copy('foo', 'wow')
/
foo/
bar
wow/
bar
mirror
mirror($originDir, $targetDir, Traversable $iterator = null, $options = [])
Mirror a directory, optionally providing a Traversable
instance to select or exclude files. Symfony's Finder
component is really good for this:, (*17)
/
foo/
.git/
objects/
bar
baz
use Task\Plugin\FilesystemPlugin;
use Symfony\Component\Finder\Finder;
$finder = new Finder;
$finder->ignoreVcs()->in('foo');
$fs = new FilesystemPlugin;
# @return FilesystemIterator('wow')
$fs->mirror('foo', 'wow', $finder);
/
foo/
.git/
objects/
bar
baz
wow/
bar
baz