From d050e298204b3347e9b32138904a95f4b55a0c3e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 17 Aug 2016 12:55:26 +0200 Subject: [PATCH 1/5] getArg trim and comment added --- Utils/ArrayUtils.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index 10f8a8eba..b9d847c49 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -222,12 +222,27 @@ class ArrayUtils return $csv; } + /** + * Get array value by argument id. + * + * Useful for parsing command line parsing + * + * @param array $data Data to convert + * @param string $delimiter Delim to use + * @param string $enclosure Enclosure to use + * @param string $escape Escape to use + * + * @return string + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ public static function getArg(string $id, array $args) { if(($key = array_search($id, $args)) === false || $key === count($args) - 1) { return null; } - return $args[$key+1]; + return trim($args[$key+1], '" '); } } From c3d6da2539e4960df3cb4b4f6ba1a590b6cd033e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 18 Aug 2016 11:28:06 +0200 Subject: [PATCH 2/5] Add file interface This will be used for the filestorage class that needs to be implemented. --- System/File/FileInterface.php | 86 +++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 System/File/FileInterface.php diff --git a/System/File/FileInterface.php b/System/File/FileInterface.php new file mode 100644 index 000000000..20d86cc16 --- /dev/null +++ b/System/File/FileInterface.php @@ -0,0 +1,86 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\System\File; + +/** + * Filesystem class. + * + * Performing operations on the file system + * + * @category Framework + * @package phpOMS\System\File + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +interface FileInterface +{ + public function getCount() : int; + + public function getSize() : int; + + public function getName() : string; + + public function getPath() : string; + + public function getParent() : FileInterface; + + public function createNode() : bool; + + public function copyNode() : bool; + + public function moveNode() : bool; + + public function deleteNode() : bool; + + public function putContent() : bool; + + public function getContent() : string; + + public function getCreatedAt() : \DateTime; + + public function getChangedAt() : \DateTime; + + public function getOwner() : int; + + public function getPermission() : string; + + public function index(); + + public static function createdAt() : \DateTime; + + public static function changedAt() : \DateTime; + + public static function owner() : int; + + public static function permission() : string; + + public static function parent() : string; + + public static function create() : bool; + + public static function delete() : bool; + + public static function copy() : bool; + + public static function move() : bool; + + public static function put() : bool; + + public static function get() : string; +} From b3e689e48191d965ae949bccfb3a2418eb68bd1d Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 19 Aug 2016 13:07:32 +0200 Subject: [PATCH 3/5] Started to extend file handling --- Log/FileLogger.php | 2 +- System/File/Directory.php | 2 +- System/File/File.php | 181 +++++++++++++++++++++++++++++----- System/File/FileAbstract.php | 2 +- System/File/FileInterface.php | 26 ++--- 5 files changed, 173 insertions(+), 40 deletions(-) diff --git a/Log/FileLogger.php b/Log/FileLogger.php index 70cfba3a9..3095c4402 100644 --- a/Log/FileLogger.php +++ b/Log/FileLogger.php @@ -104,7 +104,7 @@ class FileLogger implements LoggerInterface } if (is_dir($lpath) || strpos($lpath, '.') === false) { - Directory::createPath($lpath, '0644'); + Directory::create($lpath, '0644'); File::createFile($path = $lpath . '/' . date('Y-m-d') . '.log'); $path = realpath($path); diff --git a/System/File/Directory.php b/System/File/Directory.php index 1ce556cd9..fcd764e05 100644 --- a/System/File/Directory.php +++ b/System/File/Directory.php @@ -250,7 +250,7 @@ class Directory extends FileAbstract implements \Iterator, \ArrayAccess * @since 1.0.0 * @author Dennis Eichhorn */ - public static function createPath(string $path, string $permission = '0644', bool $recursive = false) : bool + public static function create(string $path, string $permission = '0644', bool $recursive = false) : bool { if ($recursive && !file_exists($parent = self::getParent($path))) { self::createPath($parent, $permission, $recursive); diff --git a/System/File/File.php b/System/File/File.php index 3d2e4732b..84dfb211b 100644 --- a/System/File/File.php +++ b/System/File/File.php @@ -69,32 +69,7 @@ class File extends FileAbstract */ public function createNode() : bool { - return self::createFile($this->path); - } - - /** - * Create file. - * - * @param string $path Path - * - * @return bool - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - public static function createFile(string $path) : bool - { - if (!file_exists($path)) { - if (is_writable(Directory::getParent($path))) { - touch($path); - - return true; - } else { - throw new PermissionException($path); - } - } - - return false; + return self::create($this->path); } /** @@ -122,4 +97,158 @@ class File extends FileAbstract { file_put_contents($this->path, $content); } + + public static function put(string $path, string $content, bool $overwrite = true) : bool + { + if($overwrite || !file_exists($path)) { + if(!Directory::exists(dirname($path))) { + Directory::create(dirname($path), '0644', true); + } + + file_put_contents($path, $content); + + return true; + } + + return false; + } + + public static function get(string $path) : string + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return file_get_contents($path); + } + + public static function exists(string $path) : bool + { + return file_exists($path); + } + + public static function create(string $path) : string + { + if(!file_exists($path)) { + if(!Directory::exists(dirname($path))) { + Directory::create(dirname($path), '0644', true); + } + + touch($path); + + return true; + } + + return false; + } + + public static function parent(string $path) : string + { + return Directory::parent(dirname($path)); + } + + public static function created(string $path) : \DateTime + { + if(!file_exists($path)) { + throw new PathException($path); + } + + $created = new \DateTime(); + $created->setTimestamp(filemtime($path)); + + return $created; + } + + public static function changed(string $path) : \DateTime + { + if(!file_exists($path)) { + throw new PathException($path); + } + + $changed = new \DateTime(); + $changed->setTimestamp(filectime($path)); + + return $changed; + } + + public static function size(string $path) : int + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return filesize($path); + } + + public static function owner(string $path) : int + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return fileperms($path); + } + + public static function permission(string $path) : int + { + if(!file_exists($path)) { + throw new PathException($path); + } + + return fileowner($path); + } + + public static function dirname(string $path) : string + { + return dirname($path); + } + + public static function copy(string $from, string $to, bool $overwrite = false) : bool + { + if(!file_exists($from)) { + throw new PathException($from); + } + + if($overwrite || !file_exists($to)) { + if(!Directory::exists(dirname($to))) { + Directory::create(dirname($to), '0644', true); + } + + copy($from, $to); + + return true; + } + + return false; + } + + public static function move(string $from, string $to, bool $overwrite = false) : bool + { + if(!file_exists($from)) { + throw new PathException($from); + } + + if($overwrite || !file_exists($to)) { + if(!Directory::exists(dirname($to))) { + Directory::create(dirname($to), '0644', true); + } + + rename($from, $to); + + return true; + } + + return false; + } + + public static function delete(string $path) : bool + { + if(!file_exists($path)) { + return false; + } + + unlink($path); + + return true; + } } \ No newline at end of file diff --git a/System/File/FileAbstract.php b/System/File/FileAbstract.php index 96a3a184e..d157336ae 100644 --- a/System/File/FileAbstract.php +++ b/System/File/FileAbstract.php @@ -28,7 +28,7 @@ namespace phpOMS\System\File; * @link http://orange-management.com * @since 1.0.0 */ -abstract class FileAbstract +abstract class FileAbstract implements FileInterface { /** * Path. diff --git a/System/File/FileInterface.php b/System/File/FileInterface.php index 20d86cc16..b2425bb11 100644 --- a/System/File/FileInterface.php +++ b/System/File/FileInterface.php @@ -62,25 +62,29 @@ interface FileInterface public function index(); - public static function createdAt() : \DateTime; + public static function created(string $path) : \DateTime; - public static function changedAt() : \DateTime; + public static function changed(string $path) : \DateTime; - public static function owner() : int; + public static function owner(string $path) : int; - public static function permission() : string; + public static function permission(string $path) : string; - public static function parent() : string; + public static function parent(string $path) : string; - public static function create() : bool; + public static function create(string $path) : bool; - public static function delete() : bool; + public static function delete(string $path) : bool; - public static function copy() : bool; + public static function copy(string $from, string $to, bool $overwrite = false) : bool; - public static function move() : bool; + public static function move(string $from, string $to, bool $overwrite = false) : bool; - public static function put() : bool; + public static function put(string $path, string $content, bool $overwrite = true) : bool; - public static function get() : string; + public static function get(string $path) : string; + + public static function size(string $path) : string; + + public static function exists(string $path) : bool; } From 54c126b72bf61c5175acca107eb9a09687ddaaf6 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 19 Aug 2016 13:19:45 +0200 Subject: [PATCH 4/5] fixes #61 --- Router/RouteVerb.php | 6 +++--- Router/Router.php | 15 ++++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/Router/RouteVerb.php b/Router/RouteVerb.php index 386a8f763..37de149e0 100644 --- a/Router/RouteVerb.php +++ b/Router/RouteVerb.php @@ -32,7 +32,7 @@ abstract class RouteVerb extends Enum { const GET = 1; const PUT = 2; - const SET = 3; - const DELETE = 4; - const ANY = 5; + const SET = 4; + const DELETE = 8; + const ANY = 16; } diff --git a/Router/Router.php b/Router/Router.php index ed529f03d..759a753f6 100644 --- a/Router/Router.php +++ b/Router/Router.php @@ -76,14 +76,14 @@ class Router * * @param string $route Route regex * @param mixed $destination Destination e.g. Module:function & verb - * @param string $verb Request verb + * @param int $verb Request verb * * @return void * * @since 1.0.0 * @author Dennis Eichhorn */ - public function add(string $route, $destination, string $verb = RouteVerb::GET) + public function add(string $route, $destination, int $verb = RouteVerb::GET) { if(!isset($this->routes[$route])) { $this->routes[$route] = []; @@ -99,6 +99,7 @@ class Router * Route request. * * @param RequestAbstract $request Request to route + * @param int $verb Route verb * * @return string[] * @@ -107,7 +108,7 @@ class Router * @since 1.0.0 * @author Dennis Eichhorn */ - public function route($request, string $verb = RouteVerb::GET) : array + public function route($request, int $verb = RouteVerb::GET) : array { if($request instanceof RequestAbstract) { $uri = $request->getUri(); @@ -134,17 +135,17 @@ class Router * Match route and uri. * * @param string $route Route - * @param string $routeVerb GET,POST for this route + * @param int $routeVerb GET,POST for this route * @param string $uri Uri - * @param string $remoteVerb Verb this request is using + * @param int $remoteVerb Verb this request is using * * @return bool * * @since 1.0.0 * @author Dennis Eichhorn */ - private function match(string $route, string $routeVerb, string $uri, string $remoteVerb = RouteVerb::GET) : bool + private function match(string $route, int $routeVerb, string $uri, int $remoteVerb = RouteVerb::GET) : bool { - return (bool) preg_match('~^' . $route . '$~', $uri) && ($routeVerb == RouteVerb::ANY || $remoteVerb == $routeVerb); + return (bool) preg_match('~^' . $route . '$~', $uri) && ($routeVerb == RouteVerb::ANY || $remoteVerb & $routeVerb === $remoteVerb); } } From 2ead4010b311e9ec68c388585b0d4c8ce93d4ce7 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 19 Aug 2016 13:25:53 +0200 Subject: [PATCH 5/5] fixes #64 --- Message/Http/Request.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Message/Http/Request.php b/Message/Http/Request.php index 3036fcf88..12f6df690 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -204,8 +204,14 @@ class Request extends RequestAbstract UriFactory::setQuery('/scheme', $this->uri->getScheme()); UriFactory::setQuery('/host', $this->uri->getHost()); UriFactory::setQuery('/lang', $this->l11n->getLanguage()); + UriFactory::setQuery('/base', $this->uri->getBase()); + UriFactory::setQuery('/rootPath', $this->uri->getRootPath()); UriFactory::setQuery('?', $this->uri->getQuery()); UriFactory::setQuery('%', $this->uri->__toString()); + UriFactory::setQuery('#', $this->uri->getFragment()); + UriFactory::setQuery('/', $this->uri->getPath()); + UriFactory::setQuery(':user', $this->uri->getUser()); + UriFactory::setQuery(':pass', $this->uri->getPass()); } /**