Skip to content

Commit

Permalink
patch
Browse files Browse the repository at this point in the history
  • Loading branch information
ArrayIterator committed Mar 9, 2024
1 parent 56105a4 commit b204589
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 36 deletions.
63 changes: 30 additions & 33 deletions src/HttpKernel/BaseKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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)) {
Expand Down
76 changes: 76 additions & 0 deletions src/Kernel/Interfaces/KernelInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
63 changes: 62 additions & 1 deletion src/Module/Traits/ModuleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,69 @@

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,
$this->getContainer()
);
}

/**
* 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();
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions src/Util/Generator/UUID.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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]));
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -603,6 +608,7 @@ public static function v5(string $namespace, string $name): string

/**
* @inheritdoc
* Default stringable return uuid v4
*/
public function __toString(): string
{
Expand Down

0 comments on commit b204589

Please sign in to comment.