Completed
Pull Request — master (#52)
by Mateusz
03:09 queued 01:22
created
src/SSPak.php 1 patch
Spacing   +47 added lines, -47 removed lines patch added patch discarded remove patch
@@ -88,16 +88,16 @@  discard block
 block discarded – undo
88 88
 
89 89
 	function help($args) {
90 90
 		echo "SSPak: manage SilverStripe .sspak archives.\n\nUsage:\n";
91
-		foreach($this->getActions() as $action => $info) {
91
+		foreach ($this->getActions() as $action => $info) {
92 92
 			echo "sspak $action";
93
-			if(!empty($info['unnamedArgs'])) {
94
-				foreach($info['unnamedArgs'] as $arg) echo " ($arg)";
93
+			if (!empty($info['unnamedArgs'])) {
94
+				foreach ($info['unnamedArgs'] as $arg) echo " ($arg)";
95 95
 			}
96
-			if(!empty($info['namedFlags'])) {
97
-				foreach($info['namedFlags'] as $arg) echo " (--$arg)";
96
+			if (!empty($info['namedFlags'])) {
97
+				foreach ($info['namedFlags'] as $arg) echo " (--$arg)";
98 98
 			}
99
-			if(!empty($info['namedArgs'])) {
100
-				foreach($info['namedArgs'] as $arg) echo " --$arg=\"$arg value\"";
99
+			if (!empty($info['namedArgs'])) {
100
+				foreach ($info['namedArgs'] as $arg) echo " --$arg=\"$arg value\"";
101 101
 			}
102 102
 			echo "\n  {$info['description']}\n\n";
103 103
 		}
@@ -121,13 +121,13 @@  discard block
 block discarded – undo
121 121
 
122 122
 		$filesystem = new FilesystemEntity(null, $executor);
123 123
 
124
-		if($pakParts['db']) {
124
+		if ($pakParts['db']) {
125 125
 			$dbPath = escapeshellarg($namedArgs['db']);
126 126
 			$process = $filesystem->createProcess("cat $dbPath | gzip -c");
127 127
 			$sspak->writeFileFromProcess('database.sql.gz', $process);
128 128
 		}
129 129
 
130
-		if($pakParts['assets']) {
130
+		if ($pakParts['assets']) {
131 131
 			$assetsParentArg = escapeshellarg(dirname($namedArgs['assets']));
132 132
 			$assetsBaseArg = escapeshellarg(basename($namedArgs['assets']));
133 133
 			$process = $filesystem->createProcess("cd $assetsParentArg && tar cfh - $assetsBaseArg | gzip -c");
@@ -154,7 +154,7 @@  discard block
 block discarded – undo
154 154
 		// manually.
155 155
 		// There is no cross-platform way of checking the assets.tar.gz size without unpacking, so we assume the size
156 156
 		// of database is negligible which let's us approximate the size of assets.
157
-		if (filesize($file) > 8*1024*1024*1024) {
157
+		if (filesize($file) > 8 * 1024 * 1024 * 1024) {
158 158
 			$msg = <<<EOM
159 159
 
160 160
 ERROR: SSPak is unable to extract archives over 8 GB.
@@ -175,7 +175,7 @@  discard block
 block discarded – undo
175 175
 		$sspak = new SSPakFile($file, $executor);
176 176
 
177 177
 		// Validation
178
-		if(!$sspak->exists()) throw new Exception("File '$file' doesn't exist.");
178
+		if (!$sspak->exists()) throw new Exception("File '$file' doesn't exist.");
179 179
 
180 180
 		$phar = $sspak->getPhar();
181 181
 		$phar->extractTo($dest);
@@ -206,9 +206,9 @@  discard block
 block discarded – undo
206 206
 
207 207
 		$db = new DatabaseConnector($webroot);
208 208
 
209
-		foreach($db->getTables() as $table) {
210
-			$filename = $destPath . '/' . $table . '.csv';
211
-			echo $filename . "...\n";
209
+		foreach ($db->getTables() as $table) {
210
+			$filename = $destPath.'/'.$table.'.csv';
211
+			echo $filename."...\n";
212 212
 			touch($filename);
213 213
 			$writer = new CsvTableWriter($filename);
214 214
 			$db->saveTable($table, $writer);
@@ -229,10 +229,10 @@  discard block
 block discarded – undo
229 229
 
230 230
 		$db = new DatabaseConnector($webroot);
231 231
 
232
-		foreach($db->getTables() as $table) {
233
-			$filename = $srcPath . '/' . $table . '.csv';
234
-			if(file_exists($filename)) {
235
-				echo $filename . "...\n";
232
+		foreach ($db->getTables() as $table) {
233
+			$filename = $srcPath.'/'.$table.'.csv';
234
+			if (file_exists($filename)) {
235
+				echo $filename."...\n";
236 236
 				$reader = new CsvTableReader($filename);
237 237
 				$db->loadTable($table, $reader);
238 238
 			} else {
@@ -254,16 +254,16 @@  discard block
 block discarded – undo
254 254
 
255 255
 		$webroot = new Webroot($unnamedArgs[0], $executor);
256 256
 		$file = $unnamedArgs[1];
257
-		if(file_exists($file)) throw new Exception( "File '$file' already exists.");
257
+		if (file_exists($file)) throw new Exception("File '$file' already exists.");
258 258
 
259 259
 		$sspak = new SSPakFile($file, $executor);
260 260
 
261
-		if(!empty($namedArgs['identity'])) {
261
+		if (!empty($namedArgs['identity'])) {
262 262
 			// SSH private key
263 263
 			$webroot->setSSHItentityFile($namedArgs['identity']);
264 264
 		}
265
-		if(!empty($namedArgs['from-sudo'])) $webroot->setSudo($namedArgs['from-sudo']);
266
-		else if(!empty($namedArgs['sudo'])) $webroot->setSudo($namedArgs['sudo']);
265
+		if (!empty($namedArgs['from-sudo'])) $webroot->setSudo($namedArgs['from-sudo']);
266
+		else if (!empty($namedArgs['sudo'])) $webroot->setSudo($namedArgs['sudo']);
267 267
 
268 268
 		// Look up which parts of the sspak are going to be saved
269 269
 		$pakParts = $args->pakParts();
@@ -272,7 +272,7 @@  discard block
 block discarded – undo
272 272
 		$details = $webroot->sniff();
273 273
 
274 274
 		// Create a build folder for the sspak file
275
-		$buildFolder = "/tmp/sspak-" . rand(100000,999999);
275
+		$buildFolder = "/tmp/sspak-".rand(100000, 999999);
276 276
 		$webroot->exec(array('mkdir', $buildFolder));
277 277
 
278 278
 		$dbFile = "$buildFolder/database.sql.gz";
@@ -283,22 +283,22 @@  discard block
 block discarded – undo
283 283
 		$fileList = array();
284 284
 
285 285
 		// Save DB
286
-		if($pakParts['db']) {
286
+		if ($pakParts['db']) {
287 287
 			// Check the database type
288 288
 			$dbFunction = 'getdb_'.$details['db_type'];
289
-			if(!method_exists($this,$dbFunction)) {
290
-				throw new Exception("Can't process database type '" . $details['db_type'] . "'");
289
+			if (!method_exists($this, $dbFunction)) {
290
+				throw new Exception("Can't process database type '".$details['db_type']."'");
291 291
 			}
292 292
 			$this->$dbFunction($webroot, $details, $sspak, basename($dbFile));
293 293
 		}
294 294
 
295 295
 		// Save Assets
296
-		if($pakParts['assets']) {
296
+		if ($pakParts['assets']) {
297 297
 			$this->getassets($webroot, $details['assets_path'], $sspak, basename($assetsFile));
298 298
 		}
299 299
 
300 300
 		// Save git-remote
301
-		if($pakParts['git-remote']) {
301
+		if ($pakParts['git-remote']) {
302 302
 			$this->getgitremote($webroot, $sspak, basename($gitRemoteFile));
303 303
 		}
304 304
 
@@ -318,7 +318,7 @@  discard block
 block discarded – undo
318 318
 		$hostArg = '';
319 319
 		$portArg = '';
320 320
 		if (!empty($conf['db_server']) && $conf['db_server'] != 'localhost') {
321
-			if (strpos($conf['db_server'], ':')!==false) {
321
+			if (strpos($conf['db_server'], ':') !== false) {
322 322
 				// Handle "server:port" format.
323 323
 				$server = explode(':', $conf['db_server'], 2);
324 324
 				$hostArg = escapeshellarg("--host=".$server[0]);
@@ -357,16 +357,16 @@  discard block
 block discarded – undo
357 357
 
358 358
 	function getgitremote($webroot, $sspak, $gitRemoteFile) {
359 359
 		// Only do anything if we're copying from a git checkout
360
-		$gitRepo = $webroot->getPath() .'/.git';
361
-		if($webroot->exists($gitRepo)) {
360
+		$gitRepo = $webroot->getPath().'/.git';
361
+		if ($webroot->exists($gitRepo)) {
362 362
 			// Identify current branch
363 363
 			$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'branch'));
364
-			if(preg_match("/\* ([^ \n]*)/", $output['output'], $matches) && strpos("(no branch)", $matches[1])===false) {
364
+			if (preg_match("/\* ([^ \n]*)/", $output['output'], $matches) && strpos("(no branch)", $matches[1]) === false) {
365 365
 				// If there is a current branch, use that branch's remove
366 366
 				$currentBranch = trim($matches[1]);
367
-				$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'config','--get',"branch.$currentBranch.remote"));
367
+				$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'config', '--get', "branch.$currentBranch.remote"));
368 368
 				$remoteName = trim($output['output']);
369
-				if(!$remoteName) $remoteName = 'origin';
369
+				if (!$remoteName) $remoteName = 'origin';
370 370
 
371 371
 			// Default to origin
372 372
 			} else {
@@ -375,11 +375,11 @@  discard block
 block discarded – undo
375 375
 			}
376 376
 
377 377
 			// Determine the URL of that remote
378
-			$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'config','--get',"remote.$remoteName.url"));
378
+			$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'config', '--get', "remote.$remoteName.url"));
379 379
 			$remoteURL = trim($output['output']);
380 380
 
381 381
 			// Determine the current SHA
382
-			$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'log','-1','--format=%H'));
382
+			$output = $webroot->exec(array('git', '--git-dir='.$gitRepo, 'log', '-1', '--format=%H'));
383 383
 			$sha = trim($output['output']);
384 384
 
385 385
 			$content = "remote = $remoteURL\nbranch = $currentBranch\nsha = $sha\n";
@@ -407,22 +407,22 @@  discard block
 block discarded – undo
407 407
 		$pakParts = $args->pakParts();
408 408
 
409 409
 		$namedArgs = $args->getNamedArgs();
410
-		if(!empty($namedArgs['identity'])) {
410
+		if (!empty($namedArgs['identity'])) {
411 411
 			// SSH private key
412 412
 			$webroot->setSSHItentityFile($namedArgs['identity']);
413 413
 		}
414 414
 
415 415
 		// Validation
416
-		if(!$sspak->exists()) throw new Exception( "File '$file' doesn't exist.");
416
+		if (!$sspak->exists()) throw new Exception("File '$file' doesn't exist.");
417 417
 
418 418
 		// Push database, if necessary
419 419
 		$namedArgs = $args->getNamedArgs();
420
-		if($pakParts['db'] && $sspak->contains('database.sql.gz')) {
420
+		if ($pakParts['db'] && $sspak->contains('database.sql.gz')) {
421 421
 			$webroot->putdb($sspak, isset($namedArgs['drop-db']));
422 422
 		}
423 423
 
424 424
 		// Push assets, if neccessary
425
-		if($pakParts['assets'] && $sspak->contains('assets.tar.gz')) {
425
+		if ($pakParts['assets'] && $sspak->contains('assets.tar.gz')) {
426 426
 			$webroot->putassets($sspak);
427 427
 		}
428 428
 	}
@@ -444,13 +444,13 @@  discard block
 block discarded – undo
444 444
 		$pakParts = $args->pakParts();
445 445
 
446 446
 		// Validation
447
-		if($webroot->exists($webroot->getPath())) throw new Exception( "Webroot '$webrootDir' already exists.");
448
-		if(!$sspak->exists()) throw new Exception( "File '$file' doesn't exist.");
447
+		if ($webroot->exists($webroot->getPath())) throw new Exception("Webroot '$webrootDir' already exists.");
448
+		if (!$sspak->exists()) throw new Exception("File '$file' doesn't exist.");
449 449
 
450 450
 		// Create new dir
451 451
 		$webroot->exec(array('mkdir', $webroot->getPath()));
452 452
 
453
-		if($sspak->contains('git-remote')) {
453
+		if ($sspak->contains('git-remote')) {
454 454
 			$details = $sspak->gitRemoteDetails();
455 455
 			$webroot->putgit($details);
456 456
 		}
@@ -459,12 +459,12 @@  discard block
 block discarded – undo
459 459
 
460 460
 		// Push database, if necessary
461 461
 		$namedArgs = $args->getNamedArgs();
462
-		if($pakParts['db'] && $sspak->contains('database.sql.gz')) {
462
+		if ($pakParts['db'] && $sspak->contains('database.sql.gz')) {
463 463
 			$webroot->putdb($sspak, isset($namedArgs['drop-db']));
464 464
 		}
465 465
 
466 466
 		// Push assets, if neccessary
467
-		if($pakParts['assets'] && $sspak->contains('assets.tar.gz')) {
467
+		if ($pakParts['assets'] && $sspak->contains('assets.tar.gz')) {
468 468
 			$webroot->putassets($sspak);
469 469
 		}
470 470
 	}
@@ -491,10 +491,10 @@  discard block
 block discarded – undo
491 491
 		$sspakScript = str_replace('$isSelfExtracting = false;', '$isSelfExtracting = true;', $sspakScript);
492 492
 
493 493
 		// Load the sniffer file
494
-		$snifferFile = dirname(__FILE__) . '/sspak-sniffer.php';
494
+		$snifferFile = dirname(__FILE__).'/sspak-sniffer.php';
495 495
 		$sspakScript = str_replace("\$snifferFileContent = '';\n",
496 496
 			"\$snifferFileContent = '"
497
-			. str_replace(array("\\","'"),array("\\\\", "\\'"), file_get_contents($snifferFile)) . "';\n", $sspakScript);
497
+			. str_replace(array("\\", "'"), array("\\\\", "\\'"), file_get_contents($snifferFile))."';\n", $sspakScript);
498 498
 
499 499
 		file_put_contents($destFile, $sspakScript);
500 500
 		chmod($destFile, 0775);
Please login to merge, or discard this patch.