|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace Koded\Framework; |
|
4
|
|
|
|
|
5
|
|
|
use Koded\{DIContainer, DIModule}; |
|
6
|
|
|
use Koded\Framework\Auth\{AuthBackend, AuthProcessor, BearerAuthProcessor, SessionAuthBackend}; |
|
7
|
|
|
use Koded\Http\{ServerRequest, ServerResponse}; |
|
8
|
|
|
use Koded\Http\Interfaces\{Request, Response}; |
|
9
|
|
|
use Koded\I18n\{I18n, I18nCatalog, CurlyFormatter}; |
|
10
|
|
|
use Koded\Logging\Log; |
|
11
|
|
|
use Koded\Logging\Processors\Cli; |
|
12
|
|
|
use Koded\Stdlib\{Config, Configuration, Immutable}; |
|
13
|
|
|
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface}; |
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Psr\SimpleCache\CacheInterface; |
|
16
|
|
|
use function dirname; |
|
17
|
|
|
use function is_a; |
|
18
|
|
|
use function is_readable; |
|
19
|
|
|
use function Koded\Caching\simple_cache_factory; |
|
20
|
|
|
use function putenv; |
|
21
|
|
|
|
|
22
|
|
|
final class Module implements DIModule |
|
23
|
|
|
{ |
|
24
|
|
|
private const ENV_KEY = '__KODED_CONFIG'; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(private Configuration|string $configuration) {} |
|
27
|
|
|
|
|
28
|
40 |
|
public function configure(DIContainer $container): void |
|
29
|
|
|
{ |
|
30
|
40 |
|
$container->named('$errorSerializer', 'default_serialize_error'); |
|
31
|
40 |
|
$container->bind(Request::class, /* defer */); |
|
32
|
40 |
|
$container->bind(Response::class, /* defer */); |
|
33
|
40 |
|
$container->bind(Configuration::class, /* defer */); |
|
34
|
40 |
|
$container->bind(CacheInterface::class, /* defer */); |
|
35
|
40 |
|
$container->bind(LoggerInterface::class, /* defer */); |
|
36
|
40 |
|
$container->bind(ServerRequestInterface::class, ServerRequest::class); |
|
37
|
40 |
|
$container->bind(ResponseInterface::class, ServerResponse::class); |
|
38
|
|
|
// Core instances |
|
39
|
40 |
|
$container->share($conf = $this->configuration()); |
|
40
|
40 |
|
I18n::register(I18nCatalog::new($conf), true); |
|
41
|
40 |
|
$container->share(new Log(...$conf->get('logging', []))); |
|
42
|
40 |
|
$container->share(simple_cache_factory(...$conf->get('caching', []))); |
|
43
|
40 |
|
$container->share($container->new(Router::class)); |
|
44
|
|
|
// Default authentication |
|
45
|
40 |
|
$container->bind(AuthBackend::class, SessionAuthBackend::class); |
|
46
|
40 |
|
$container->bind(AuthProcessor::class, BearerAuthProcessor::class); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
40 |
|
private function configuration(): Configuration |
|
50
|
|
|
{ |
|
51
|
40 |
|
$factory = new Config('', $this->defaultConfiguration()); |
|
52
|
40 |
|
if (empty($this->configuration)) { |
|
53
|
31 |
|
goto load; |
|
54
|
|
|
} |
|
55
|
9 |
|
if (is_a($this->configuration, Configuration::class, true)) { |
|
56
|
8 |
|
$factory->fromObject($this->configuration); |
|
57
|
|
|
//$factory->root = dirname((new ReflectionClass($this->configuration))->getFileName()); |
|
58
|
1 |
|
} elseif (is_readable($this->configuration)) { |
|
59
|
1 |
|
putenv(self::ENV_KEY . '=' . $this->configuration); |
|
60
|
1 |
|
$factory->fromEnvVariable(self::ENV_KEY); |
|
61
|
1 |
|
$factory->root = dirname($this->configuration); |
|
62
|
|
|
} |
|
63
|
|
|
load: |
|
64
|
40 |
|
is_readable("$factory->root/.env") and $factory->fromEnvFile("$factory->root/.env"); |
|
65
|
40 |
|
foreach ($factory->get('autoloaders', []) as $autoloader) { |
|
66
|
2 |
|
include_once $autoloader; |
|
67
|
|
|
} |
|
68
|
40 |
|
return $factory; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
40 |
|
private function defaultConfiguration(): Immutable |
|
72
|
|
|
{ |
|
73
|
40 |
|
return new Immutable( |
|
74
|
40 |
|
[ |
|
75
|
|
|
// I18n directives |
|
76
|
40 |
|
'translation.dir' => __DIR__ . '/../locale', |
|
77
|
40 |
|
'translation.formatter' => CurlyFormatter::class, |
|
78
|
|
|
|
|
79
|
|
|
// CORS overrides (all values are scalar) |
|
80
|
40 |
|
'cors.disable' => false, |
|
81
|
40 |
|
'cors.origin' => '', |
|
82
|
40 |
|
'cors.methods' => '', |
|
83
|
40 |
|
'cors.headers' => '', |
|
84
|
40 |
|
'cors.expose' => '', |
|
85
|
40 |
|
'cors.maxAge' => 0, |
|
86
|
|
|
|
|
87
|
|
|
// Logging |
|
88
|
40 |
|
'logging' => [ |
|
89
|
40 |
|
[ |
|
90
|
40 |
|
[ |
|
91
|
40 |
|
'class' => Cli::class, |
|
92
|
40 |
|
'format' => '[levelname] message', |
|
93
|
40 |
|
'levels' => Log::INFO |
|
94
|
40 |
|
] |
|
95
|
40 |
|
], |
|
96
|
40 |
|
'dateformat' => 'd-m-Y H:i:s' |
|
97
|
40 |
|
], |
|
98
|
40 |
|
]); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|