Skip to content

Commit a10116e

Browse files
committed
v1.0 release
0 parents  commit a10116e

16 files changed

+399
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/vendor/

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "micro/kernel-boot-configuration",
3+
"description": "Micro Framework: Kernel Boot loader - component to provide plugin configuration",
4+
"type": "library",
5+
"version": "1.0",
6+
"require": {
7+
"micro/kernel": "^1"
8+
},
9+
"require-dev": {
10+
"phpunit/phpunit": "^9"
11+
},
12+
"license": "MIT",
13+
"autoload": {
14+
"psr-4": {
15+
"Micro\\Framework\\Kernel\\": "src/"
16+
}
17+
},
18+
"authors": [
19+
{
20+
"name": "Stanislau Komar",
21+
"email": "[email protected]"
22+
}
23+
]
24+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Boot;
4+
5+
use Micro\Framework\Kernel\Configuration\ApplicationConfigurationFactoryInterface;
6+
use Micro\Framework\Kernel\Configuration\ApplicationConfigurationInterface;
7+
use Micro\Framework\Kernel\Configuration\DefaultApplicationConfigurationFactory;
8+
use Micro\Framework\Kernel\Configuration\Plugin\ConfigurableInterface;
9+
use Micro\Framework\Kernel\Plugin\PluginBootLoaderInterface;
10+
11+
class ConfigurationProviderBootLoader implements PluginBootLoaderInterface
12+
{
13+
private readonly ApplicationConfigurationInterface $configuration;
14+
15+
/**
16+
* @param array|ApplicationConfigurationInterface|ApplicationConfigurationFactoryInterface $config
17+
*/
18+
public function __construct(
19+
array|ApplicationConfigurationInterface|ApplicationConfigurationFactoryInterface $config
20+
)
21+
{
22+
$applicationConfig = $config;
23+
24+
if(is_array($config)) {
25+
$applicationConfig = (new DefaultApplicationConfigurationFactory($config));
26+
}
27+
28+
if(($applicationConfig instanceof ApplicationConfigurationFactoryInterface)) {
29+
$applicationConfig = $applicationConfig->create();
30+
}
31+
32+
$this->configuration = $applicationConfig;
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function boot(object $applicationPlugin): void
39+
{
40+
if(!($applicationPlugin instanceof ConfigurableInterface)) {
41+
return;
42+
}
43+
44+
$applicationPlugin->setConfiguration($this->configuration);
45+
}
46+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
interface ApplicationConfigurationFactoryInterface
6+
{
7+
/**
8+
* @return ApplicationConfigurationInterface
9+
*/
10+
public function create(): ApplicationConfigurationInterface;
11+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
interface ApplicationConfigurationInterface
6+
{
7+
/**
8+
* @param string $key
9+
* @param mixed|null $default
10+
* @param bool $nullable
11+
*
12+
* @return mixed
13+
*/
14+
public function get(string $key, mixed $default = null, bool $nullable = true): mixed;
15+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
use Micro\Framework\Kernel\Configuration\Exception\InvalidConfigurationException;
6+
7+
class DefaultApplicationConfiguration implements ApplicationConfigurationInterface
8+
{
9+
private const BOOLEAN_TRUE = [
10+
'true', 'on', '1', 'yes'
11+
];
12+
13+
/**
14+
* @param array<string, mixed> $configuration
15+
*/
16+
public function __construct(private readonly array $configuration)
17+
{
18+
}
19+
20+
/**
21+
* {@inheritDoc}
22+
*/
23+
public function get(string $key, $default = null, bool $nullable = true): mixed
24+
{
25+
if(is_bool($default)) {
26+
return $this->getBooleanValue($key, $default);
27+
}
28+
29+
$value = $this->getValue($key, $default);
30+
31+
if($nullable === false && !$value && !is_numeric($value)) {
32+
throw new InvalidConfigurationException(sprintf('Configuration key "%s" can not be NULL', $key));
33+
}
34+
35+
return $value;
36+
}
37+
38+
/**
39+
* @param string $key
40+
* @param bool $default
41+
*
42+
* @return bool
43+
*/
44+
protected function getBooleanValue(string $key, bool $default): bool
45+
{
46+
$value = $this->getValue($key, $default);
47+
if($value === null) {
48+
return $default;
49+
}
50+
51+
return in_array(mb_strtolower($value), self::BOOLEAN_TRUE, true);
52+
}
53+
54+
/**
55+
* @param string $key
56+
* @param mixed $default
57+
*
58+
* @return mixed
59+
*/
60+
protected function getValue(string $key, mixed $default): mixed
61+
{
62+
return $this->configuration[$key] ?? $default;
63+
}
64+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
class DefaultApplicationConfigurationFactory implements ApplicationConfigurationFactoryInterface
6+
{
7+
/**
8+
* @param array<string, mixed> $configuration
9+
*/
10+
public function __construct(private readonly array $configuration)
11+
{
12+
}
13+
14+
/**
15+
* @return ApplicationConfigurationInterface
16+
*/
17+
public function create(): ApplicationConfigurationInterface
18+
{
19+
return new DefaultApplicationConfiguration($this->configuration);
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration\Exception;
4+
5+
class InvalidConfigurationException extends \RuntimeException
6+
{
7+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
class PluginConfiguration implements PluginConfigurationInterface
6+
{
7+
/**
8+
* @param ApplicationConfigurationInterface $configuration
9+
*/
10+
public function __construct(
11+
protected readonly ApplicationConfigurationInterface $configuration
12+
)
13+
{
14+
}
15+
16+
/**
17+
* @param string|array $list
18+
* @param string $separator
19+
* @return string[]
20+
*/
21+
protected function explodeStringToArray(string|array $list, string $separator = ','): array
22+
{
23+
if(is_array($list)) {
24+
return $list;
25+
}
26+
27+
if($separator === '') {
28+
return [$list];
29+
}
30+
31+
$itemsColl = explode($separator, $list);
32+
33+
return array_map('trim', $itemsColl);
34+
}
35+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
interface PluginConfigurationInterface
6+
{
7+
}

0 commit comments

Comments
 (0)