-
Notifications
You must be signed in to change notification settings - Fork 2
/
container.php
executable file
·54 lines (52 loc) · 1.98 KB
/
container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Skeleton application for SimpleMVC
*
* @link http://github.com/simplemvc/skeleton
* @copyright Copyright (c) Enrico Zimuel (https://www.zimuel.it)
* @license https://opensource.org/licenses/MIT MIT License
*/
declare(strict_types=1);
use App\Controller\Secret;
use League\Plates\Engine;
use Monolog\Formatter\LineFormatter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
return [
'config' => require 'config.php',
PDO::class => function(ContainerInterface $c) {
try {
return new PDO($c->get('config')['database']['pdo_dsn']);
} catch (PDOException $e) {
// cloud not find driver error (e.g missing the PHP sqlite3 ext)
if (false !== strpos($e->getMessage(), 'could not find driver')) {
$driver = $c->get('config')['database']['pdo_dsn'];
$driver = substr($driver, 0, strpos($driver, ':'));
printf("PDO error: you need to install the %s extension for PHP", $driver);
exit(1);
}
printf("PDO Error: %s", $e->getMessage());
exit(1);
}
},
Engine::class => function(ContainerInterface $c) {
$config = $c->get('config')['view'];
$engine = new Engine($config['path']);
foreach ($config['folders'] as $name => $folder) {
$engine->addFolder($name, $folder);
}
return $engine;
},
Secret::class => function(ContainerInterface $c) {
return new Secret($c->get(Engine::class), $c->get('config')['authentication']);
},
LoggerInterface::class => function(ContainerInterface $c) {
$config = $c->get('config')['logger'];
$logger = new Logger($config['name']);
$handler = new StreamHandler($config['path'], $config['level']);
$handler->setFormatter((new LineFormatter())->ignoreEmptyContextAndExtra());
return $logger->pushHandler($handler);
}
];