Fixing comments and implementation

This commit is contained in:
Dennis Eichhorn 2016-10-25 20:29:13 +02:00
parent 7898a31c41
commit 808e3e4288
4 changed files with 48 additions and 81 deletions

View File

@ -193,21 +193,6 @@ interface ContainerInterface
*/ */
public static function basename(string $path) : string; public static function basename(string $path) : string;
/**
* Get amount of sub-resources.
*
* A file will always return 1 as it doesn't have any sub-resources.
*
* @param string $path Path of the resource
* @param bool $recursive Should count also sub-sub-resources
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function count(string $path, bool $recursive = false) : int;
/** /**
* Get amount of sub-resources. * Get amount of sub-resources.
* *

View File

@ -30,4 +30,43 @@ namespace phpOMS\System\File;
*/ */
interface DirectoryInterface extends ContainerInterface, \Iterator, \ArrayAccess interface DirectoryInterface extends ContainerInterface, \Iterator, \ArrayAccess
{ {
/**
* Get amount of sub-resources.
*
* A file will always return 1 as it doesn't have any sub-resources.
*
* @param string $path Path of the resource
* @param bool $recursive Should count also sub-sub-resources
* @param array $ignore Ignore files
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public static function count(string $path, bool $recursive = false, array $ignore = []) : int;
/**
* Get node by name.
*
* @param string $name File/direcotry name
*
* @return mixed
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getNode(string $name);
/**
* Add file or directory.
*
* @param mixed $file File to add
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function add($file) : bool;
} }

View File

@ -87,16 +87,9 @@ class Directory extends FileAbstract implements DirectoryInterface
} }
/** /**
* Add file or directory. * {@inheritdoc}
*
* @param FileAbstract $file File to add
*
* @return bool
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function add(FileAbstract $file) : bool public function add($file) : bool
{ {
$this->count += $file->getCount(); $this->count += $file->getCount();
$this->size += $file->getSize(); $this->size += $file->getSize();
@ -106,19 +99,9 @@ class Directory extends FileAbstract implements DirectoryInterface
} }
/** /**
* Get folder size recursively. * {@inheritdoc}
*
* This can become rather slow for large structures.
*
* @param string $dir Root dir to inspect
* @param bool $recursive Get size recursive
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn
*/ */
public static function getFolderSize(string $dir, bool $recursive = true) : int public static function size(string $dir, bool $recursive = true) : int
{ {
$countSize = 0; $countSize = 0;
$count = 0; $count = 0;
@ -129,7 +112,7 @@ class Directory extends FileAbstract implements DirectoryInterface
foreach ($dir_array as $key => $filename) { foreach ($dir_array as $key => $filename) {
if ($filename != ".." && $filename != ".") { if ($filename != ".." && $filename != ".") {
if (is_dir($dir . "/" . $filename) && $recursive) { if (is_dir($dir . "/" . $filename) && $recursive) {
$countSize += self::getFolderSize($dir . "/" . $filename, $recursive); $countSize += self::size($dir . "/" . $filename, $recursive);
} else { } else {
if (is_file($dir . "/" . $filename)) { if (is_file($dir . "/" . $filename)) {
$countSize += filesize($dir . "/" . $filename); $countSize += filesize($dir . "/" . $filename);
@ -144,18 +127,9 @@ class Directory extends FileAbstract implements DirectoryInterface
} }
/** /**
* Get file count inside path. * {@inheritdoc}
*
* @param string $path Path to folder
* @param bool $recursive Should sub folders be counted as well?
* @param array $ignore Ignore these sub-paths
*
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public static function getFileCount(string $path, bool $recursive = true, array $ignore = ['.', '..', 'cgi-bin', public static function count(string $path, bool $recursive = true, array $ignore = ['.', '..', 'cgi-bin',
'.DS_Store']) '.DS_Store'])
{ {
$size = 0; $size = 0;
@ -167,7 +141,7 @@ class Directory extends FileAbstract implements DirectoryInterface
} }
if (is_dir(rtrim($path, '/') . '/' . $t)) { if (is_dir(rtrim($path, '/') . '/' . $t)) {
if ($recursive) { if ($recursive) {
$size += self::getFileCount(rtrim($path, '/') . '/' . $t, true, $ignore); $size += self::count(rtrim($path, '/') . '/' . $t, true, $ignore);
} }
} else { } else {
$size++; $size++;
@ -264,14 +238,6 @@ class Directory extends FileAbstract implements DirectoryInterface
// TODO: Implement move() method. // TODO: Implement move() method.
} }
/**
* {@inheritdoc}
*/
public static function size(string $path, bool $recursive = true) : int
{
// TODO: Implement size() method.
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -281,14 +247,7 @@ class Directory extends FileAbstract implements DirectoryInterface
} }
/** /**
* Get node by name. * {@inheritdoc}
*
* @param string $name File/direcotry name
*
* @return FileAbstract
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function getNode(string $name) : FileAbstract public function getNode(string $name) : FileAbstract
{ {
@ -424,14 +383,6 @@ class Directory extends FileAbstract implements DirectoryInterface
// TODO: Implement basename() method. // TODO: Implement basename() method.
} }
/**
* {@inheritdoc}
*/
public static function count(string $path, bool $recursive = false) : int
{
// TODO: Implement count() method.
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -420,14 +420,6 @@ class File extends FileAbstract implements FileInterface
// TODO: Implement basename() method. // TODO: Implement basename() method.
} }
/**
* {@inheritdoc}
*/
public static function count(string $path, bool $recursive = false) : int
{
// TODO: Implement count() method.
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */