Implement console framework features

This commit is contained in:
Dennis Eichhorn 2018-07-27 19:52:56 +02:00
parent b7d3427e62
commit 0877aeb7d5
7 changed files with 59 additions and 199 deletions

View File

@ -1,129 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\Console;
/**
* CommandManager class.
*
* @package Framework
* @since 1.0.0
*
* @todo : Hey, this looks like a copy of an event manager!
*/
class CommandManager implements \Countable
{
/**
* Commands.
*
* @var mixed[]
* @since 1.0.0
*/
private $commands = [];
/**
* Commands.
*
* @var int
* @since 1.0.0
*/
private $count = 0;
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
}
/**
* Attach new command.
*
* @param string $cmd Command ID
* @param mixed $callback Function callback
* @param mixed $source Provider
* @param bool $overwrite Overwrite existing
*
* @return bool
*
* @since 1.0.0
*/
public function attach(string $cmd, $callback, $source, bool $overwrite = true) : bool
{
if ($overwrite || !isset($this->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;
}
}

View File

@ -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;

View File

@ -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;
}

View File

@ -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, '<?php return ' . ArrayParser::serializeArray($appRoutes) . ';', LOCK_EX);
}
@ -214,11 +216,13 @@ class InstallerAbstract
{
$directories = new Directory(\dirname($info->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, '<?php return ' . ArrayParser::serializeArray($appHooks) . ';', LOCK_EX);
}

View File

@ -35,15 +35,6 @@ final class Router
*/
private $routes = [];
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct()
{
}
/**
* Add routes from file.
*
@ -55,7 +46,7 @@ final class Router
*/
public function importFromFile(string $path) : bool
{
if (file_exists($path)) {
if (\file_exists($path)) {
/** @noinspection PhpIncludeInspection */
$this->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();

View File

@ -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;
}
/**

View File

@ -1,26 +0,0 @@
<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
namespace phpOMS\tests\Console;
require_once __DIR__ . '/../Autoloader.php';
use phpOMS\Console\CommandManager;
class CommandManagerTest extends \PHPUnit\Framework\TestCase
{
public function testPlaceholder()
{
self::markTestIncomplete();
}
}