Skip to content

Commit

Permalink
another patch of scru fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Donat committed May 11, 2014
1 parent f46fc31 commit e42d2f5
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 188 deletions.
57 changes: 37 additions & 20 deletions src/VirtualFileSystem/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function root()
*
* @throws NotFoundException
*/
public function fileAt($path)
public function nodeAt($path)
{
$pathParts = array_filter(explode('/', str_replace('\\', '/', $path)), 'strlen');

Expand All @@ -115,10 +115,10 @@ public function fileAt($path)
*
* @return bool
*/
public function hasFileAt($path)
public function hasNodeAt($path)
{
try {
$this->fileAt($path);
$this->nodeAt($path);

return true;
} catch (NotFoundException $e) {
Expand All @@ -138,7 +138,7 @@ public function hasFileAt($path)
*/
public function directoryAt($path)
{
$file = $this->fileAt($path);
$file = $this->nodeAt($path);

if (!$file instanceof Directory) {
throw new NotDirectoryException();
Expand All @@ -147,6 +147,27 @@ public function directoryAt($path)
return $file;
}

/**
* Returns file at given path
*
* @param string $path
*
* @return Structure\File
*
* @throws NotFileException
* @throws NotFoundException
*/
public function fileAt($path)
{
$file = $this->nodeAt($path);

if (!$file instanceof File) {
throw new NotFileException();
}

return $file;
}

/**
* Creates Directory at given path.
*
Expand Down Expand Up @@ -193,16 +214,16 @@ public function createDir($path, $recursive = false, $mode = null)
public function createLink($path, $destination)
{

$destination = $this->fileAt($destination);
$destination = $this->nodeAt($destination);

try {
$file = $this->fileAt($path);
$this->nodeAt($path);
throw new \RuntimeException(sprintf('%s already exists', $path));
} catch (NotFoundException $e) {

}

$parent = $this->fileAt(dirname($path));
$parent = $this->directoryAt(dirname($path));

$parent->addLink($newLink = $this->factory()->getLink(basename($path), $destination));

Expand All @@ -222,7 +243,7 @@ public function createLink($path, $destination)
*/
public function createFile($path, $data = null)
{
if ($this->hasFileAt($path)) {
if ($this->hasNodeAt($path)) {
throw new \RuntimeException(sprintf('%s already exists', $path));
}

Expand Down Expand Up @@ -263,18 +284,18 @@ public function createStructure(array $structure, $parent = '/')
*/
public function move($fromPath, $toPath)
{
$fromNode = $this->fileAt($fromPath);
$fromNode = $this->nodeAt($fromPath);

try {
$nodeToOverride = $this->fileAt($toPath);
$nodeToOverride = $this->nodeAt($toPath);

if(!is_a($nodeToOverride, get_class($fromNode))) {
if (!is_a($nodeToOverride, get_class($fromNode))) {
//nodes of a different type
throw new \RuntimeException('Can\'t move.');
}

if($nodeToOverride instanceof Directory) {
if($nodeToOverride->size()) {
if ($nodeToOverride instanceof Directory) {
if ($nodeToOverride->size()) {
//nodes of a different type
throw new \RuntimeException('Can\'t override non empty directory.');
}
Expand All @@ -283,18 +304,14 @@ public function move($fromPath, $toPath)
$this->remove($toPath, true);

} catch (NotFoundException $e) {

//nothing at destination, we're good
}

$toParent = $this->directoryAt(dirname($toPath));

$fromNode->setBasename(basename($toPath));

if ($fromNode instanceof File) {
$toParent->addFile($fromNode);
} else {
$toParent->addDirectory($fromNode);
}
$toParent->addNode($fromNode);

$this->remove($fromPath, true);

Expand All @@ -311,7 +328,7 @@ public function move($fromPath, $toPath)
*/
public function remove($path, $recursive = false)
{
$fileToRemove = $this->fileAt($path);
$fileToRemove = $this->nodeAt($path);

if (!$recursive && $fileToRemove instanceof Directory) {
throw new \RuntimeException('Won\'t non-recursively remove directory');
Expand Down
21 changes: 21 additions & 0 deletions src/VirtualFileSystem/NotFileException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/*
* This file is part of the php-vfs package.
*
* (c) Michael Donat <michael.donat@me.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace VirtualFileSystem;

/**
* Thrown when non-existing Node is requested.
*
* @author Michael Donat <michael.donat@me.com>
* @package php-vfs
*/
class NotFileException extends \Exception
{
}
2 changes: 1 addition & 1 deletion src/VirtualFileSystem/Structure/Directory.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function addLink(Link $link)
*
* @throws FileExistsException
*/
protected function addNode(Node $node)
public function addNode(Node $node)
{
if (array_key_exists($node->basename(), $this->children)) {
throw new FileExistsException(sprintf('%s already exists', $node->basename()));
Expand Down

0 comments on commit e42d2f5

Please sign in to comment.