Completed
Pull Request — master (#27041)
by Thomas
12:53
created

GenerateCommand::getTemplate()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 4
nop 1
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Thomas Müller <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud GmbH.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Core\Command\Db\Migrations;
23
24
25
use OC\DB\MigrationService;
26
use OCP\IConfig;
27
use OCP\IDBConnection;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Exception\RuntimeException;
30
use Symfony\Component\Console\Input\InputArgument;
31
use Symfony\Component\Console\Input\InputInterface;
32
use Symfony\Component\Console\Output\OutputInterface;
33
34
class GenerateCommand extends Command {
35
36
	private static $_templateSimple =
37
		'<?php
38
namespace <namespace>;
39
40
use OCP\Migration\ISimpleMigration;
41
use OCP\Migration\IOutput;
42
43
/**
44
 * Auto-generated migration step: Please modify to your needs!
45
 */
46
class Version<version> implements ISimpleMigration {
47
48
    /**
49
     * @param IOutput $out
50
     */
51
    public function run(IOutput $out) {
52
        // auto-generated - please modify it to your needs
53
    }
54
}
55
';
56
57
	private static $_templateSchema =
58
		'<?php
59
namespace <namespace>;
60
61
use Doctrine\DBAL\Schema\Schema;
62
use OCP\Migration\ISchemaMigration;
63
64
/**
65
 * Auto-generated migration step: Please modify to your needs!
66
 */
67
class Version<version> implements ISchemaMigration {
68
69
	public function changeSchema(Schema $schema, array $options) {
70
		// auto-generated - please modify it to your needs
71
    }
72
}
73
';
74
75
	private static $_templateSql =
76
		'<?php
77
namespace <namespace>;
78
79
use OCP\IDBConnection;
80
use OCP\Migration\ISqlMigration;
81
82
/**
83
 * Auto-generated migration step: Please modify to your needs!
84
 */
85
class Version<version> implements ISqlMigration {
86
87
	public function sql(IDBConnection $connection) {
88
		// auto-generated - please modify it to your needs
89
    }
90
}
91
';
92
93
	/** @var IDBConnection */
94
	private $connection;
95
96
	/**
97
	 * @param IDBConnection $connection
98
	 */
99
	public function __construct(IDBConnection $connection) {
100
		$this->connection = $connection;
101
102
		parent::__construct();
103
	}
104
105
	protected function configure() {
106
		$this
107
			->setName('migrations:generate')
108
			->addArgument('app', InputArgument::REQUIRED, 'Name of the app this migration command shall work on')
109
			->addArgument('kind', InputArgument::REQUIRED, 'simple, schema or sql - defines the kind of migration to be generated');
110
111
		parent::configure();
112
	}
113
114
	public function execute(InputInterface $input, OutputInterface $output) {
115
		$appName = $input->getArgument('app');
116
		$ms = new MigrationService($appName, $this->connection);
117
118
		$kind = $input->getArgument('kind');
119
		$version = date('YmdHis');
120
		$path = $this->generateMigration($ms, $version, $kind);
121
122
		$output->writeln("New migration class has been generated to <info>$path</info>");
123
124
	}
125
126
	/**
127
	 * @param MigrationService $ms
128
	 * @param string $version
129
	 * @param string $kind
130
	 * @return string
131
	 */
132
	private function generateMigration(MigrationService $ms, $version, $kind) {
133
		$placeHolders = [
134
			'<namespace>',
135
			'<version>',
136
		];
137
		$replacements = [
138
			$ms->getMigrationsNamespace(),
139
			$version,
140
		];
141
		$code = str_replace($placeHolders, $replacements, $this->getTemplate($kind));
142
		$dir = $ms->getMigrationsDirectory();
143
		$path = $dir . '/Version' . $version . '.php';
144
145
		if (file_put_contents($path, $code) === false) {
146
			throw new RuntimeException('Failed to generate new migration step.');
147
		}
148
149
		return $path;
150
	}
151
152
	private function getTemplate($kind) {
153
		if ($kind === 'simple') {
154
			return self::$_templateSimple;
155
		}
156
		if ($kind === 'schema') {
157
			return self::$_templateSchema;
158
		}
159
		if ($kind === 'sql') {
160
			return self::$_templateSql;
161
		}
162
		throw new \InvalidArgumentException('Kind can only be one of the following: simple, schema or sql');
163
	}
164
165
}
166