Skip to content

Instantly share code, notes, and snippets.

@WyriHaximus
Created September 17, 2014 20:25
Show Gist options
  • Save WyriHaximus/bfbfa48602d11832a48b to your computer and use it in GitHub Desktop.
Save WyriHaximus/bfbfa48602d11832a48b to your computer and use it in GitHub Desktop.
protected $recursiveInvoker;
protected function getRecursiveInvoker() {
if ($this->recursiveInvoker instanceof RecursiveInvoker) {
return $this->recursiveInvoker;
}
$this->recursiveInvoker = new RecursiveInvoker($this);
return $this->recursiveInvoker;
}
public function chownRecursive($uid = -1, $gid = -1) {
return $this->getRecursiveInvoker('chown', $uid, $gid);
}
<?php
namespace React\Filesystem;
class RecursiveInvoker {
protected $node;
public function __construct(Directory $node) {
$this->node = $node;
}
public function execute($method, $args) {
$deferred = new Deferred();
$this->ls()->then(function($list) use ($deferred, $method, $args) {
$this->filesystem->getLoop()->futureTick(function() use ($list, $deferred, $method, $args) {
$this->iterateNode($list, $deferred, $method, $args);
});
});
return $deferred->promise();
}
protected function iterateNode($list, $deferred, $method, $args) {
$promises = [];
foreach ($list as $node) {
if ($node instanceof Directory) {
$promises[] = call_user_func_array([$node, $method . 'Recursive'], $args);
} else {
$promises[] = call_user_func_array([$node, $method], $args);
}
}
\React\Promise\all($promises)->then(function() use ($deferred, $method, $args) {
call_user_func_array([$this, $method], $args)->then(function() use ($deferred) {
$deferred->resolve();
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment