aboutsummaryrefslogtreecommitdiff
path: root/lib/migrations/Migration.php
blob: 355bfe2dee502d2acfe32145468886f72721ccf6 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
<?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()
    {
    }

    /**
     * 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'])) {
            return;
        }

        $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
     */
    protected function announce($format /* , ... */)
    {
        # format message
        $args = func_get_args();
        $message = vsprintf(array_shift($args), $args);

        return $this->write(Migrator::mark($message));
    }
}