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\Common\Persistence\Mapping\ClassMetadata; |
9
|
|
|
use Doctrine\Common\Proxy\ProxyGenerator; |
10
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
11
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
12
|
|
|
use Doctrine\ORM\Mapping\MappingException; |
13
|
|
|
use midgard\portable\storage\connection; |
14
|
|
|
|
15
|
|
|
class midgard_storage |
16
|
|
|
{ |
17
|
2 |
|
public static function create_base_storage() |
18
|
|
|
{ |
19
|
2 |
|
$em = connection::get_em(); |
20
|
2 |
|
$ns = $em->getConfiguration()->getEntityNamespace("midgard"); |
21
|
|
|
|
22
|
2 |
|
$cm_repligard = $em->getClassMetadata($ns . '\\midgard_repligard'); |
23
|
2 |
|
if (!self::create_class_storage($cm_repligard->getName())) { |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
2 |
|
$cm_person = $em->getClassMetadata($ns . '\\midgard_person'); |
27
|
2 |
|
if (!self::create_class_storage($cm_person->getName())) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
2 |
|
$cm_user = $em->getClassMetadata($ns . '\\midgard_user'); |
31
|
2 |
|
if (!self::create_class_storage($cm_user->getName())) { |
32
|
|
|
return false; |
33
|
|
|
} |
34
|
|
|
|
35
|
2 |
|
$admin = $em->find('midgard:midgard_user', 1); |
36
|
|
|
|
37
|
2 |
|
if ($admin === null) { |
38
|
2 |
|
$fqcn = $cm_person->getName(); |
39
|
2 |
|
$person = new $fqcn; |
40
|
2 |
|
$person->firstname = 'Midgard'; |
41
|
2 |
|
$person->lastname = 'Administrator'; |
42
|
2 |
|
$person->create(); |
43
|
|
|
|
44
|
2 |
|
$fqcn = $cm_user->getName(); |
45
|
2 |
|
$admin = new $fqcn; |
46
|
2 |
|
$admin->authtype = 'Plaintext'; |
47
|
2 |
|
$admin->authtypeid = 2; |
48
|
2 |
|
$admin->login = 'admin'; |
49
|
2 |
|
$admin->password = 'password'; |
50
|
2 |
|
$admin->active = true; |
51
|
2 |
|
$admin->usertype = 2; |
52
|
2 |
|
$admin->set_person($person); |
53
|
2 |
|
$admin->create(); |
54
|
2 |
|
} |
55
|
|
|
|
56
|
2 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
public static function create_class_storage($classname) |
60
|
1 |
|
{ |
61
|
4 |
|
$em = connection::get_em(); |
62
|
|
|
|
63
|
4 |
|
$cm = self::get_cm($em, $classname); |
64
|
4 |
|
if ($cm === false) { |
65
|
1 |
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
4 |
|
if (!$em->getConnection()->getSchemaManager()->tablesExist([$cm->getTableName()])) { |
69
|
4 |
|
$tool = new SchemaTool($em); |
70
|
4 |
|
$tool->createSchema([$cm]); |
71
|
4 |
|
} |
72
|
|
|
|
73
|
4 |
|
self::generate_proxyfile($cm); |
74
|
|
|
|
75
|
4 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
private static function generate_proxyfile(ClassMetadata $cm) |
79
|
|
|
{ |
80
|
4 |
|
$em = connection::get_em(); |
81
|
4 |
|
$generator = new ProxyGenerator($em->getConfiguration()->getProxyDir(), $em->getConfiguration()->getProxyNamespace()); |
82
|
4 |
|
$generator->setPlaceholder('baseProxyInterface', 'Doctrine\ORM\Proxy\Proxy'); |
83
|
4 |
|
$filename = $generator->getProxyFileName($cm->getName()); |
84
|
4 |
|
if (file_exists($filename)) { |
85
|
3 |
|
unlink($filename); |
86
|
3 |
|
} |
87
|
4 |
|
$generator->generateProxyClass($cm, $filename); |
88
|
4 |
|
} |
89
|
|
|
|
90
|
4 |
|
private static function get_cm($em, $classname) |
91
|
|
|
{ |
92
|
4 |
|
if (!class_exists($classname)) { |
93
|
|
|
// if the class doesn't exist (e.g. for some_random_string), there is really nothing we could do |
94
|
2 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
$factory = $em->getMetadataFactory(); |
98
|
|
|
try { |
99
|
4 |
|
return $factory->getMetadataFor($classname); |
100
|
3 |
|
} catch (MappingException $e) { |
101
|
|
|
// add namespace |
102
|
3 |
|
$classname = $em->getConfiguration()->getEntityNamespace("midgard") . '\\' . $classname; |
103
|
|
|
try { |
104
|
3 |
|
return $factory->getMetadataFor($classname); |
105
|
|
|
} catch (MappingException $e) { |
106
|
|
|
// check for merged classes (duplicate tablenames) |
107
|
2 |
|
$classname = get_class(new $classname); |
108
|
|
|
return $factory->getMetadataFor($classname); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Update DB table according to MgdSchema information. |
115
|
|
|
* |
116
|
|
|
* this does not use SchemaTool's updateSchema, since this would delete columns that are no longer |
117
|
|
|
* in the MgdSchema definition |
118
|
|
|
* |
119
|
|
|
* @param string $classname The MgdSchema class to work on |
120
|
|
|
*/ |
121
|
1 |
|
public static function update_class_storage($classname) |
122
|
|
|
{ |
123
|
1 |
|
$em = connection::get_em(); |
124
|
1 |
|
$cm = self::get_cm($em, $classname); |
125
|
1 |
|
if ($cm === false) { |
126
|
|
|
return false; |
127
|
|
|
} |
128
|
1 |
|
$sm = $em->getConnection()->getSchemaManager(); |
129
|
1 |
|
if ($sm->tablesExist([$cm->getTableName()])) { |
130
|
1 |
|
$tool = new SchemaTool($em); |
131
|
1 |
|
$conn = $em->getConnection(); |
132
|
1 |
|
$from = $sm->createSchema(); |
133
|
1 |
|
$to = $tool->getSchemaFromMetadata([$cm]); |
134
|
|
|
|
135
|
1 |
|
$comparator = new Comparator; |
136
|
1 |
|
$diff = $comparator->compare($from, $to); |
137
|
1 |
|
if (!empty($diff->changedTables[$cm->getTableName()]->removedColumns)) { |
138
|
1 |
|
$diff->changedTables[$cm->getTableName()]->removedColumns = []; |
139
|
1 |
|
} |
140
|
1 |
|
$sql = $diff->toSaveSql($conn->getDatabasePlatform()); |
141
|
|
|
|
142
|
|
|
|
143
|
1 |
|
foreach ($sql as $sql_line) { |
144
|
1 |
|
$conn->executeQuery($sql_line); |
145
|
1 |
|
} |
146
|
|
|
|
147
|
1 |
|
self::generate_proxyfile($cm); |
148
|
|
|
|
149
|
1 |
|
return true; |
150
|
|
|
} |
151
|
|
|
return false; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public static function delete_class_storage($classname) |
|
|
|
|
155
|
|
|
{ |
156
|
|
|
return false; |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
public static function class_storage_exists($classname) |
160
|
|
|
{ |
161
|
1 |
|
$em = connection::get_em(); |
162
|
|
|
|
163
|
1 |
|
$cm = self::get_cm($em, $classname); |
164
|
1 |
|
if ($cm === false) { |
165
|
1 |
|
return false; |
166
|
|
|
} |
167
|
|
|
|
168
|
1 |
|
return $em->getConnection()->getSchemaManager()->tablesExist([$cm->getTableName()]); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.