Completed
Push — master ( 51d11c...72fa5d )
by Rougin
01:42
created

Manager::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Rougin\Refinery;
4
5
use Rougin\SparkPlug\SparkPlug;
6
7
/**
8
 * Manager
9
 *
10
 * @package Refinery
11
 * @author  Rougin Royce Gutib <[email protected]>
12
 */
13
class Manager
14
{
15
    /**
16
     * @var \Rougin\SparkPlug\SparkPlug
17
     */
18
    protected $ci;
19
20
    /**
21
     * @var string
22
     */
23
    protected $path;
24
25
    /**
26
     * Initializes the migration instance.
27
     *
28
     * @param string $path
29
     */
30
    public function __construct($path)
31
    {
32
        $globals = array('PRJK' => 'Refinery');
33
34
        $ci = new SparkPlug($globals, array());
35
36
        substr($path, -1) !== '/' && $path .= '/';
37
38
        $this->path = (string) $path;
39
40
        $this->ci = $ci->set('APPPATH', $path);
41
    }
42
43
    /**
44
     * Creates a new file to the "migrations" directory.
45
     *
46
     * @param  string $filename
47
     * @param  string $content
48
     * @return void
49
     */
50
    public function create($filename, $content)
51
    {
52
        $path = $this->path . '/migrations/';
53
54
        $filename = (string) $path . $filename;
55
56
        file_put_contents($filename, $content);
57
    }
58
59
    /**
60
     * Returns the current migration version.
61
     *
62
     * @return string
63
     */
64
    public function current()
65
    {
66
        return $this->get('migration_version');
67
    }
68
69
    /**
70
     * Returns a generated migration filename.
71
     *
72
     * @param  string $name
73
     * @return string
74
     */
75
    public function filename($name)
76
    {
77
        $type = $this->get('migration_type', "'timestamp'");
78
79
        $prefix = (string) date('YmdHis', (integer) time());
80
81
        if ($type === "'sequential'") {
82
            $path = (string) $this->path . '/migrations';
83
84
            $path = new \FilesystemIterator($path);
85
86
            $regex = new \RegexIterator($path, '/^.+\.php$/i');
87
88
            $items = count(iterator_to_array($regex));
89
90
            $prefix = (string) sprintf('%03d', $items + 1);
91
        }
92
93
        return (string) $prefix . '_' . $name . '.php';
94
    }
95
96
    /**
97
     * Returns an array of database migrations.
98
     *
99
     * @return array
100
     */
101
    public function migrations()
102
    {
103
        $migration = $this->load();
104
105
        $items = $migration->find_migrations();
106
107
        $this->set('migration_enabled', 'false');
108
109
        return (array) $items;
110
    }
111
112
    /**
113
     * Migrates to a specific schema version.
114
     *
115
     * @param  string $version
116
     * @return boolean|string
117
     */
118
    public function migrate($version = null)
119
    {
120
        $migration = $this->load();
121
122
        if ($version !== null) {
123
            $result = $migration->version($version);
124
        } else {
125
            $result = $migration->latest();
126
        }
127
128
        if ($result !== true) {
129
            $this->set('migration_version', $result);
130
        }
131
132
        $this->set('migration_enabled', 'false');
133
134
        return $result;
135
    }
136
137
    /**
138
     * Resets the migration schema.
139
     *
140
     * @return boolean
141
     */
142
    public function reset()
143
    {
144
        $migration = $this->load();
145
146
        $result = (string) $migration->version(0);
147
148
        $result = $result === '000' ? 0 : $result;
149
150
        $this->set('migration_version', $result);
151
152
        $this->set('migration_enabled', 'false');
153
154
        return (string) $result;
155
    }
156
157
    /**
158
     * Returns a value from migration configuration.
159
     *
160
     * @param  string $key
161
     * @param  mixed  $default
162
     * @return mixed
163
     */
164
    protected function get($key, $default = null)
165
    {
166
        $file = $this->path . '/config/migration.php';
167
168
        $config = file_get_contents((string) $file);
169
170
        $pattern = '/\$config\[\\\'' . $key . '\\\'\] = (.*?);/i';
171
172
        preg_match($pattern, $config, $matches);
173
174
        return $matches ? $matches[1] : $default;
175
    }
176
177
    /**
178
     * Loads the migration instance.
179
     *
180
     * @return \CI_Migration
181
     */
182
    protected function load()
183
    {
184
        $key = 'migration_enabled';
185
186
        $ci = $this->ci->instance();
187
188
        $this->set($key, 'true');
189
190
        $ci->load->library('migration');
0 ignored issues
show
Bug introduced by
The property load does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
191
192
        return $ci->migration;
0 ignored issues
show
Bug introduced by
The property migration does not seem to exist in CI_Controller.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
193
    }
194
195
    /**
196
     * Changes the value from migration configuration.
197
     *
198
     * @param  string $key
199
     * @param  string $value
200
     * @return void
201
     */
202
    protected function set($key, $value)
203
    {
204
        $file = (string) $this->path . '/config/migration.php';
205
206
        $replace = '$config[\'' . $key . '\'] = ' . $value . ';';
207
208
        $config = file_get_contents((string) $file);
209
210
        $pattern = '/\$config\[\\\'' . $key . '\\\'\] = (.*?);/i';
211
212
        $result = preg_replace($pattern, $replace, $config);
213
214
        file_put_contents((string) $file, (string) $result);
215
    }
216
}
217