Newer
Older
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
<?php
/**
* Migration.php - abstract base class for migrations
*
* This class serves as the abstract base class for all migrations.
*
* @author Marcus Lunzenauer <mlunzena@uos.de>
* @copyright 2007 - Marcus Lunzenauer <mlunzena@uos.de>
* @license GPL2 or any later version
* @package migrations
*/
abstract class Migration
{
/**
* use verbose output
*
* @var boolean
*/
private $verbose;
/**
* Initalize a Migration object (optionally using verbose output).
*
* @param boolean $verbose verbose output (default FALSE)
*/
public function __construct($verbose = false)
{
$this->setVerbose($verbose);
}
/**
* Sets the verbose state of this migration.
* @param boolean $state Verbosity state
*/
public function setVerbose($state = true)
{
$this->verbose = (bool) $state;
}
/**
* Abstract method describing this migration step.
* This method should be implemented in a migration subclass.
*
* @return string migration description
*/
public function description()
{

Jan-Hendrik Willms
committed
return '';
/**
* Returns the name of the migration. If the migration is an anonymous
* class, the the name is created from the filename. Otherwise, it's the
* class name of the migration.
*
* @return string
*/
public function getName(): string
{
$reflection = new ReflectionClass($this);
if ($reflection->isAnonymous()) {
$filename = basename($reflection->getFileName(), '.php');
$name = implode(' ', array_slice(explode('_', $filename), 1));
return ucfirst($name);
}
return static::class;
}
/**
* Abstract method performing this migration step.
* This method should be implemented in a migration subclass.
*/
protected function up()
{
}
/**
* Abstract method reverting this migration step.
* This method should be implemented in a migration subclass.
*/
protected function down()
{
}
/**
* Perform or revert this migration, depending on the indicated direction.
*
* @param ?string $direction migration direction (either 'up' or 'down')
*/
public function migrate($direction)
{
if (!in_array($direction, ['up', 'down'])) {
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
}
$result = $this->$direction();
// Reset SORM cache
SimpleORMap::expireTableScheme();
return $result;
}
/**
* Print the given string (if verbose output is enabled).
*
* @param string $text text to print
*/
protected function write($text = '')
{
if ($this->verbose) {
echo "{$text}\n";
}
}
/**
* Print the given formatted string (if verbose output is enabled).
* Output always includes the migration's class name.
*
* @param string $format,... printf-style format string and parameters
*/
{
# format message
$args = func_get_args();
$message = vsprintf(array_shift($args), $args);
/**
* Pads and highlights a given text to a specific length with the given
* sign.
*
* @param string $text
* @param string $sign
*/
protected function mark($text, $sign = '=')
{
$text = trim($text);
if ($text) {
$text = " {$text} ";
}
return str_pad("{$sign}{$sign}{$text}", 79, $sign, STR_PAD_RIGHT);
}