From 0877aeb7d5d0082517af33d442c773980480576b Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 27 Jul 2018 19:52:56 +0200 Subject: [PATCH] Implement console framework features --- Console/CommandManager.php | 129 --------------------------- Message/Console/Request.php | 36 +++++++- Message/Console/Response.php | 19 ---- Module/InstallerAbstract.php | 30 ++++--- Router/Router.php | 13 +-- Uri/Argument.php | 5 ++ tests/Console/CommandManagerTest.php | 26 ------ 7 files changed, 59 insertions(+), 199 deletions(-) delete mode 100644 Console/CommandManager.php delete mode 100644 tests/Console/CommandManagerTest.php diff --git a/Console/CommandManager.php b/Console/CommandManager.php deleted file mode 100644 index a76251c97..000000000 --- a/Console/CommandManager.php +++ /dev/null @@ -1,129 +0,0 @@ -commands[$cmd])) { - $this->commands[$cmd] = [$callback, $source]; - $this->count++; - - return true; - } - - return false; - } - - /** - * Detach existing command. - * - * @param string $cmd Command ID - * @param mixed $source Provider - * - * @return bool - * - * @since 1.0.0 - */ - public function detach(string $cmd, $source) : bool - { - if (array_key_exists($cmd, $this->commands)) { - unset($this->commands[$cmd]); - $this->count--; - - return true; - } - - return false; - } - - /** - * Trigger command. - * - * @param string $cmd Command ID - * @param mixed $para Parameters to pass - * - * @return mixed|bool - * - * @since 1.0.0 - */ - public function trigger(string $cmd, $para) - { - if (array_key_exists($cmd, $this->commands)) { - return $this->commands[$cmd][0]($para); - } - - return false; - } - - /** - * Count commands. - * - * @return int - * - * @since 1.0.0 - */ - public function count() : int - { - return $this->count; - } -} diff --git a/Message/Console/Request.php b/Message/Console/Request.php index 3f74dbae1..3ac251ae6 100644 --- a/Message/Console/Request.php +++ b/Message/Console/Request.php @@ -55,6 +55,36 @@ final class Request extends RequestAbstract $this->header->setL11n($l11n ?? new Localization()); $this->uri = $uri; + $this->init(); + } + + /** + * Init request. + * + * This is used in order to either initialize the current http request or a batch of GET requests + * + * @return void + * + * @since 1.0.0 + */ + private function init() : void + { + $lang = \explode('_', $_SERVER['LANG'] ?? ''); + $this->header->getL11n()->setLanguage($lang[0] ?? 'en'); + + $this->cleanupGlobals(); + } + + /** + * Clean up globals that musn't be used any longer + * + * @return void + * + * @since 1.0.0 + */ + private function cleanupGlobals() : void + { + unset($_SERVER); } /** @@ -114,7 +144,11 @@ final class Request extends RequestAbstract public function getMethod() : string { if ($this->method === null) { - $this->method = RequestMethod::GET; + $temp = $this->uri->__toString(); + $found = \stripos($temp, ':'); + $method = $found !== false && $found > 3 && $found < 8 ? \substr($temp, 0, $found) : RequestMethod::GET; + + $this->method = $method === false ? RequestMethod::GET : $method; } return $this->method; diff --git a/Message/Console/Response.php b/Message/Console/Response.php index 94f26195b..10effadb6 100644 --- a/Message/Console/Response.php +++ b/Message/Console/Response.php @@ -137,25 +137,6 @@ final class Response extends ResponseAbstract implements RenderableInterface } } - return $this->removeWhitespaceAndLineBreak($render); - } - - /** - * Remove whitespace and line break from render - * - * @param string $render Rendered string - * - * @return string - * - * @since 1.0.0 - */ - private function removeWhitespaceAndLineBreak(string $render) : string - { - $types = $this->header->get('Content-Type'); - if (\stripos($types[0], MimeType::M_HTML) !== false) { - return \trim(\preg_replace('/(\s{2,}|\n|\t)(?![^<>]*<\/pre>)/', ' ', $render)); - } - return $render; } diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index 6d77f0701..b7a8a2418 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -18,6 +18,7 @@ use phpOMS\DataStorage\Database\DatabaseType; use phpOMS\DataStorage\Database\Exception\InvalidDatabaseTypeException; use phpOMS\DataStorage\Database\DatabasePool; use phpOMS\System\File\Local\Directory; +use phpOMS\System\File\Local\File; use phpOMS\System\File\PathException; use phpOMS\System\File\PermissionException; use phpOMS\Utils\Parser\Php\ArrayParser; @@ -73,7 +74,6 @@ class InstallerAbstract $sth->bindValue(':from', $val['from'], \PDO::PARAM_STR); $sth->bindValue(':for', $val['for'], \PDO::PARAM_STR); $sth->bindValue(':file', $val['file'], \PDO::PARAM_STR); - $sth->execute(); } } @@ -150,11 +150,13 @@ class InstallerAbstract { $directories = new Directory(\dirname($info->getPath()) . '/Admin/Routes'); - foreach ($directories as $key => $subdir) { - if ($subdir instanceof Directory) { - foreach ($subdir as $key2 => $file) { - self::installRoutes(__DIR__ . '/../../' . $subdir->getName() . '/' . basename($file->getName(), '.php') . '/Routes.php', $file->getPath()); + foreach ($directories as $key => $child) { + if ($child instanceof Directory) { + foreach ($child as $key2 => $file) { + self::installRoutes(__DIR__ . '/../../' . $child->getName() . '/' . \basename($file->getName(), '.php') . '/Routes.php', $file->getPath()); } + } elseif ($child instanceof File) { + self::installRoutes(__DIR__ . '/../../' . $child->getName() . '/Routes.php', $child->getPath()); } } } @@ -185,7 +187,7 @@ class InstallerAbstract throw new PathException($destRoutePath); } - if (!is_writable($destRoutePath)) { + if (!\is_writable($destRoutePath)) { throw new PermissionException($destRoutePath); } @@ -194,7 +196,7 @@ class InstallerAbstract /** @noinspection PhpIncludeInspection */ $moduleRoutes = include $srcRoutePath; - $appRoutes = array_merge_recursive($appRoutes, $moduleRoutes); + $appRoutes = \array_merge_recursive($appRoutes, $moduleRoutes); \file_put_contents($destRoutePath, 'getPath()) . '/Admin/Hooks'); - foreach ($directories as $key => $subdir) { - if ($subdir instanceof Directory) { - foreach ($subdir as $key2 => $file) { - self::installHooks(__DIR__ . '/../../' . $subdir->getName() . '/' . basename($file->getName(), '.php') . '/Hooks.php', $file->getPath()); + foreach ($directories as $key => $child) { + if ($child instanceof Directory) { + foreach ($child as $key2 => $file) { + self::installHooks(__DIR__ . '/../../' . $child->getName() . '/' . \basename($file->getName(), '.php') . '/Hooks.php', $file->getPath()); } + } elseif ($child instanceof File) { + self::installRoutes(__DIR__ . '/../../' . $child->getName() . '/Hooks.php', $child->getPath()); } } } @@ -249,7 +253,7 @@ class InstallerAbstract throw new PathException($destHookPath); } - if (!is_writable($destHookPath)) { + if (!\is_writable($destHookPath)) { throw new PermissionException($destHookPath); } @@ -258,7 +262,7 @@ class InstallerAbstract /** @noinspection PhpIncludeInspection */ $moduleHooks = include $srcHookPath; - $appHooks = array_merge_recursive($appHooks, $moduleHooks); + $appHooks = \array_merge_recursive($appHooks, $moduleHooks); \file_put_contents($destHookPath, 'routes += include $path; @@ -105,7 +96,7 @@ final class Router if ($request instanceof RequestAbstract) { $uri = $request->getUri()->getRoute(); $verb = $request->getRouteVerb(); - } elseif (is_string($request)) { + } elseif (\is_string($request)) { $uri = $request; } else { throw new \InvalidArgumentException(); diff --git a/Uri/Argument.php b/Uri/Argument.php index bbd50c006..1b5196197 100644 --- a/Uri/Argument.php +++ b/Uri/Argument.php @@ -145,6 +145,11 @@ final class Argument implements UriInterface public function set(string $uri) : void { $this->uri = $uri; + + $temp = $this->__toString(); + $found = \stripos($temp, ':'); + $path = $found !== false && $found > 3 && $found < 8 ? \substr($temp, $found) : $temp; + $this->path = $path === false ? '' : $path; } /** diff --git a/tests/Console/CommandManagerTest.php b/tests/Console/CommandManagerTest.php deleted file mode 100644 index 60e60ba76..000000000 --- a/tests/Console/CommandManagerTest.php +++ /dev/null @@ -1,26 +0,0 @@ -