1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
<?php
/**
* Do database migrations
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jeroen Roos
*/
namespace upgrade;
use settings;
/**
* Database migrations
*
* @package Zoph
* @author Jeroen Roos
*/
class migrations {
private $todo = array();
private $down = array();
private $dir = "";
private $migrations = array();
/**
* Create migrations class and load migrations
* @param string directory to load from
* @codeCoverageIgnore - run in test setup
*/
public function __construct(string $dir = "migrations/") {
$this->dir = settings::$phpLocation . "/" . $dir;
if (!defined("MIGRATIONS")) {
define("MIGRATIONS", 1);
$migrations = glob($this->dir . "/*.migration.php");
foreach ($migrations as $migration) {
$this->register(require_once($migration));
}
}
}
/**
* Register a migration
* @param migration migration to register
*/
public function register(migration $migration) {
$this->migrations[]=$migration;
}
/**
* Check whether there are any migrations pending
* @param string current version
* @return bool whether there are any pending migrations
*/
public function check(string $version) {
$this->todo=array();
$this->down=array();
foreach ($this->migrations as $migration) {
if ($migration->isRequired($version)) {
$this->todo[] = $migration;
}
if ($migration->isCurrent($version)) {
$this->down[] = $migration;
}
}
return !empty($this->todo);
}
/**
* Get pending migrations
* @return array pending migrations
*/
public function get() {
return $this->todo;
}
/**
* Get 'down' migration
* Get migrations available to migrate down - at this moment this is always one, the last, migration
* @return array down migrations
*/
public function getDown() {
return $this->down;
}
}
|