From b2045897f38b69c007083951e0f8516c87ce3c51 Mon Sep 17 00:00:00 2001 From: ArrayIterator Date: Sat, 9 Mar 2024 11:54:22 +0700 Subject: [PATCH] patch --- src/HttpKernel/BaseKernel.php | 63 +++++++++---------- src/Kernel/Interfaces/KernelInterface.php | 76 +++++++++++++++++++++++ src/Module/Traits/ModuleTrait.php | 63 ++++++++++++++++++- src/Util/Generator/UUID.php | 10 ++- 4 files changed, 176 insertions(+), 36 deletions(-) diff --git a/src/HttpKernel/BaseKernel.php b/src/HttpKernel/BaseKernel.php index f8bd4dd..2226d9e 100644 --- a/src/HttpKernel/BaseKernel.php +++ b/src/HttpKernel/BaseKernel.php @@ -382,6 +382,9 @@ public function __call(string $name, array $arguments) /*! STATUS */ private bool $providerRegistered = false; + /** + * @var ?string The root directory + */ private ?string $rootDirectory = null; /** @@ -390,25 +393,24 @@ public function __call(string $name, array $arguments) private string $baseConfigFile = self::BASE_CONFIG_FILE_NAME; /** - * @var ?Throwable + * @inheritdoc */ - private ?Throwable $configErrorException = null; - public function isHasInit(): bool { return $this->hasInit; } + /** + * @inheritdoc + */ public function isReady(): bool { return $this->ready; } - public function getConfigErrorException(): ?Throwable - { - return $this->configErrorException; - } - + /** + * @inheritdoc + */ final public function init() : static { if ($this->hasInit) { @@ -527,31 +529,26 @@ final public function init() : static $this->configError = self::CONFIG_NOT_FILE; } else { $configurations = null; - try { - // json, yaml, yml, php, env - switch (strtolower(pathinfo($this->configFile, PATHINFO_EXTENSION))) { - case 'env': - $configurations = DataNormalizer::notationToArray( - DotEnv::fromFile($this->configFile) - ); - break; - case 'json': - $configurations = json_decode((string)file_get_contents($this->configFile), true); - break; - case 'yaml': - case 'yml': - $configurations = Yaml::parseFile($this->configFile); - break; - case 'php': - $configurations = (function ($configFile) { - $result = require $configFile; - return is_iterable($result) ? $result : null; - })->bindTo(null)($configFile); - break; - } - } catch (Throwable $e) { - $this->configErrorException = $e; - unset($e); + // json, yaml, yml, php, env + switch (strtolower(pathinfo($this->configFile, PATHINFO_EXTENSION))) { + case 'env': + $configurations = DataNormalizer::notationToArray( + DotEnv::fromFile($this->configFile) + ); + break; + case 'json': + $configurations = json_decode((string)file_get_contents($this->configFile), true); + break; + case 'yaml': + case 'yml': + $configurations = Yaml::parseFile($this->configFile); + break; + case 'php': + $configurations = (function ($configFile) { + $result = require $configFile; + return is_iterable($result) ? $result : null; + })->bindTo(null)($configFile); + break; } if (!is_iterable($configurations)) { diff --git a/src/Kernel/Interfaces/KernelInterface.php b/src/Kernel/Interfaces/KernelInterface.php index f53aff1..0286c7f 100644 --- a/src/Kernel/Interfaces/KernelInterface.php +++ b/src/Kernel/Interfaces/KernelInterface.php @@ -22,33 +22,109 @@ interface KernelInterface extends RunnableInterface, TerminableInterface */ public const BASE_CONFIG_FILE_NAME = 'config.php'; + /** + * Register a directory to search for controllers + * + * @param string ...$directory The directory to search for controllers + */ public function registerControllerDirectory(string ...$directory); + /** + * Remove a directory from search for controllers + * + * @param string ...$directory The directory to remove from search for controllers + */ public function removeControllerDirectory(string ...$directory); + /** + * Get start memory + * + * @return int Start memory + */ public function getStartMemory() : int; + /** + * Get start time + * + * @return float Start time + * @uses \microtime() + */ public function getStartTime() : float; + /** + * Boot the kernel + */ public function boot(); + /** + * Shutdown the kernel + */ public function shutdown(); + /** + * Check if the kernel is booted + * + * @return bool True if the kernel is booted, false otherwise + */ public function isBooted(): bool; + /** + * Check if the kernel is shutdown + * + * @return bool True if the kernel is shutdown, false otherwise + */ public function isShutdown(): bool; + /** + * Get the Http Kernel + * + * @return HttpKernelInterface + */ public function getHttpKernel() : HttpKernelInterface; + /** + * Get the config error + * + * @return ?string The config + * @uses KernelInterface::CONFIG_EMPTY_FILE + * @uses KernelInterface::CONFIG_NOT_FILE + * @uses KernelInterface::CONFIG_NOT_ITERABLE + * @uses KernelInterface::CONFIG_UNAVAILABLE + */ public function getConfigError(): ?string; + /** + * Get the config file + * + * @return ?string The config file + */ public function getConfigFile(): ?string; + /** + * Get the root directory + * + * @return ?string The root directory + */ public function getRootDirectory(): ?string; + /** + * Check if the kernel has been initialized + * + * @return bool + */ public function isHasInit(): bool; + /** + * Initialize the kernel + * + * @return $this + */ public function init() : static; + /** + * Check if the kernel is ready + * + * @return bool + */ public function isReady() : bool; } diff --git a/src/Module/Traits/ModuleTrait.php b/src/Module/Traits/ModuleTrait.php index 5d367bc..0630d60 100644 --- a/src/Module/Traits/ModuleTrait.php +++ b/src/Module/Traits/ModuleTrait.php @@ -13,17 +13,37 @@ trait ModuleTrait { + /** + * @var string Module name + */ protected string $name = ''; + /** + * @var ?string Module description + */ protected ?string $description = ''; + /** + * @var bool Module is important + */ protected bool $important = false; + /** + * @var int Module priority + */ protected int $priority = ModuleInterface::DEFAULT_PRIORITY; + /** + * @var bool Module is initialized + */ private bool $init = false; - public function getKernel() + /** + * Getting kernel from container + * + * @return KernelInterface + */ + public function getKernel() : KernelInterface { return ContainerHelper::service( KernelInterface::class, @@ -31,16 +51,31 @@ public function getKernel() ); } + /** + * Get module is important + * + * @return bool Module is important + */ public function isImportant(): bool { return $this->important; } + /** + * Get container + *s + * @return ContainerInterface Container + */ public function getContainer(): ContainerInterface { return $this->getModules()->getContainer(); } + /** + * Get manager + * + * @return ManagerInterface Manager + */ public function getManager(): ManagerInterface { $manager = $this->getModules()->getManager(); @@ -51,16 +86,32 @@ public function getManager(): ManagerInterface return $manager; } + /** + * Get module name + * + * @return string Module name + */ public function getName(): string { return $this->name; } + /** + * Get module description + * + * @return ?string Module description + */ public function getDescription(): ?string { return $this->description; } + /** + * Doing initialization + * + * @return void + * @final Module initialization + */ final public function init(): void { if ($this->init) { @@ -69,16 +120,26 @@ final public function init(): void $this->doInit(); } + /** + * Get module priority + * @return int Module priority + */ public function getPriority(): int { return $this->priority; } + /** + * Method (override by children) when module is initialized + */ protected function doInit() { // pass } + /** + * @return Modules Modules + */ public function getModules(): Modules { return $this->modules; diff --git a/src/Util/Generator/UUID.php b/src/Util/Generator/UUID.php index 4acfba1..67084ee 100644 --- a/src/Util/Generator/UUID.php +++ b/src/Util/Generator/UUID.php @@ -167,6 +167,9 @@ public static function version(string $uuid): ?int * 4. clock seq hi and reserved * 5. clock seq low * 6. node + * 7. version + * 8. variant + * 9. variant name * * @param string $uuid * @return ?array{ @@ -266,7 +269,9 @@ public static function integerId(string $uuid): ?string // using bcmul() && bcadd() binary calculator function & prevent loss of precision for ($i = 0; $i < $length; $i++) { // get the char from hex at position $i + // -> bcmul the decimal by 16 $dec = Consolidation::multiplyInt($dec, 16); + // -> bcadd the decimal by the integer value of the hex char $dec = Consolidation::addInt($dec, hexdec($hex[$i])); } @@ -433,7 +438,7 @@ public static function generate( if ($type === self::UUID_TYPE_MD5 || $type === self::UUID_TYPE_SHA1) { if (!$hash) { $hash = match ($type) { - self::UUID_TYPE_MD5 => md5(Random::bytes(16)), + self::UUID_TYPE_MD5 => md5(Random::bytes(16)), // random_bytes(16) is 128 bits default => sha1(Random::bytes(16)), }; } elseif ($type === self::UUID_TYPE_SHA1) { @@ -506,7 +511,7 @@ public static function generate( // time high and version bits (12) $timeHi = ($timestamp >> 48) & 0x0fff; } else { - $timeLow = Random::int(0, 0xffffffff); + $timeLow = Random::int(0, 0xffffffff); // random_int $timeMid = Random::int(0, 0xffff); $timeHi = Random::int(0, 0x0fff); } @@ -603,6 +608,7 @@ public static function v5(string $namespace, string $name): string /** * @inheritdoc + * Default stringable return uuid v4 */ public function __toString(): string {