Completed
Pull Request — master (#1)
by Rougin
02:21
created

AbstractCommand::getMigrations()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Rougin\Refinery\Commands;
4
5
use Rougin\Describe\Describe;
6
use League\Flysystem\Filesystem;
7
8
/**
9
 * Abstract Command
10
 *
11
 * @package Refinery
12
 * @author  Rougin Royce Gutib <[email protected]>
13
 */
14
abstract class AbstractCommand extends \Symfony\Component\Console\Command\Command
15
{
16
    /**
17
     * @var \CI_Controller
18
     */
19
    protected $codeigniter;
20
21
    /**
22
     * @var \Rougin\Describe\Describe
23
     */
24
    protected $describe;
25
26
    /**
27
     * @var \League\Flysystem\Filesystem
28
     */
29
    protected $filesystem;
30
31
    /**
32
     * @var \Twig_Environment
33
     */
34
    protected $renderer;
35
36
    /**
37
     * @param \CI_Controller               $codeigniter
38
     * @param \Rougin\Describe\Describe    $describe
39
     * @param \League\Flysystem\Filesystem $filesystem
40
     * @param \Twig_Environment            $renderer
41
     */
42 3
    public function __construct(\CI_Controller $codeigniter, Describe $describe, Filesystem $filesystem, \Twig_Environment $renderer)
43
    {
44 3
        parent::__construct();
45
46 3
        $this->codeigniter = $codeigniter;
47 3
        $this->describe    = $describe;
48 3
        $this->filesystem  = $filesystem;
49 3
        $this->renderer    = $renderer;
50 3
    }
51
52
    /**
53
     * Changes the migration version.
54
     *
55
     * @param  integer $current
56
     * @param  integer $timestamp
57
     * @return void
58
     */
59
    public function changeVersion($current, $timestamp)
60
    {
61
        $old = '$config[\'migration_version\'] = ' . $current . ';';
62
        $new = '$config[\'migration_version\'] = ' . $timestamp . ';';
63
64
        $config = $this->filesystem->read('application/config/migration.php');
65
        $config = str_replace($old, $new, $config);
66
67
        $this->filesystem->update('application/config/migration.php', $config);
68
    }
69
70
    /**
71
     * Gets the latest migration version
72
     *
73
     * @return string
74
     */
75
    public function getLatestVersion()
76
    {
77
        $config  = $this->filesystem->read('application/config/migration.php');
78
        $pattern = '/\$config\[\'migration_version\'\] = (\d+);/';
79
80
        preg_match_all($pattern, $config, $match);
81
82
        return $match[1][0];
83
    }
84
85
    /**
86
     * Gets list of migrations from the specified directory.
87
     *
88
     * @param  string $path
89
     * @return array
90
     */
91
    public function getMigrations($path)
92
    {
93
        $filenames  = [];
94
        $migrations = [];
95
96
        // Searches a listing of migration files and sorts them after
97
        $directory = new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS);
98
        $iterator  = new \RecursiveIteratorIterator($directory, \RecursiveIteratorIterator::SELF_FIRST);
99
100
        foreach ($iterator as $path) {
101
            array_push($filenames, str_replace('.php', '', $path->getFilename()));
102
103
            $migration = substr($path->getFilename(), 0, 14);
104
105
            is_numeric($migration) || $migration = substr($path->getFilename(), 0, 3);
106
107
            array_push($migrations, $migration);
108
        }
109
110
        sort($filenames);
111
        sort($migrations);
112
113
        return [ $filenames, $migrations ];
114
    }
115
116
    /**
117
     * Checks whether the command is enabled or not in the current environment.
118
     *
119
     * @return boolean
120
     */
121
    public function isEnabled()
122
    {
123
        $migrations = glob(APPPATH . 'migrations/*.php');
124
125
        return count($migrations) > 0;
126
    }
127
128
    /**
129
     * Enables/disables the Migration Class.
130
     *
131
     * @param  boolean $enabled
132
     * @return void
133
     */
134
    public function toggleMigration($enabled = false)
135
    {
136
        $old = [ '$config[\'migration_enabled\'] = TRUE;', '$config[\'migration_enabled\'] = true;' ];
137
        $new = [ '$config[\'migration_enabled\'] = FALSE;', '$config[\'migration_enabled\'] = false;' ];
138
139
        if ($enabled) {
140
            $old = [ '$config[\'migration_enabled\'] = FALSE;', '$config[\'migration_enabled\'] = false;' ];
141
            $new = [ '$config[\'migration_enabled\'] = TRUE;', '$config[\'migration_enabled\'] = true;' ];
142
        }
143
144
        $config = $this->filesystem->read('application/config/migration.php');
145
        $config = str_replace($old, $new, $config);
146
147
        $this->filesystem->update('application/config/migration.php', $config);
148
    }
149
}
150