From aaa936ad99031a8bd9a409a06d3eb6a30f09c00d Mon Sep 17 00:00:00 2001
From: Dennis Eichhorn
Date: Sat, 7 Oct 2017 13:03:55 +0200
Subject: [PATCH] Adjust during test implementation
---
System/File/ContainerInterface.php | 2 +-
System/File/Local/Directory.php | 128 +++++++---
System/File/Local/File.php | 25 +-
System/File/Local/LocalStorage.php | 361 +++++------------------------
System/File/StorageAbstract.php | 107 ++++++++-
5 files changed, 282 insertions(+), 341 deletions(-)
diff --git a/System/File/ContainerInterface.php b/System/File/ContainerInterface.php
index ae3457ae5..403f4e1c0 100644
--- a/System/File/ContainerInterface.php
+++ b/System/File/ContainerInterface.php
@@ -70,7 +70,7 @@ interface ContainerInterface
*
* @since 1.0.0
*/
- public static function permission(string $path) : string;
+ public static function permission(string $path) : int;
/**
* Get the parent path of the resource.
diff --git a/System/File/Local/Directory.php b/System/File/Local/Directory.php
index 5288243a0..dce7c641d 100644
--- a/System/File/Local/Directory.php
+++ b/System/File/Local/Directory.php
@@ -68,7 +68,7 @@ class Directory extends FileAbstract implements DirectoryInterface
}
/**
- * List all files in directorz.
+ * List all files in directory.
*
* @param string $path Path
* @param string $filter Filter
@@ -80,31 +80,43 @@ class Directory extends FileAbstract implements DirectoryInterface
public static function list(string $path, string $filter = '*') : array
{
$list = [];
+ $path = rtrim($path, '\\/');
- foreach (glob($path . DIRECTORY_SEPARATOR . $filter) as $filename) {
- $list[] = str_replace(['/\\', '\\'], ['/', '/'], $filename);
+ foreach ($iterator = new \RecursiveIteratorIterator(
+ new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
+ \RecursiveIteratorIterator::SELF_FIRST) as $item
+ ) {
+ $list[] = str_replace('\\', '/', $iterator->getSubPathName());
}
return $list;
}
- public static function listByExtension(string $dir, string $extension) : array
+ /**
+ * List all files by extension directory.
+ *
+ * @param string $path Path
+ * @param string $extension Extension
+ *
+ * @return array
+ *
+ * @since 1.0.0
+ */
+ public static function listByExtension(string $path, string $extension) : array
{
- $files = [];
- $ffs = scandir($dir);
- foreach ($ffs as $ff) {
- if ($ff !== '.' && $ff !== '..') {
- if (is_dir($dir . '/' . $ff)) {
- $files = array_merge($files, self::listFilesByExtension($dir . '/' . $ff, $extension));
- } else {
- if (StringUtils::endsWith($ff, $extension)) {
- $files[] = $dir . '/' . $ff;
- }
- }
+ $list = [];
+ $path = rtrim($path, '\\/');
+
+ foreach ($iterator = new \RecursiveIteratorIterator(
+ new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
+ \RecursiveIteratorIterator::SELF_FIRST) as $item
+ ) {
+ if($item->getExtension() === $extension) {
+ $list[] = str_replace('\\', '/', $iterator->getSubPathName());
}
}
-
- return $files;
+
+ return $list;
}
/**
@@ -204,10 +216,10 @@ class Directory extends FileAbstract implements DirectoryInterface
unset($files[0]);
foreach ($files as $file) {
- if (is_dir($file)) {
- self::delete($file);
+ if (is_dir($path . '/' . $file)) {
+ self::delete($path . '/' . $file);
} else {
- unlink($file);
+ unlink($path . '/' . $file);
}
}
@@ -251,7 +263,14 @@ class Directory extends FileAbstract implements DirectoryInterface
*/
public static function changed(string $path) : \DateTime
{
- // TODO: Implement changed() method.
+ if (!file_exists($path)) {
+ throw new PathException($path);
+ }
+
+ $changed = new \DateTime();
+ $changed->setTimestamp(filectime($path));
+
+ return $changed;
}
/**
@@ -259,15 +278,23 @@ class Directory extends FileAbstract implements DirectoryInterface
*/
public static function owner(string $path) : int
{
- // TODO: Implement owner() method.
+ if (!file_exists($path)) {
+ throw new PathException($path);
+ }
+
+ return fileowner($path);
}
/**
* {@inheritdoc}
*/
- public static function permission(string $path) : string
+ public static function permission(string $path) : int
{
- // TODO: Implement permission() method.
+ if (!file_exists($path)) {
+ throw new PathException($path);
+ }
+
+ return fileperms($path);
}
/**
@@ -275,7 +302,26 @@ class Directory extends FileAbstract implements DirectoryInterface
*/
public static function copy(string $from, string $to, bool $overwrite = false) : bool
{
- // TODO: Implement copy() method.
+ if(!$overwrite && file_exists($to)) {
+ return false;
+ }
+
+ if (!file_exists($to)) {
+ self::create($to, 0644, true);
+ }
+
+ foreach ($iterator = new \RecursiveIteratorIterator(
+ new \RecursiveDirectoryIterator($from, \RecursiveDirectoryIterator::SKIP_DOTS),
+ \RecursiveIteratorIterator::SELF_FIRST) as $item
+ ) {
+ if ($item->isDir()) {
+ mkdir($to . '/' . $iterator->getSubPathName());
+ } else {
+ copy($from . '/' . $iterator->getSubPathName(), $to . '/' . $iterator->getSubPathName());
+ }
+ }
+
+ return true;
}
/**
@@ -283,7 +329,17 @@ class Directory extends FileAbstract implements DirectoryInterface
*/
public static function move(string $from, string $to, bool $overwrite = false) : bool
{
- // TODO: Implement move() method.
+ if (!$overwrite && file_exists($to)) {
+ return false;
+ }
+
+ if (!self::exists(self::parent($to))) {
+ self::create(self::parent($to), 0644, true);
+ }
+
+ rename($from, $to);
+
+ return true;
}
/**
@@ -326,6 +382,10 @@ class Directory extends FileAbstract implements DirectoryInterface
public static function create(string $path, int $permission = 0644, bool $recursive = false) : bool
{
if (!file_exists($path)) {
+ if(!$recursive && !file_exists(self::parent($path))) {
+ return false;
+ }
+
mkdir($path, $permission, $recursive);
return true;
@@ -433,6 +493,22 @@ class Directory extends FileAbstract implements DirectoryInterface
return basename($path);
}
+ /**
+ * {@inheritdoc}
+ */
+ public static function dirname(string $path) : string
+ {
+ return basename($path);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function dirpath(string $path) : string
+ {
+ return $path;
+ }
+
/**
* {@inheritdoc}
*/
diff --git a/System/File/Local/File.php b/System/File/Local/File.php
index 2da086631..65f49c4a7 100644
--- a/System/File/Local/File.php
+++ b/System/File/Local/File.php
@@ -66,7 +66,6 @@ class File extends FileAbstract implements FileInterface
*/
public static function put(string $path, string $content, int $mode = ContentPutMode::REPLACE | ContentPutMode::CREATE) : bool
{
- // todo: create all else cases, right now all getting handled the same way which is wrong
$exists = file_exists($path);
if (
@@ -75,11 +74,17 @@ class File extends FileAbstract implements FileInterface
|| (($mode & ContentPutMode::REPLACE) === ContentPutMode::REPLACE && $exists)
|| (!$exists && ($mode & ContentPutMode::CREATE) === ContentPutMode::CREATE)
) {
- if (!Directory::exists(dirname($path))) {
- Directory::create(dirname($path), 0644, true);
- }
+ if(($mode & ContentPutMode::APPEND) === ContentPutMode::APPEND && $exists) {
+ file_put_contents($path, file_get_contents($path) . $content);
+ } elseif(($mode & ContentPutMode::PREPEND) === ContentPutMode::PREPEND && $exists) {
+ file_put_contents($path, $content . file_get_contents($path));
+ } else {
+ if (!Directory::exists(dirname($path))) {
+ Directory::create(dirname($path), 0644, true);
+ }
- file_put_contents($path, $content);
+ file_put_contents($path, $content);
+ }
return true;
}
@@ -99,6 +104,14 @@ class File extends FileAbstract implements FileInterface
return file_get_contents($path);
}
+ /**
+ * {@inheritdoc}
+ */
+ public static function count(string $path) : int
+ {
+ return 1;
+ }
+
/**
* {@inheritdoc}
*/
@@ -204,7 +217,7 @@ class File extends FileAbstract implements FileInterface
/**
* {@inheritdoc}
*/
- public static function permission(string $path) : string
+ public static function permission(string $path) : int
{
if (!file_exists($path)) {
throw new PathException($path);
diff --git a/System/File/Local/LocalStorage.php b/System/File/Local/LocalStorage.php
index 6604da447..572685838 100644
--- a/System/File/Local/LocalStorage.php
+++ b/System/File/Local/LocalStorage.php
@@ -45,12 +45,17 @@ class LocalStorage extends StorageAbstract
return self::$instance;
}
+ private static function getClassType(string $path) : string
+ {
+ return is_dir($path) ? Directory::class : File::class;
+ }
+
/**
* {@inheritdoc}
*/
public static function created(string $path) : \DateTime
{
- // TODO: Implement created() method.
+ return self::getClassType($path)::created($path);
}
/**
@@ -58,7 +63,7 @@ class LocalStorage extends StorageAbstract
*/
public static function changed(string $path) : \DateTime
{
- // TODO: Implement changed() method.
+ return self::getClassType($path)::changed($path);
}
/**
@@ -66,7 +71,7 @@ class LocalStorage extends StorageAbstract
*/
public static function owner(string $path) : int
{
- // TODO: Implement owner() method.
+ return self::getClassType($path)::owner($path);
}
/**
@@ -74,7 +79,7 @@ class LocalStorage extends StorageAbstract
*/
public static function permission(string $path) : string
{
- // TODO: Implement permission() method.
+ return self::getClassType($path)::permission($path);
}
/**
@@ -82,7 +87,7 @@ class LocalStorage extends StorageAbstract
*/
public static function parent(string $path) : string
{
- // TODO: Implement parent() method.
+ return self::getClassType($path)::parent($path);
}
/**
@@ -90,7 +95,7 @@ class LocalStorage extends StorageAbstract
*/
public static function create(string $path) : bool
{
- // TODO: Implement create() method.
+ return self::getClassType($path)::create($path);
}
/**
@@ -98,7 +103,7 @@ class LocalStorage extends StorageAbstract
*/
public static function delete(string $path) : bool
{
- // TODO: Implement delete() method.
+ return self::getClassType($path)::delete($path);
}
/**
@@ -106,7 +111,7 @@ class LocalStorage extends StorageAbstract
*/
public static function copy(string $from, string $to, bool $overwrite = false) : bool
{
- // TODO: Implement copy() method.
+ return self::getClassType($from)::copy($from, $to, $overwrite);
}
/**
@@ -114,7 +119,7 @@ class LocalStorage extends StorageAbstract
*/
public static function move(string $from, string $to, bool $overwrite = false) : bool
{
- // TODO: Implement move() method.
+ return self::getClassType($from)::move($from, $to, $overwrite);
}
/**
@@ -122,7 +127,7 @@ class LocalStorage extends StorageAbstract
*/
public static function size(string $path, bool $recursive = true) : int
{
- // TODO: Implement size() method.
+ return self::getClassType($path)::size($path, $recursive);
}
/**
@@ -130,7 +135,7 @@ class LocalStorage extends StorageAbstract
*/
public static function exists(string $path) : bool
{
- // TODO: Implement exists() method.
+ return self::getClassType($path)::exists($path);
}
/**
@@ -138,7 +143,7 @@ class LocalStorage extends StorageAbstract
*/
public static function name(string $path) : string
{
- // TODO: Implement name() method.
+ return self::getClassType($path)::name($path);
}
/**
@@ -146,7 +151,7 @@ class LocalStorage extends StorageAbstract
*/
public static function basename(string $path) : string
{
- // TODO: Implement basename() method.
+ return self::getClassType($path)::basename($path);
}
/**
@@ -154,237 +159,7 @@ class LocalStorage extends StorageAbstract
*/
public static function count(string $path, bool $recursive = true, array $ignore = []) : int
{
- // TODO: Implement count() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getCount(bool $recursive = false) : int
- {
- // TODO: Implement getCount() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getSize(bool $recursive = false) : int
- {
- // TODO: Implement getSize() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getName() : string
- {
- // TODO: Implement getName() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getPath() : string
- {
- // TODO: Implement getPath() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getParent() : ContainerInterface
- {
- // TODO: Implement getParent() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function createNode() : bool
- {
- // TODO: Implement createNode() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function copyNode(string $to, bool $overwrite = false) : bool
- {
- // TODO: Implement copyNode() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function moveNode(string $to, bool $overwrite = false) : bool
- {
- // TODO: Implement moveNode() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function deleteNode() : bool
- {
- // TODO: Implement deleteNode() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getCreatedAt() : \DateTime
- {
- // TODO: Implement getCreatedAt() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getChangedAt() : \DateTime
- {
- // TODO: Implement getChangedAt() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getOwner() : int
- {
- // TODO: Implement getOwner() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getPermission() : string
- {
- // TODO: Implement getPermission() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function index()
- {
- // TODO: Implement index() method.
- }
-
- /**
- * Return the current element
- * @link http://php.net/manual/en/iterator.current.php
- * @return mixed Can return any type.
- * @since 5.0.0
- */
- public function current()
- {
- // TODO: Implement current() method.
- }
-
- /**
- * Move forward to next element
- * @link http://php.net/manual/en/iterator.next.php
- * @return void Any returned value is ignored.
- * @since 5.0.0
- */
- public function next()
- {
- // TODO: Implement next() method.
- }
-
- /**
- * Return the key of the current element
- * @link http://php.net/manual/en/iterator.key.php
- * @return mixed scalar on success, or null on failure.
- * @since 5.0.0
- */
- public function key()
- {
- // TODO: Implement key() method.
- }
-
- /**
- * Checks if current position is valid
- * @link http://php.net/manual/en/iterator.valid.php
- * @return boolean The return value will be casted to boolean and then evaluated.
- * Returns true on success or false on failure.
- * @since 5.0.0
- */
- public function valid()
- {
- // TODO: Implement valid() method.
- }
-
- /**
- * Rewind the Iterator to the first element
- * @link http://php.net/manual/en/iterator.rewind.php
- * @return void Any returned value is ignored.
- * @since 5.0.0
- */
- public function rewind()
- {
- // TODO: Implement rewind() method.
- }
-
- /**
- * Whether a offset exists
- * @link http://php.net/manual/en/arrayaccess.offsetexists.php
- * @param mixed $offset
- * An offset to check for.
- *
- * @return boolean true on success or false on failure.
- *
- *
- * The return value will be casted to boolean if non-boolean was returned.
- * @since 5.0.0
- */
- public function offsetExists($offset)
- {
- // TODO: Implement offsetExists() method.
- }
-
- /**
- * Offset to retrieve
- * @link http://php.net/manual/en/arrayaccess.offsetget.php
- * @param mixed $offset
- * The offset to retrieve.
- *
- * @return mixed Can return all value types.
- * @since 5.0.0
- */
- public function offsetGet($offset)
- {
- // TODO: Implement offsetGet() method.
- }
-
- /**
- * Offset to set
- * @link http://php.net/manual/en/arrayaccess.offsetset.php
- * @param mixed $offset
- * The offset to assign the value to.
- *
- * @param mixed $value
- * The value to set.
- *
- * @return void
- * @since 5.0.0
- */
- public function offsetSet($offset, $value)
- {
- // TODO: Implement offsetSet() method.
- }
-
- /**
- * Offset to unset
- * @link http://php.net/manual/en/arrayaccess.offsetunset.php
- * @param mixed $offset
- * The offset to unset.
- *
- * @return void
- * @since 5.0.0
- */
- public function offsetUnset($offset)
- {
- // TODO: Implement offsetUnset() method.
+ return self::getClassType($path)::count($path, $recursive, $ignore);
}
/**
@@ -392,7 +167,11 @@ class LocalStorage extends StorageAbstract
*/
public static function put(string $path, string $content, int $mode = 0) : bool
{
- // TODO: Implement put() method.
+ if(is_dir($path)) {
+ throw new \Exception();
+ }
+
+ return File::put($path, $content, $mode);
}
/**
@@ -400,23 +179,23 @@ class LocalStorage extends StorageAbstract
*/
public static function get(string $path) : string
{
- // TODO: Implement get() method.
+ if(is_dir($path)) {
+ throw new \Exception();
+ }
+
+ return File::get($path);
}
/**
* {@inheritdoc}
*/
- public function putContent(string $content, int $mode = 0) : bool
+ public static function list(string $path, string $filter = '*') : string
{
- // TODO: Implement putContent() method.
- }
+ if(is_file($path)) {
+ throw new \Exception();
+ }
- /**
- * {@inheritdoc}
- */
- public function getContent() : string
- {
- // TODO: Implement getContent() method.
+ return Directory::list($path, $filter);
}
/**
@@ -424,23 +203,7 @@ class LocalStorage extends StorageAbstract
*/
public static function sanitize(string $path, string $replace = '') : string
{
- // TODO: Implement sanitize() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getNode(string $name)
- {
- // TODO: Implement getNode() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function addNode($file) : bool
- {
- // TODO: Implement addNode() method.
+ return self::getClassType($path)::sanitize($path, $replace);
}
/**
@@ -448,7 +211,11 @@ class LocalStorage extends StorageAbstract
*/
public static function set(string $path, string $content) : bool
{
- // TODO: Implement set() method.
+ if(is_dir($path)) {
+ throw new \Exception();
+ }
+
+ return File::set_socket_blocking($path, $content);
}
/**
@@ -456,7 +223,11 @@ class LocalStorage extends StorageAbstract
*/
public static function append(string $path, string $content) : bool
{
- // TODO: Implement append() method.
+ if(is_dir($path)) {
+ throw new \Exception();
+ }
+
+ return File::append($path, $content);
}
/**
@@ -464,7 +235,11 @@ class LocalStorage extends StorageAbstract
*/
public static function prepend(string $path, string $content) : bool
{
- // TODO: Implement prepend() method.
+ if(is_dir($path)) {
+ throw new \Exception();
+ }
+
+ return File::prepend($path, $content);
}
/**
@@ -472,38 +247,10 @@ class LocalStorage extends StorageAbstract
*/
public static function extension(string $path) : string
{
- // TODO: Implement extension() method.
- }
+ if(is_dir($path)) {
+ throw new \Exception();
+ }
- /**
- * {@inheritdoc}
- */
- public function setContent(string $content) : bool
- {
- // TODO: Implement setContent() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function appendContent(string $content) : bool
- {
- // TODO: Implement appendContent() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function prependContent(string $content) : bool
- {
- // TODO: Implement prependContent() method.
- }
-
- /**
- * {@inheritdoc}
- */
- public function getExtension() : string
- {
- // TODO: Implement getExtension() method.
+ return File::extension($path, $content);
}
}
\ No newline at end of file
diff --git a/System/File/StorageAbstract.php b/System/File/StorageAbstract.php
index 1445cd7b4..b6ae436ec 100644
--- a/System/File/StorageAbstract.php
+++ b/System/File/StorageAbstract.php
@@ -26,7 +26,7 @@ namespace phpOMS\System\File;
* @link http://orange-management.com
* @since 1.0.0
*/
-abstract class StorageAbstract implements DirectoryInterface, FileInterface
+abstract class StorageAbstract
{
/**
* Storage type.
@@ -67,4 +67,109 @@ abstract class StorageAbstract implements DirectoryInterface, FileInterface
{
return $this->type;
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function created(string $path) : \DateTime;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function changed(string $path) : \DateTime;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function owner(string $path) : int;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function permission(string $path) : string;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function parent(string $path) : string;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function create(string $path) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function delete(string $path) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function copy(string $from, string $to, bool $overwrite = false) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function move(string $from, string $to, bool $overwrite = false) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function size(string $path, bool $recursive = true) : int;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function exists(string $path) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function name(string $path) : string;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function basename(string $path) : string;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function count(string $path, bool $recursive = true, array $ignore = []) : int;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function put(string $path, string $content, int $mode = 0) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function get(string $path) : string;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function sanitize(string $path, string $replace = '') : string;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function set(string $path, string $content) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function append(string $path, string $content) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function prepend(string $path, string $content) : bool;
+
+ /**
+ * {@inheritdoc}
+ */
+ public abstract static function extension(string $path) : string;
}