1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Config; |
4
|
|
|
|
5
|
|
|
use Aura\Session\SessionFactory; |
6
|
|
|
use Aura\Session\Session; |
7
|
|
|
use DI\Container; |
8
|
|
|
use Slim\App; |
9
|
|
|
use Slim\Views\PhpRenderer; |
10
|
|
|
use Valitron\Validator; |
11
|
|
|
|
12
|
|
|
use Jidaikobo\Kontiki\Services\FileService; |
13
|
|
|
use Jidaikobo\Kontiki\Services\RoutesService; |
14
|
|
|
use Jidaikobo\Kontiki\Services\ValidationService; |
15
|
|
|
use Jidaikobo\Kontiki\Core\Auth; |
16
|
|
|
use Jidaikobo\Kontiki\Core\Database; |
17
|
|
|
use Jidaikobo\Kontiki\Models\UserModel; |
18
|
|
|
|
19
|
|
|
class Dependencies |
20
|
|
|
{ |
21
|
|
|
public function __construct(private App $app) {} |
22
|
|
|
|
23
|
|
|
public function register(): void |
24
|
|
|
{ |
25
|
|
|
/** @var Container $container */ |
26
|
|
|
$container = $this->app->getContainer(); |
27
|
|
|
|
28
|
|
|
$container->set(App::class, $this->app); |
29
|
|
|
$container->set(Database::class, fn() => $this->createDatabase()); |
30
|
|
|
$container->set(Session::class, fn() => $this->createSession()); |
31
|
|
|
$container->set(UserModel::class, fn($c) => $this->createUserModel($c)); |
32
|
|
|
$container->set(Auth::class, fn($c) => $this->createAuth($c)); |
33
|
|
|
$container->set(ValidationService::class, fn($c) => $this->createValidationService($c)); |
34
|
|
|
$container->set(PhpRenderer::class, fn() => $this->createPhpRenderer()); |
35
|
|
|
$container->set(FileService::class, fn() => $this->createFileService()); |
36
|
|
|
$container->set(RoutesService::class, fn() => $this->createRoutesService()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function createDatabase(): Database |
40
|
|
|
{ |
41
|
|
|
return new Database([ |
42
|
|
|
'driver' => 'sqlite', |
43
|
|
|
'database' => env('PROJECT_PATH', '') . '/' . env('DB_DATABASE', ''), |
44
|
|
|
'charset' => 'utf8', |
45
|
|
|
'collation' => 'utf8_unicode_ci', |
46
|
|
|
'prefix' => '', |
47
|
|
|
]); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
private function createSession(): Session |
51
|
|
|
{ |
52
|
|
|
$uri = $_SERVER['REQUEST_URI'] ?? ''; |
53
|
|
|
if ( |
54
|
|
|
str_contains($uri, '.js') || |
55
|
|
|
str_contains($uri, '.css') || |
56
|
|
|
str_contains($uri, '.ico') |
57
|
|
|
) { |
58
|
|
|
session_cache_limiter('private_no_expire'); |
59
|
|
|
} |
60
|
|
|
return (new SessionFactory())->newInstance($_COOKIE); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function createUserModel(Container $c): UserModel |
64
|
|
|
{ |
65
|
|
|
return new UserModel( |
66
|
|
|
$c->get(Database::class), |
67
|
|
|
$c->get(ValidationService::class) |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function createAuth(Container $c): Auth |
72
|
|
|
{ |
73
|
|
|
return new Auth( |
74
|
|
|
$c->get(Session::class), |
75
|
|
|
$c->get(UserModel::class) |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function createValidationService(Container $c): ValidationService |
80
|
|
|
{ |
81
|
|
|
$validator = new Validator([], [], env('APPLANG', 'en')); |
82
|
|
|
return new ValidationService($c->get(Database::class), $validator); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function createPhpRenderer(): PhpRenderer |
86
|
|
|
{ |
87
|
|
|
return new PhpRenderer(__DIR__ . '/../../src/views'); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function createFileService(): FileService |
91
|
|
|
{ |
92
|
|
|
$uploadDir = env('PROJECT_PATH', '') . env('UPLOADDIR', ''); |
93
|
|
|
$allowedTypes = json_decode(env('ALLOWED_MIME_TYPES', '[]'), true); |
94
|
|
|
$maxSize = env('MAXSIZE', 5000000); |
95
|
|
|
return new FileService($uploadDir, $allowedTypes, $maxSize); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function createRoutesService(): RoutesService |
99
|
|
|
{ |
100
|
|
|
return new RoutesService($this->app->getRouteCollector()); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|