Skip to content

Commit e37e6f9

Browse files
committed
INitial commit
0 parents  commit e37e6f9

13 files changed

+336
-0
lines changed

composer.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "micro/framework-kernel",
3+
"type": "library",
4+
"description": "",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Micro\\Framework\\Kernel\\": "src/"
9+
}
10+
},
11+
"authors": [
12+
{
13+
"name": "Stanislau.Komar",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"minimum-stability": "stable",
18+
"require": {
19+
}
20+
}
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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
interface ApplicationConfigurationInterface
6+
{
7+
/**
8+
* @param string $key
9+
* @param $default
10+
* @return mixed
11+
*/
12+
public function get(string $key, $default = null): mixed;
13+
}
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 DefaultApplicationConfiguration implements ApplicationConfigurationInterface
6+
{
7+
/**
8+
* @param array<string, mixed> $configuration
9+
*/
10+
public function __construct(private array $configuration)
11+
{
12+
}
13+
14+
/**
15+
* {@inheritDoc}
16+
*/
17+
public function get(string $key, $default = null): mixed
18+
{
19+
return $this->configuration[$key] ?? $default;
20+
}
21+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
use JetBrains\PhpStorm\Pure;
6+
7+
class DefaultApplicationConfigurationFactory implements ApplicationConfigurationFactoryInterface
8+
{
9+
/**
10+
* @param array<string, mixed> $configuration
11+
*/
12+
public function __construct(private array $configuration)
13+
{
14+
}
15+
16+
/**
17+
* @return ApplicationConfigurationInterface
18+
*/
19+
#[Pure] public function create(): ApplicationConfigurationInterface
20+
{
21+
return new DefaultApplicationConfiguration($this->configuration);
22+
}
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
class PluginConfiguration
6+
{
7+
/**
8+
* @param ApplicationConfigurationInterface $configuration
9+
*/
10+
public function __construct(protected ApplicationConfigurationInterface $configuration)
11+
{
12+
}
13+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration;
4+
5+
use Micro\Framework\Kernel\Configuration\Resolver\PluginConfigurationClassResolverInterface;
6+
use Micro\Framework\Kernel\Configuration\Resolver\PluginNameResolver;
7+
use Micro\Framework\Kernel\Configuration\Resolver\PluginNameShortResolver;
8+
9+
class PluginConfigurationClassResolver
10+
{
11+
/**
12+
* @var PluginConfigurationClassResolverInterface[]
13+
*/
14+
private array $resolvers;
15+
16+
/**
17+
* @param string $pluginClass
18+
* @param ApplicationConfigurationInterface $applicationConfiguration
19+
*/
20+
public function __construct(
21+
private string $pluginClass,
22+
private ApplicationConfigurationInterface $applicationConfiguration
23+
) {
24+
$this->resolvers[] = new PluginNameResolver();
25+
$this->resolvers[] = new PluginNameShortResolver();
26+
}
27+
28+
/**
29+
* @return PluginConfiguration
30+
*/
31+
public function resolve(): PluginConfiguration
32+
{
33+
$configClassDefault = PluginConfiguration::class;
34+
$configClasses = [];
35+
36+
foreach ($this->resolvers as $resolver) {
37+
$configClass = $resolver->resolve($this->pluginClass);
38+
39+
if(!class_exists($configClass)) {
40+
continue;
41+
}
42+
43+
$configClasses[] = $configClass;
44+
}
45+
46+
if(count($configClasses) > 1) {
47+
throw new \RuntimeException(
48+
sprintf(
49+
'Too many configuration classes for Application plugin "%s". [%s]',
50+
$this->pluginClass,
51+
implode(", ", $configClasses)
52+
)
53+
);
54+
}
55+
56+
$configClass = $configClasses[0] ?? $configClassDefault;
57+
58+
return new $configClass($this->applicationConfiguration);
59+
}
60+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration\Resolver;
4+
5+
interface PluginConfigurationClassResolverInterface
6+
{
7+
/**
8+
* @param string $pluginClass
9+
* @return string
10+
*/
11+
public function resolve(string $pluginClass): string;
12+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration\Resolver;
4+
5+
class PluginNameResolver implements PluginConfigurationClassResolverInterface
6+
{
7+
/**
8+
* {@inheritDoc}
9+
*/
10+
public function resolve(string $pluginClass): string
11+
{
12+
return $pluginClass . 'Configuration';
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Micro\Framework\Kernel\Configuration\Resolver;
4+
5+
class PluginNameShortResolver implements PluginConfigurationClassResolverInterface
6+
{
7+
/**
8+
* {@inheritDoc}
9+
*/
10+
public function resolve(string $pluginClass): string
11+
{
12+
return $pluginClass . 'Config';
13+
}
14+
}

0 commit comments

Comments
 (0)