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);
}]);
Add to composer.json
:
...
"require-dev": {
"task/filesystem": "~0.2"
}
...
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.
open
open($filename, $mode = 'r+')
FilesystemPlugin::touch($filename, $time = null, $atime = null)
See Symfony's Filesystem::touch
documentation for argument description. Returns Task\Plugin\Filesystem\File
, opened with r+
.
ls
ls($dir)
copy($source, $target, $override = false)
Supports multiple operations, e.g.
Given:
use Task\Plugin\FilesystemPlugin;
$fs = new FilesystemPlugin;
File to file:
/
foo
# @return File('bar')
$fs->copy('foo', 'bar')
/
foo
bar
File to directory:
/
foo
bar/
# @return File('bar/foo')
$fs->copy('foo', 'bar')
/
foo
bar/
foo
Link to link:
/
foo
bar -> foo
# @return File('wow')
$fs->copy('foo', 'wow')
/
foo
bar -> foo
wow -> foo
Directory to directory:
/
foo/
bar
# @return FilesystemIterator('wow')
$fs->copy('foo', 'wow')
/
foo/
bar
wow/
bar
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:
/
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