Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow nesting of trees #1059

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
72 changes: 54 additions & 18 deletions lib/DAV/Tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Tree {
class Tree implements ICollection {

/**
* The root node
Expand Down Expand Up @@ -60,21 +60,20 @@ function getNodeForPath($path) {
return $this->rootNode;
}

// Attempting to fetch its parent
list($parentName, $baseName) = Uri\split($path);
$parts = explode('/', $path);
$node = $this->rootNode;

// If there was no parent, we must simply ask it from the root node.
if ($parentName === "") {
$node = $this->rootNode->getChild($baseName);
} else {
// Otherwise, we recursively grab the parent and ask him/her.
$parent = $this->getNodeForPath($parentName);

if (!($parent instanceof ICollection))
while (count($parts)) {
if (!($node instanceof ICollection))
throw new Exception\NotFound('Could not find node at path: ' . $path);

$node = $parent->getChild($baseName);

if ($node instanceof self) {
$node = $node->getNodeForPath(implode('/', $parts));
break;
} else {
$part = array_shift($parts);
if ($part !== '') $node = $node->getChild($part);
}
}

$this->cache[$path] = $node;
Expand Down Expand Up @@ -176,9 +175,13 @@ function move($sourcePath, $destinationPath) {
* @param string $path
* @return void
*/
function delete($path) {
function delete($path = null) {

$node = $this->getNodeForPath($path);
if ($path === null || $path === '') {
$node = $this->rootNode;
} else {
$node = $this->getNodeForPath($path);
}
$node->delete();

list($parent) = Uri\split($path);
Expand All @@ -189,12 +192,17 @@ function delete($path) {
/**
* Returns a list of childnodes for a given path.
*
* @param string $path
* @param string|null $path
* @return \Traversable
*/
function getChildren($path) {
function getChildren($path = null) {

if ($path === null || $path === '') {
$node = $this->rootNode;
} else {
$node = $this->getNodeForPath($path);
}

$node = $this->getNodeForPath($path);
$basePath = trim($path, '/');
if ($basePath !== '') $basePath .= '/';

Expand Down Expand Up @@ -341,4 +349,32 @@ protected function copyNode(INode $source, ICollection $destinationParent, $dest

}

function createFile($name, $data = null) {
return $this->rootNode->createFile($name, $data);
}

function createDirectory($name) {
$this->rootNode->createDirectory($name);
}

function getChild($name) {
return $this->rootNode->getChild($name);
}

function childExists($name) {
return $this->rootNode->childExists($name);
}

function getName() {
return $this->rootNode->getName();
}

function setName($name) {
$this->rootNode->setName($name);
}

function getLastModified()
{
return $this->rootNode->getLastModified();
}
}
14 changes: 13 additions & 1 deletion tests/Sabre/DAV/TreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ function testGetMultipleNodes2() {

}

function testGetSubTreeNode() {

$tree = new TreeMock();
$this->assertTrue($tree->nodeExists('subtree/sub/1'));

}

}

class TreeMock extends Tree {
Expand All @@ -119,7 +126,12 @@ function __construct() {
new TreeFileTester('1'),
new TreeFileTester('2'),
new TreeFileTester('3'),
])
]),
new Tree(new TreeDirectoryTester('subtree', [
new TreeDirectoryTester('sub', [
new TreeFileTester('1')
]),
]))
])
);

Expand Down