Verzeichnisstruktur phpBB-3.1.0


Veröffentlicht
27.10.2014

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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

schema_generator.php

Zuletzt modifiziert: 09.10.2024, 12:54 - Dateigröße: 5.71 KiB


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 */
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 $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 $migration_class)
081              {
082                  $open_dependencies = array_diff($migration_class::depends_on(), $tree);
083   
084                  if (empty($open_dependencies))
085                  {
086                      $migration = new $migration_class($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
087                      $tree[] = $migration_class;
088                      $migration_key = array_search($migration_class, $migrations);
089   
090                      foreach ($migration->update_schema() as $change_type => $data)
091                      {
092                          if ($change_type === 'add_tables')
093                          {
094                              foreach ($data as $table => $table_data)
095                              {
096                                  $this->tables[$table] = $table_data;
097                              }
098                          }
099                          else if ($change_type === 'drop_tables')
100                          {
101                              foreach ($data as $table)
102                              {
103                                  unset($this->tables[$table]);
104                              }
105                          }
106                          else if ($change_type === 'add_columns')
107                          {
108                              foreach ($data as $table => $add_columns)
109                              {
110                                  foreach ($add_columns as $column => $column_data)
111                                  {
112                                      if (isset($column_data['after']))
113                                      {
114                                          $columns = $this->tables[$table]['COLUMNS'];
115                                          $offset = array_search($column_data['after'], array_keys($columns));
116                                          unset($column_data['after']);
117   
118                                          if ($offset === false)
119                                          {
120                                              $this->tables[$table]['COLUMNS'][$column] = array_values($column_data);
121                                          }
122                                          else
123                                          {
124                                              $this->tables[$table]['COLUMNS'] = array_merge(array_slice($columns, 0, $offset + 1, true), array($column => array_values($column_data)), array_slice($columns, $offset));
125                                          }
126                                      }
127                                      else
128                                      {
129                                          $this->tables[$table]['COLUMNS'][$column] = $column_data;
130                                      }
131                                  }
132                              }
133                          }
134                          else if ($change_type === 'change_columns')
135                          {
136                              foreach ($data as $table => $change_columns)
137                              {
138                                  foreach ($change_columns as $column => $column_data)
139                                  {
140                                      $this->tables[$table]['COLUMNS'][$column] = $column_data;
141                                  }
142                              }
143                          }
144                          else if ($change_type === 'drop_columns')
145                          {
146                              foreach ($data as $table => $drop_columns)
147                              {
148                                  if (is_array($drop_columns))
149                                  {
150                                      foreach ($drop_columns as $column)
151                                      {
152                                          unset($this->tables[$table]['COLUMNS'][$column]);
153                                      }
154                                  }
155                                  else
156                                  {
157                                      unset($this->tables[$table]['COLUMNS'][$drop_columns]);
158                                  }
159                              }
160                          }
161                          else if ($change_type === 'add_unique_index')
162                          {
163                              foreach ($data as $table => $add_index)
164                              {
165                                  foreach ($add_index as $key => $index_data)
166                                  {
167                                      $this->tables[$table]['KEYS'][$key] = array('UNIQUE', $index_data);
168                                  }
169                              }
170                          }
171                          else if ($change_type === 'add_index')
172                          {
173                              foreach ($data as $table => $add_index)
174                              {
175                                  foreach ($add_index as $key => $index_data)
176                                  {
177                                      $this->tables[$table]['KEYS'][$key] = array('INDEX', $index_data);
178                                  }
179                              }
180                          }
181                          else if ($change_type === 'drop_keys')
182                          {
183                              foreach ($data as $table => $drop_keys)
184                              {
185                                  foreach ($drop_keys as $key)
186                                  {
187                                      unset($this->tables[$table]['KEYS'][$key]);
188                                  }
189                              }
190                          }
191                          else
192                          {
193                              var_dump($change_type);
194                          }
195                      }
196                      unset($migrations[$migration_key]);
197                  }
198                  else if ($check_dependencies)
199                  {
200                      $this->dependencies = array_merge($this->dependencies, $open_dependencies);
201                  }
202              }
203   
204              // Only run this check after the first run
205              if ($check_dependencies)
206              {
207                  $this->check_dependencies();
208                  $check_dependencies = false;
209              }
210          }
211   
212          ksort($this->tables);
213          return $this->tables;
214      }
215   
216      /**
217      * Check if one of the migrations files' dependencies can't be resolved
218      * by the supplied list of migrations
219      *
220      * @throws \UnexpectedValueException If a dependency can't be resolved
221      */
222      protected function check_dependencies()
223      {
224          // Strip duplicate values from array
225          $this->dependencies = array_unique($this->dependencies);
226   
227          foreach ($this->dependencies as $dependency)
228          {
229              if (!in_array($dependency, $this->class_names))
230              {
231                  throw new \UnexpectedValueException("Unable to resolve the dependency '$dependency'");
232              }
233          }
234      }
235  }
236