1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SeedApp; |
4
|
|
|
|
5
|
|
|
use Monolog\Logger; |
6
|
|
|
use Monolog\Handler\StreamHandler; |
7
|
|
|
use Monolog\Registry; |
8
|
|
|
use SlimX\Models\Error; |
9
|
|
|
use SlimX\Exceptions\ErrorCodeException; |
10
|
|
|
|
11
|
|
|
class DependencyManager |
12
|
|
|
{ |
13
|
|
|
protected $container; |
14
|
|
|
|
15
|
|
|
public function __construct(\Slim\App $app) |
16
|
|
|
{ |
17
|
|
|
$this->container = $app->getContainer(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function loadDependencies() |
21
|
|
|
{ |
22
|
|
|
$this->container['log'] = function ($container) { |
23
|
|
|
$conf = $container->get('settings')['logger']; |
24
|
|
|
$log = new Logger($conf['name']); |
25
|
|
|
$log->pushHandler(new StreamHandler( |
26
|
|
|
__DIR__ . '/../../logs/api-' . |
27
|
|
|
(new \DateTime())->format('Ymd') . '.log', |
28
|
|
|
$conf['level'] ?? Logger::DEBUG |
29
|
|
|
)); |
30
|
|
|
try { |
31
|
|
|
Registry::addLogger($log, 'log'); |
32
|
|
|
} catch (\InvalidArgumentException $e) { |
33
|
|
|
// Log already exists. Let it go! |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return $log; |
37
|
|
|
}; |
38
|
|
|
|
39
|
|
|
$this->container['error'] = function ($container) { |
|
|
|
|
40
|
|
|
$error = new Error(); |
41
|
|
|
$error->setCodeList([ |
42
|
|
|
1000 => [ |
43
|
|
|
'status' => 406, |
44
|
|
|
'message' => 'API version is mandatory', |
45
|
|
|
], |
46
|
|
|
]); |
47
|
|
|
|
48
|
|
|
return $error; |
49
|
|
|
}; |
50
|
|
|
|
51
|
|
|
$this->container['errorHandler'] = function ($container) { |
52
|
|
|
return function ($request, $response, $exception) use ($container) { |
53
|
|
|
if ($exception instanceof ErrorCodeException) { |
54
|
|
|
$container->get('log')->info('Error code ' . $exception->getCode()); |
55
|
|
|
return $container->get('error')->handle($response, $exception->getCode()); |
56
|
|
|
} |
57
|
|
|
}; |
58
|
|
|
}; |
59
|
|
|
|
60
|
|
|
$this->container['testDbConnection'] = function ($container) { |
61
|
|
|
$dbTest = $container->get('settings')['dbtest']; |
62
|
|
|
return new \PDO( |
63
|
|
|
"mysql:host={$dbTest['host']};dbname={$dbTest['dbname']}", |
64
|
|
|
$dbTest['user'], |
65
|
|
|
$dbTest['pass'] |
66
|
|
|
); |
67
|
|
|
}; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.