Passed
Push — master ( 94c6b8...b4b452 )
by Aimeos
05:00
created

Base::getPostDependencies()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2015-2018
6
 * @package MW
7
 * @subpackage Setup
8
 */
9
10
11
namespace Aimeos\MW\Setup\Task;
12
13
14
/**
15
 * Base class for all setup tasks
16
 *
17
 * @package MW
18
 * @subpackage Setup
19
 */
20
abstract class Base implements \Aimeos\MW\Setup\Task\Iface
21
{
22
	private $dbm;
23
	private $paths = [];
24
	private $schemas = [];
25
	private $connections = [];
26
	protected $additional;
27
28
	/** @deprecated Use getSchema() instead */
29
	protected $schema;
30
31
	/** @deprecated Use acquire() and release() instead */
32
	protected $conn;
33
34
35
	/**
36
	 * Initializes the task object.
37
	 *
38
	 * @param \Aimeos\MW\Setup\DBSchema\Iface $schema Database schema object
39
	 * @param \Aimeos\MW\DB\Connection\Iface $conn Database connection
40
	 * @param mixed $additional Additionally provided information for the setup tasks if required
41
	 * @param string[] $paths List of paths of the setup tasks ordered by dependencies
42
	 */
43
	public function __construct( \Aimeos\MW\Setup\DBSchema\Iface $schema, \Aimeos\MW\DB\Connection\Iface $conn,
44
		$additional = null, array $paths = [] )
45
	{
46
		$this->connections['db'] = $conn;
47
		$this->schema = $schema;
48
		$this->conn = $conn;
49
		$this->paths = $paths;
50
		$this->additional = $additional;
51
	}
52
53
54
	/**
55
	 * Returns the list of task names which this task depends on
56
	 *
57
	 * @return string[] List of task names
58
	 */
59
	public function getPreDependencies()
60
	{
61
		return [];
62
	}
63
64
65
	/**
66
	 * Returns the list of task names which depends on this task.
67
	 *
68
	 * @return string[] List of task names
69
	 */
70
	public function getPostDependencies()
71
	{
72
		return [];
73
	}
74
75
76
	/**
77
	 * Updates the schema and migrates the data
78
	 *
79
	 * @return void
80
	 */
81
	public function migrate()
82
	{
83
	}
84
85
86
	/**
87
	 * Undo all schema changes and migrate data back
88
	 *
89
	 * @return void
90
	*/
91
	public function rollback()
92
	{
93
	}
94
95
96
	/**
97
	 * Cleans up old data required for roll back
98
	 *
99
	 * @return void
100
	*/
101
	public function clean()
102
	{
103
	}
104
105
106
	/**
107
	 * Sets the database manager object
108
	 *
109
	 * @param \Aimeos\MW\DB\Manager\Iface $dbm Database manager
110
	 */
111
	public function setDatabaseManager( \Aimeos\MW\DB\Manager\Iface $dbm )
112
	{
113
		$this->dbm = $dbm;
114
	}
115
116
117
	/**
118
	 * Sets the associative list of schemas with the resource name as key.
119
	 *
120
	 * @param \Aimeos\MW\Setup\DBSchema\Iface[] $schemas Associative list of schemas
121
	 */
122
	public function setSchemas( array $schemas )
123
	{
124
		$this->schemas = $schemas;
125
	}
126
127
128
	/**
129
	 * Returns the database connection
130
	 *
131
	 * @param string $name Name from the resource configuration
132
	 * @return \Aimeos\MW\DB\Connection\Iface Database connection
133
	 */
134
	protected function acquire( $name = 'db' )
135
	{
136
		return $this->dbm->acquire( $name );
137
	}
138
139
140
	/**
141
	 * Releases the database connection
142
	 *
143
	 * @param \Aimeos\MW\DB\Connection\Iface $conn Database connection
144
	 * @param string $name Name from the resource configuration
145
	 */
146
	protected function release( \Aimeos\MW\DB\Connection\Iface $conn, $name = 'db' )
147
	{
148
		return $this->dbm->release( $conn, $name );
149
	}
150
151
152
	/**
153
	 * Executes a given SQL statement.
154
	 *
155
	 * @param string $sql SQL statement to execute
156
	 * @param string $name Name from the resource configuration
157
	 */
158
	protected function execute( $sql, $name = 'db' )
159
	{
160
		$conn = $this->acquire( $name );
161
		$conn->create( $sql )->execute()->finish();
162
		$this->release( $conn, $name );
163
	}
164
165
166
	/**
167
	 * Executes a list of given SQL statements.
168
	 *
169
	 * @param string[] $list List of SQL statement to execute
170
	 * @param string $name Name from the resource configuration
171
	 */
172
	protected function executeList( array $list, $name = 'db' )
173
	{
174
		$conn = $this->acquire( $name );
175
176
		foreach( $list as $sql ) {
177
			$conn->create( $sql )->execute()->finish();
178
		}
179
180
		$this->release( $conn, $name );
181
	}
182
183
184
	/**
185
	 * Returns the schemas specified by the given resource name.
186
	 *
187
	 * @param string $name Name from resource configuration
188
	 * @return \Aimeos\MW\Setup\DBSchema\Iface
189
	 */
190
	protected function getSchema( $name )
191
	{
192
		if( !isset( $this->schemas[$name] ) ) {
193
			return $this->schema;
194
		}
195
196
		return $this->schemas[$name];
197
	}
198
199
200
	/**
201
	 * Returns the setup task paths ordered by their dependencies
202
	 *
203
	 * @return string[] List of file system paths
204
	 */
205
	protected function getSetupPaths()
206
	{
207
		return $this->paths;
208
	}
209
210
211
	/**
212
	 * Executes a given SQL statement and returns the value of the named column and first row.
213
	 *
214
	 * @param string $sql SQL statement to execute
215
	 * @param string $column Column name to retrieve
216
	 * @param string $name Name from the resource configuration
217
	 * @return string Column value
218
	 */
219
	protected function getValue( $sql, $column, $name = 'db' )
220
	{
221
		$conn = $this->acquire( $name );
222
223
		try
224
		{
225
			$result = $conn->create( $sql )->execute();
226
227
			if( ( $row = $result->fetch() ) === false ) {
228
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No rows found: %1$s', $sql ) );
229
			}
230
231
			$result->finish();
232
233
			if( array_key_exists( $column, $row ) === false ) {
234
				throw new \Aimeos\MW\Setup\Exception( sprintf( 'No column "%1$s" found: %2$s', $column, $sql ) );
235
			}
236
237
			$this->release( $conn, $name );
238
		}
239
		catch( \Exception $e )
240
		{
241
			$this->release( $conn, $name );
242
			throw $e;
243
		}
244
245
		return $row[$column];
246
	}
247
248
249
	/**
250
	 * Prints the message for the current test.
251
	 *
252
	 * @param string $msg Current message
253
	 * @param integer $level Indent level of the message (default: 0 )
254
	 * @param string|null $status Current status
255
	 */
256
	protected function msg( $msg, $level = 0, $status = null )
257
	{
258
		$pre = '';
259
		for( $i = 0; $i < 2*$level; $i++ ) {
260
			$pre .= ' ';
261
		}
262
263
		echo str_pad( $pre . $msg, 70 ) . ( $status !== null ? $status . PHP_EOL : '' );
264
	}
265
266
267
	/**
268
	 * Prints the status for the current test.
269
	 *
270
	 * @param string $status Current status
271
	 */
272
	protected function status( $status )
273
	{
274
		echo $status . PHP_EOL;
275
	}
276
277
278
	/**
279
	 * Extracts the table definitions from the given content.
280
	 *
281
	 * @param string $content Content of the file to parse
282
	 * @return string[] Associative list of table names with table create statements ordered like in the file
283
	 */
284
	protected function getTableDefinitions( $content )
285
	{
286
		$defs = [];
287
		$matches = [];
288
289
		$regex = '/CREATE TABLE \"?([a-zA-Z0-9_]+)\"? .*(\n\n|$)/sU';
290
		if ( preg_match_all($regex, $content, $matches, PREG_SET_ORDER) === false ) {
291
			throw new \Aimeos\MW\Setup\Exception('Unable to get table definitions');
292
		}
293
294
		foreach ( $matches as $match ) {
295
			$defs[$match[1]] = $match[0];
296
		}
297
298
		return $defs;
299
	}
300
301
302
	/**
303
	 * Extracts the index definitions from the given content.
304
	 *
305
	 * @param string $content Content of the file to parse
306
	 * @return string[] Associative list of index names with index create statements ordered like in the file
307
	 */
308
	protected function getIndexDefinitions( $content )
309
	{
310
		$defs = [];
311
		$matches = [];
312
313
		if ( preg_match_all('/CREATE [a-zA-Z]* ?INDEX \"?([a-zA-Z0-9_]+)\"? ON \"?([a-zA-Z0-9_]+)\"? .+(\n\n|$)/sU', $content, $matches, PREG_SET_ORDER) === false ) {
314
			throw new \Aimeos\MW\Setup\Exception('Unable to get index definitions');
315
		}
316
317
		foreach ( $matches as $match ) {
318
			$name = $match[2] . '.' . $match[1];
319
			$defs[$name] = $match[0];
320
		}
321
322
		return $defs;
323
	}
324
325
326
	/**
327
	 * Extracts the trigger definitions from the given content.
328
	 *
329
	 * @param string $content Content of the file to parse
330
	 * @return string[] Associative list of trigger names with trigger create statements ordered like in the file
331
	 */
332
	protected function getTriggerDefinitions( $content )
333
	{
334
		$defs = [];
335
		$matches = [];
336
337
		$regex = '/CREATE TRIGGER \"?([a-zA-Z0-9_]+)\"? .*(\n\n|$)/sU';
338
		if ( preg_match_all($regex, $content, $matches, PREG_SET_ORDER) === false ) {
339
			throw new \Aimeos\MW\Setup\Exception('Unable to get trigger definitions');
340
		}
341
342
		foreach ( $matches as $match ) {
343
			$defs[$match[1]] = $match[0];
344
		}
345
346
		return $defs;
347
	}
348
}
349