[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/phpbb/db/migration/ -> schema_generator.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\db\migration;
  15  
  16  /**
  17  * The schema generator generates the schema based on the existing migrations
  18  */
  19  class schema_generator
  20  {
  21      /** @var \phpbb\config\config */
  22      protected $config;
  23  
  24      /** @var \phpbb\db\driver\driver_interface */
  25      protected $db;
  26  
  27      /** @var \phpbb\db\tools\tools_interface */
  28      protected $db_tools;
  29  
  30      /** @var array */
  31      protected $class_names;
  32  
  33      /** @var string */
  34      protected $table_prefix;
  35  
  36      /** @var string */
  37      protected $phpbb_root_path;
  38  
  39      /** @var string */
  40      protected $php_ext;
  41  
  42      /** @var array */
  43      protected $tables;
  44  
  45      /** @var array */
  46      protected $dependencies = array();
  47  
  48      /**
  49      * Constructor
  50      */
  51  	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)
  52      {
  53          $this->config = $config;
  54          $this->db = $db;
  55          $this->db_tools = $db_tools;
  56          $this->class_names = $class_names;
  57          $this->phpbb_root_path = $phpbb_root_path;
  58          $this->php_ext = $php_ext;
  59          $this->table_prefix = $table_prefix;
  60      }
  61  
  62      /**
  63      * Loads all migrations and their application state from the database.
  64      *
  65      * @return array
  66      */
  67  	public function get_schema()
  68      {
  69          if (!empty($this->tables))
  70          {
  71              return $this->tables;
  72          }
  73  
  74          $migrations = $this->class_names;
  75  
  76          $tree = array();
  77          $check_dependencies = true;
  78          while (!empty($migrations))
  79          {
  80              foreach ($migrations as $key => $migration_class)
  81              {
  82                  // Unset classes that are not a valid migration
  83                  if (\phpbb\db\migrator::is_migration($migration_class) === false)
  84                  {
  85                      unset($migrations[$key]);
  86                      continue;
  87                  }
  88  
  89                  $open_dependencies = array_diff($migration_class::depends_on(), $tree);
  90  
  91                  if (empty($open_dependencies))
  92                  {
  93                      $migration = new $migration_class($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
  94                      $tree[] = $migration_class;
  95                      $migration_key = array_search($migration_class, $migrations);
  96  
  97                      foreach ($migration->update_schema() as $change_type => $data)
  98                      {
  99                          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  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1