|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
4
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata; |
|
9
|
|
|
use Doctrine\Common\Proxy\ProxyGenerator; |
|
10
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
|
11
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
|
12
|
|
|
use midgard\portable\storage\connection; |
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use midgard\portable\command\schema; |
|
15
|
|
|
|
|
16
|
|
|
class midgard_storage |
|
17
|
|
|
{ |
|
18
|
3 |
|
public static function create_base_storage() : bool |
|
19
|
|
|
{ |
|
20
|
3 |
|
$em = connection::get_em(); |
|
21
|
|
|
|
|
22
|
3 |
|
if (!self::create_class_storage(connection::get_fqcn('midgard_repligard'))) { |
|
23
|
|
|
return false; |
|
24
|
|
|
} |
|
25
|
3 |
|
if (!self::create_class_storage(connection::get_fqcn('midgard_person'))) { |
|
26
|
|
|
return false; |
|
27
|
|
|
} |
|
28
|
3 |
|
if (!self::create_class_storage(connection::get_fqcn('midgard_user'))) { |
|
29
|
|
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
$admin = $em->find(connection::get_fqcn('midgard_user'), 1); |
|
33
|
|
|
|
|
34
|
3 |
|
if ($admin === null) { |
|
35
|
3 |
|
$fqcn = connection::get_fqcn('midgard_person'); |
|
36
|
3 |
|
$person = new $fqcn; |
|
37
|
3 |
|
$person->firstname = 'Midgard'; |
|
38
|
3 |
|
$person->lastname = 'Administrator'; |
|
39
|
3 |
|
$person->create(); |
|
40
|
|
|
|
|
41
|
3 |
|
$fqcn = connection::get_fqcn('midgard_user'); |
|
42
|
3 |
|
$admin = new $fqcn; |
|
43
|
3 |
|
$admin->authtype = 'Legacy'; |
|
44
|
3 |
|
$admin->authtypeid = 2; |
|
45
|
3 |
|
$admin->login = 'admin'; |
|
46
|
3 |
|
$admin->password = password_hash('password', PASSWORD_DEFAULT); |
|
47
|
3 |
|
$admin->active = true; |
|
48
|
3 |
|
$admin->usertype = 2; |
|
49
|
3 |
|
$admin->set_person($person); |
|
50
|
3 |
|
$admin->create(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
3 |
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
5 |
|
public static function create_class_storage(string $classname) : bool |
|
57
|
|
|
{ |
|
58
|
5 |
|
$em = connection::get_em(); |
|
59
|
|
|
|
|
60
|
5 |
|
$cm = self::get_cm($em, $classname); |
|
61
|
5 |
|
if ($cm === null) { |
|
62
|
1 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
5 |
|
if (!$em->getConnection()->createSchemaManager()->tablesExist([$cm->getTableName()])) { |
|
66
|
5 |
|
$tool = new SchemaTool($em); |
|
67
|
5 |
|
$tool->createSchema([$cm]); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
5 |
|
self::generate_proxyfile($cm); |
|
71
|
|
|
|
|
72
|
5 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
5 |
|
private static function generate_proxyfile(ClassMetadata $cm) |
|
76
|
|
|
{ |
|
77
|
5 |
|
$em = connection::get_em(); |
|
78
|
5 |
|
$generator = new ProxyGenerator($em->getConfiguration()->getProxyDir(), $em->getConfiguration()->getProxyNamespace()); |
|
|
|
|
|
|
79
|
5 |
|
$generator->setPlaceholder('baseProxyInterface', 'Doctrine\ORM\Proxy\Proxy'); |
|
80
|
5 |
|
$filename = $generator->getProxyFileName($cm->getName()); |
|
81
|
5 |
|
if (file_exists($filename)) { |
|
82
|
3 |
|
unlink($filename); |
|
83
|
|
|
} |
|
84
|
5 |
|
$generator->generateProxyClass($cm, $filename); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
5 |
|
private static function get_cm(EntityManager $em, string $classname) : ?\Doctrine\ORM\Mapping\ClassMetadata |
|
88
|
|
|
{ |
|
89
|
5 |
|
if (!class_exists($classname)) { |
|
90
|
|
|
// if the class doesn't exist (e.g. for some_random_string), there is really nothing we could do |
|
91
|
2 |
|
return null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
5 |
|
$factory = $em->getMetadataFactory(); |
|
95
|
|
|
try { |
|
96
|
5 |
|
return $factory->getMetadataFor($classname); |
|
97
|
3 |
|
} catch (MappingException) { |
|
98
|
|
|
// add namespace |
|
99
|
3 |
|
$classname = connection::get_fqcn($classname); |
|
100
|
|
|
try { |
|
101
|
3 |
|
return $factory->getMetadataFor($classname); |
|
102
|
|
|
} catch (MappingException) { |
|
103
|
|
|
// check for merged classes (duplicate tablenames) |
|
104
|
|
|
$classname = get_class(new $classname); |
|
105
|
|
|
return $factory->getMetadataFor($classname); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Update DB table according to MgdSchema information. |
|
112
|
|
|
* |
|
113
|
|
|
* this does not use SchemaTool's updateSchema, since this would delete columns that are no longer |
|
114
|
|
|
* in the MgdSchema definition |
|
115
|
|
|
*/ |
|
116
|
1 |
|
public static function update_class_storage(string $classname) : bool |
|
117
|
|
|
{ |
|
118
|
1 |
|
$em = connection::get_em(); |
|
119
|
1 |
|
$cm = self::get_cm($em, $classname); |
|
120
|
1 |
|
if ($cm === null) { |
|
121
|
|
|
return false; |
|
122
|
|
|
} |
|
123
|
1 |
|
$sm = $em->getConnection()->createSchemaManager(); |
|
124
|
1 |
|
if ($sm->tablesExist([$cm->getTableName()])) { |
|
125
|
1 |
|
$tool = new SchemaTool($em); |
|
126
|
1 |
|
$conn = $em->getConnection(); |
|
127
|
1 |
|
$from = $sm->introspectSchema(); |
|
128
|
1 |
|
$to = $tool->getSchemaFromMetadata([$cm]); |
|
129
|
1 |
|
$diff = schema::diff($from, $to, false); |
|
130
|
1 |
|
$sql = $conn->getDatabasePlatform()->getAlterSchemaSQL($diff); |
|
131
|
|
|
|
|
132
|
1 |
|
foreach ($sql as $sql_line) { |
|
133
|
1 |
|
$conn->executeQuery($sql_line); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
self::generate_proxyfile($cm); |
|
137
|
|
|
|
|
138
|
1 |
|
return true; |
|
139
|
|
|
} |
|
140
|
|
|
return false; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
public static function class_storage_exists(string $classname) : bool |
|
144
|
|
|
{ |
|
145
|
1 |
|
$em = connection::get_em(); |
|
146
|
1 |
|
if ($cm = self::get_cm($em, $classname)) { |
|
147
|
1 |
|
return $em->getConnection()->createSchemaManager()->tablesExist([$cm->getTableName()]); |
|
148
|
|
|
} |
|
149
|
1 |
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|