1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Schnittstabil\Dartisan\ServiceProviders; |
4
|
|
|
|
5
|
|
|
use Garden\Cli\Args; |
6
|
|
|
use Schnittstabil\Dartisan\Container; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @SuppressWarnings(PHPMD.ShortVariable) |
10
|
|
|
*/ |
11
|
|
|
class ConfigServiceProvider |
12
|
|
|
{ |
13
|
|
|
protected function env($key, $default = null) |
14
|
|
|
{ |
15
|
|
|
$value = getenv($key); |
16
|
|
|
|
17
|
|
|
return $value === false ? $default : $value; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
protected function setEntry(Container $container, $option, $env, $default = null, $type = null) |
21
|
|
|
{ |
22
|
|
|
$container->set($option, function (Container $c) use ($option, $env, $default, $type) { |
23
|
|
|
$defaultValue = $this->env($env, $default); |
24
|
|
|
|
25
|
|
|
if ($type !== null) { |
26
|
|
|
$defaultValue = filter_var($defaultValue, $type); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
return $c->get(Args::class)->getOpt($option, $defaultValue); |
30
|
|
|
}); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
protected function setDatabaseDriver(Container $c) |
34
|
|
|
{ |
35
|
|
|
$this->setEntry($c, 'connection-driver', 'DB_DRIVER', 'mysql'); |
36
|
|
|
$this->setEntry($c, 'connection-prefix', 'DB_PREFIX', ''); |
37
|
|
|
$this->setEntry($c, 'connection-charset', 'DB_CHARSET', 'utf8'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function setDatabaseConnection(Container $c) |
41
|
|
|
{ |
42
|
|
|
$this->setEntry($c, 'connection-host', 'DB_HOST', 'localhost'); |
43
|
|
|
$this->setEntry($c, 'connection-database', 'DB_DATABASE', 'forge'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function setDatabaseCredentials(Container $c) |
47
|
|
|
{ |
48
|
|
|
$this->setEntry($c, 'connection-username', 'DB_USERNAME', 'forge'); |
49
|
|
|
$this->setEntry($c, 'connection-password', 'DB_PASSWORD'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
protected function setDatabasePostgreSqlOpts(Container $c) |
53
|
|
|
{ |
54
|
|
|
$this->setEntry($c, 'connection-schema', 'DB_SCHEMA', 'public'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function setDatabaseMySqlOpts(Container $c) |
58
|
|
|
{ |
59
|
|
|
$this->setEntry($c, 'connection-collation', 'DB_COLLATION', 'utf8_unicode_ci'); |
60
|
|
|
$this->setEntry($c, 'connection-strict', 'DB_STRICT', false, FILTER_VALIDATE_BOOLEAN); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function setDatabaseAbstractionOpts(Container $c) |
64
|
|
|
{ |
65
|
|
|
$this->setEntry($c, 'migration-path', 'DB_MIGRATION_PATH', 'database/migrations'); |
66
|
|
|
$this->setEntry($c, 'migration-table', 'DB_MIGRATION_TABLE', 'migrations'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function __invoke(Container $c) |
70
|
|
|
{ |
71
|
|
|
$this->setDatabaseDriver($c); |
72
|
|
|
$this->setDatabaseConnection($c); |
73
|
|
|
$this->setDatabaseCredentials($c); |
74
|
|
|
$this->setDatabasePostgreSqlOpts($c); |
75
|
|
|
$this->setDatabaseMySqlOpts($c); |
76
|
|
|
$this->setDatabaseAbstractionOpts($c); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|