Fixing writable check and interface bugs

This commit is contained in:
Dennis Eichhorn 2016-03-27 15:08:17 +02:00
parent 3afa428a31
commit 08d06200d2
3 changed files with 37 additions and 19 deletions

View File

@ -13,7 +13,7 @@
* @version 1.0.0 * @version 1.0.0
* @link http://orange-management.com * @link http://orange-management.com
*/ */
namespace phpOMS\System; namespace phpOMS\System\File;
/** /**
* Filesystem class. * Filesystem class.
@ -28,19 +28,24 @@ namespace phpOMS\System;
* @link http://orange-management.com * @link http://orange-management.com
* @since 1.0.0 * @since 1.0.0
*/ */
class Directory extends FileAbstract implements Iterator, ArrayAccess class Directory extends FileAbstract implements \Iterator, \ArrayAccess
{ {
private $filter = '*'; private $filter = '*';
private $nodes = []; private $nodes = [];
public static function create(string $path, $permission = 0644) : bool public static function create(string $path, int $permission = 0644, bool $recursive = false) : bool
{ {
if($recursive && !file_exists($parent = self::getParent($path))) {
self::create($parent, $permission, $recursive);
}
if (!file_exists($path)) { if (!file_exists($path)) {
if(is_writable($path)) { if(is_writable(self::getParent($path))) {
mkdir($path, $permission, true); mkdir($path, $permission, true);
return true; return true;
} else { } else {
throw new PathException($path); throw new PathException($path);
} }
} }
@ -48,6 +53,14 @@ class Directory extends FileAbstract implements Iterator, ArrayAccess
return false; return false;
} }
public static function getParent(string $path) : string
{
$path = explode('/', str_replace('\\', '/', $path));
array_pop($path);
return implode('/', $path);
}
public function __construct(string $path, string $filter = '*') public function __construct(string $path, string $filter = '*')
{ {
$this->filter = $filter; $this->filter = $filter;
@ -105,55 +118,55 @@ class Directory extends FileAbstract implements Iterator, ArrayAccess
/* Iterator */ /* Iterator */
public function rewind() public function rewind()
{ {
reset($this->node); reset($this->nodes);
} }
public function current() public function current()
{ {
return current($this->node); return current($this->nodes);
} }
public function key() public function key()
{ {
return key($this->node); return key($this->nodes);
} }
public function next() public function next()
{ {
return next($this->node); return next($this->nodes);
} }
public function valid() public function valid()
{ {
$key = key($this->node); $key = key($this->nodes);
return ($key !== null && $key !== false); return ($key !== null && $key !== false);
} }
/* ArrayAccess */ /* ArrayAccess */
public function offsetSet(string $offset, FileInterface $value) public function offsetSet($offset, $value)
{ {
if (is_null($offset)) { if (is_null($offset)) {
$this->add($value); $this->add($value);
} else { } else {
$this->node[$offset] = $value; $this->nodes[$offset] = $value;
} }
} }
public function offsetExists(string $offset) public function offsetExists($offset)
{ {
return isset($this->node[$offset]); return isset($this->nodes[$offset]);
} }
public function offsetUnset(string $offset) public function offsetUnset($offset)
{ {
if(isset($this->node[$offset])) { if(isset($this->nodes[$offset])) {
unset($this->node[$offset]); unset($this->nodes[$offset]);
} }
} }
public function offsetGet(string $offset) public function offsetGet($offset)
{ {
return $this->node[$offset] ?? null; return $this->nodes[$offset] ?? null;
} }
} }

View File

@ -34,7 +34,7 @@ class File extends FileAbstract
public static function create(string $path) : bool public static function create(string $path) : bool
{ {
if (!file_exists($path)) { if (!file_exists($path)) {
if(is_writable($path)) { if(is_writable(Directory::getParent($path))) {
touch($path); touch($path);
return true; return true;

View File

@ -68,6 +68,11 @@ abstract class FileAbstract
return $this->path; return $this->path;
} }
public function parent() : Directory
{
return new Directory(Directory::getParent($this->path));
}
public function getCreatedAt() : \DateTime public function getCreatedAt() : \DateTime
{ {
return $this->createdAt; return $this->createdAt;