Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
schema_generator.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\db\migration;
015
016 /**
017 * The schema generator generates the schema based on the existing migrations
018 */
019 class schema_generator
020 {
021 /** @var \phpbb\config\config */
022 protected $config;
023
024 /** @var \phpbb\db\driver\driver_interface */
025 protected $db;
026
027 /** @var \phpbb\db\tools\tools_interface */
028 protected $db_tools;
029
030 /** @var array */
031 protected $class_names;
032
033 /** @var string */
034 protected $table_prefix;
035
036 /** @var string */
037 protected $phpbb_root_path;
038
039 /** @var string */
040 protected $php_ext;
041
042 /** @var array */
043 protected $tables;
044
045 /** @var array */
046 protected $dependencies = array();
047
048 /**
049 * Constructor
050 */
051 public function __construct(array $class_names, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\db\tools\tools_interface $db_tools, $phpbb_root_path, $php_ext, $table_prefix)
052 {
053 $this->config = $config;
054 $this->db = $db;
055 $this->db_tools = $db_tools;
056 $this->class_names = $class_names;
057 $this->phpbb_root_path = $phpbb_root_path;
058 $this->php_ext = $php_ext;
059 $this->table_prefix = $table_prefix;
060 }
061
062 /**
063 * Loads all migrations and their application state from the database.
064 *
065 * @return array
066 */
067 public function get_schema()
068 {
069 if (!empty($this->tables))
070 {
071 return $this->tables;
072 }
073
074 $migrations = $this->class_names;
075
076 $tree = array();
077 $check_dependencies = true;
078 while (!empty($migrations))
079 {
080 foreach ($migrations as $key => $migration_class)
081 {
082 // Unset classes that are not a valid migration
083 if (\phpbb\db\migrator::is_migration($migration_class) === false)
084 {
085 unset($migrations[$key]);
086 continue;
087 }
088
089 $open_dependencies = array_diff($migration_class::depends_on(), $tree);
090
091 if (empty($open_dependencies))
092 {
093 $migration = new $migration_class($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
094 $tree[] = $migration_class;
095 $migration_key = array_search($migration_class, $migrations);
096
097 foreach ($migration->update_schema() as $change_type => $data)
098 {
099 if ($change_type === 'add_tables')
100 {
101 foreach ($data as $table => $table_data)
102 {
103 $this->tables[$table] = $table_data;
104 }
105 }
106 else if ($change_type === 'drop_tables')
107 {
108 foreach ($data as $table)
109 {
110 unset($this->tables[$table]);
111 }
112 }
113 else if ($change_type === 'add_columns')
114 {
115 foreach ($data as $table => $add_columns)
116 {
117 foreach ($add_columns as $column => $column_data)
118 {
119 if (isset($column_data['after']))
120 {
121 $columns = $this->tables[$table]['COLUMNS'];
122 $offset = array_search($column_data['after'], array_keys($columns));
123 unset($column_data['after']);
124
125 if ($offset === false)
126 {
127 $this->tables[$table]['COLUMNS'][$column] = array_values($column_data);
128 }
129 else
130 {
131 $this->tables[$table]['COLUMNS'] = array_merge(array_slice($columns, 0, $offset + 1, true), array($column => array_values($column_data)), array_slice($columns, $offset));
132 }
133 }
134 else
135 {
136 $this->tables[$table]['COLUMNS'][$column] = $column_data;
137 }
138 }
139 }
140 }
141 else if ($change_type === 'change_columns')
142 {
143 foreach ($data as $table => $change_columns)
144 {
145 foreach ($change_columns as $column => $column_data)
146 {
147 $this->tables[$table]['COLUMNS'][$column] = $column_data;
148 }
149 }
150 }
151 else if ($change_type === 'drop_columns')
152 {
153 foreach ($data as $table => $drop_columns)
154 {
155 if (is_array($drop_columns))
156 {
157 foreach ($drop_columns as $column)
158 {
159 unset($this->tables[$table]['COLUMNS'][$column]);
160 }
161 }
162 else
163 {
164 unset($this->tables[$table]['COLUMNS'][$drop_columns]);
165 }
166 }
167 }
168 else if ($change_type === 'add_unique_index')
169 {
170 foreach ($data as $table => $add_index)
171 {
172 foreach ($add_index as $key => $index_data)
173 {
174 $this->tables[$table]['KEYS'][$key] = array('UNIQUE', $index_data);
175 }
176 }
177 }
178 else if ($change_type === 'add_index')
179 {
180 foreach ($data as $table => $add_index)
181 {
182 foreach ($add_index as $key => $index_data)
183 {
184 $this->tables[$table]['KEYS'][$key] = array('INDEX', $index_data);
185 }
186 }
187 }
188 else if ($change_type === 'drop_keys')
189 {
190 foreach ($data as $table => $drop_keys)
191 {
192 foreach ($drop_keys as $key)
193 {
194 unset($this->tables[$table]['KEYS'][$key]);
195 }
196 }
197 }
198 else
199 {
200 var_dump($change_type);
201 }
202 }
203 unset($migrations[$migration_key]);
204 }
205 else if ($check_dependencies)
206 {
207 $this->dependencies = array_merge($this->dependencies, $open_dependencies);
208 }
209 }
210
211 // Only run this check after the first run
212 if ($check_dependencies)
213 {
214 $this->check_dependencies();
215 $check_dependencies = false;
216 }
217 }
218
219 ksort($this->tables);
220 return $this->tables;
221 }
222
223 /**
224 * Check if one of the migrations files' dependencies can't be resolved
225 * by the supplied list of migrations
226 *
227 * @throws \UnexpectedValueException If a dependency can't be resolved
228 */
229 protected function check_dependencies()
230 {
231 // Strip duplicate values from array
232 $this->dependencies = array_unique($this->dependencies);
233
234 foreach ($this->dependencies as $dependency)
235 {
236 if (!in_array($dependency, $this->class_names))
237 {
238 throw new \UnexpectedValueException("Unable to resolve the dependency '$dependency'");
239 }
240 }
241 }
242 }
243