1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NettePhoenix; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Nette\DI\Container; |
7
|
|
|
|
8
|
|
|
final class ConfigParser |
9
|
|
|
{ |
10
|
|
|
private const ENVIRONMENT = 'local'; |
11
|
|
|
|
12
|
|
|
private $container; |
13
|
|
|
|
14
|
|
|
private $migrationDirs = []; |
15
|
|
|
|
16
|
|
|
private $logTableName = 'phoenix_log'; |
17
|
|
|
|
18
|
|
|
public function __construct(Container $container) |
19
|
4 |
|
{ |
20
|
|
|
$this->container = $container; |
21
|
4 |
|
} |
22
|
4 |
|
|
23
|
|
|
public function addMigrationDir(string $migrationDir, string $dirName = null): ConfigParser |
24
|
4 |
|
{ |
25
|
|
|
if (!$dirName) { |
26
|
4 |
|
$this->migrationDirs[] = $migrationDir; |
27
|
4 |
|
return $this; |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
if (isset($this->migrationDirs[$dirName])) { |
31
|
|
|
throw new InvalidArgumentException('Migration dir with name "' . $dirName . '" already exists'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$this->migrationDirs[$dirName] = $migrationDir; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function setLogTableName(string $logTableName): ConfigParser |
39
|
2 |
|
{ |
40
|
|
|
$this->logTableName = $logTableName; |
41
|
2 |
|
return $this; |
42
|
2 |
|
} |
43
|
|
|
|
44
|
|
|
public function createConfig(): array |
45
|
2 |
|
{ |
46
|
|
|
$configData = [ |
47
|
2 |
|
'migration_dirs' => $this->migrationDirs, |
48
|
2 |
|
'default_environment' => self::ENVIRONMENT, |
49
|
|
|
'log_table_name' => $this->logTableName, |
50
|
|
|
]; |
51
|
4 |
|
$parameters = $this->container->getParameters(); |
52
|
|
|
$dbData = $parameters['database']['default']; |
53
|
|
|
$charset = $dbData['charset'] ?? ($dbData['adapter'] === 'mysql' ? 'utf8mb4' : 'utf8'); |
54
|
4 |
|
$configData['environments'][self::ENVIRONMENT] = [ |
55
|
4 |
|
'adapter' => $dbData['adapter'], |
56
|
4 |
|
'host' => $dbData['host'], |
57
|
|
|
'username' => $dbData['user'], |
58
|
4 |
|
'password' => $dbData['password'], |
59
|
4 |
|
'db_name' => $dbData['dbname'], |
60
|
|
|
'charset' => $charset, |
61
|
4 |
|
'collation' => $dbData['collation'] ?? ($dbData['adapter'] === 'mysql' && $charset === 'utf8mb4' ? 'utf8mb4_general_ci' : null), |
62
|
|
|
]; |
63
|
|
|
if (isset($dbData['port'])) { |
64
|
4 |
|
$configData['environments'][self::ENVIRONMENT]['port'] = $dbData['port']; |
65
|
4 |
|
} |
66
|
|
|
return $configData; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|