[ Index ]

PHP Cross Reference of phpBB-3.1.12-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 */
  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 $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 $migration_class)
  81              {
  82                  $open_dependencies = array_diff($migration_class::depends_on(), $tree);
  83  
  84                  if (empty($open_dependencies))
  85                  {
  86                      $migration = new $migration_class($this->config, $this->db, $this->db_tools, $this->phpbb_root_path, $this->php_ext, $this->table_prefix);
  87                      $tree[] = $migration_class;
  88                      $migration_key = array_search($migration_class, $migrations);
  89  
  90                      foreach ($migration->update_schema() as $change_type => $data)
  91                      {
  92                          if ($change_type === 'add_tables')
  93                          {
  94                              foreach ($data as $table => $table_data)
  95                              {
  96                                  $this->tables[$table] = $table_data;
  97                              }
  98                          }
  99                          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  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1