|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of orm |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Slick\Orm\OrmModule\EventHandlers; |
|
13
|
|
|
|
|
14
|
|
|
use Slick\Orm\OrmModule; |
|
15
|
|
|
use Slick\Orm\OrmModule\ModuleEventHandler; |
|
16
|
|
|
use function PHPUnit\Framework\stringContains; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* OrmHandler |
|
20
|
|
|
* |
|
21
|
|
|
* @package Slick\Orm\OrmModule\EventHandlers |
|
22
|
|
|
*/ |
|
23
|
|
|
final class OrmHandler implements ModuleEventHandler |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
private static string $defaultSettings = <<<EOS |
|
27
|
|
|
<?php |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* This file is part of orm module configuration. |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
use Slick\Orm\Infrastructure\Persistence\ManagerSettings; |
|
34
|
|
|
|
|
35
|
|
|
return [ |
|
36
|
|
|
"databases" => [ |
|
37
|
|
|
"default" => [ |
|
38
|
|
|
"url" => isset(\$_ENV["DATABASE_URL"]) ? \$_ENV["DATABASE_URL"] : 'pdo-sqlite:///data/database.sqlite', |
|
39
|
|
|
"devMode" => getenv("APP_ENV") === "develop", |
|
40
|
|
|
'entityPaths' => ["/src/Domain"], |
|
41
|
|
|
'implDriver' => ManagerSettings::ATTRIBUTE_DRIVER_IMPL |
|
42
|
|
|
] |
|
43
|
|
|
], |
|
44
|
|
|
"types" => [ |
|
45
|
|
|
] |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
EOS; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
*/ |
|
53
|
|
|
public function onEnable(array $context = []): void |
|
54
|
|
|
{ |
|
55
|
|
|
if (is_file(OrmModule::$ormCnfFile)) { |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$folders = [OrmModule::$appConfig.'/modules', APP_ROOT .'/data']; |
|
60
|
|
|
|
|
61
|
|
|
array_walk($folders, function (string $path) { |
|
62
|
|
|
if (!is_dir($path)) { |
|
63
|
|
|
mkdir($path, 0755, true); |
|
64
|
|
|
} |
|
65
|
|
|
}); |
|
66
|
|
|
|
|
67
|
|
|
file_put_contents(OrmModule::$ormCnfFile, self::$defaultSettings); |
|
68
|
|
|
$this->verifyEnvironment(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function onDisable(array $context = []): void |
|
75
|
|
|
{ |
|
76
|
|
|
if (!$context['purge']) { |
|
77
|
|
|
return; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
if (is_file(OrmModule::$ormCnfFile)) { |
|
81
|
|
|
unlink(OrmModule::$ormCnfFile); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
|
|
86
|
|
|
private function verifyEnvironment(): void |
|
87
|
|
|
{ |
|
88
|
|
|
$file = APP_ROOT . '/.env'; |
|
89
|
|
|
$append = [ |
|
90
|
|
|
'# Data base DSN for the default connection.', |
|
91
|
|
|
'# This will be used in the config/modules/orm.php settings file.', |
|
92
|
|
|
'', |
|
93
|
|
|
'# DATABASE_URL=pdo-mysql://user:pass@localhost:3306/database?charset=utf8' |
|
94
|
|
|
]; |
|
95
|
|
|
|
|
96
|
|
|
if (!file_exists($file)) { |
|
97
|
|
|
file_put_contents($file, implode("\n", $append)); |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$content = file_get_contents($file); |
|
102
|
|
|
if (is_string($content) && str_contains($content, 'DATABASE_URL=')) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
file_put_contents($file, $content . "\n" . implode("\n", $append)); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|