Skip to content

Commit ff912a0

Browse files
Merge pull request #309 from sebastianfeldmann/bugfix/cli-var-escaping
Use new cli var feature to fix escaping
2 parents 8fd1980 + 2d2d4c1 commit ff912a0

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"ext-json": "*",
5050
"ext-spl": "*",
5151
"sebastian/environment": "^3.0|^4.0|^5.0",
52-
"sebastianfeldmann/cli": "^3.1"
52+
"sebastianfeldmann/cli": "^3.4"
5353
},
5454
"require-dev": {
5555
"sebastianfeldmann/git": "^3.2",

src/Cli/Executable/Pgdump.php

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class Pgdump extends Abstraction implements Executable
3636
* @var int
3737
*/
3838
private $port;
39-
39+
4040
/**
4141
* Run the dump in parallel by dumping njobs tables simultaneously.
4242
* --jobs=njobs
@@ -187,7 +187,7 @@ class Pgdump extends Abstraction implements Executable
187187
* @var string
188188
*/
189189
private $file;
190-
190+
191191
/**
192192
* List of available output formats
193193
*
@@ -234,7 +234,7 @@ public function __construct(string $path = '')
234234
*
235235
* @param string $user
236236
* @param string $password
237-
* @return \phpbu\App\Cli\Executable\Pgdump
237+
* @return Pgdump
238238
*/
239239
public function credentials(string $user = '', string $password = '') : Pgdump
240240
{
@@ -247,7 +247,7 @@ public function credentials(string $user = '', string $password = '') : Pgdump
247247
* Set the postgreSQL hostname.
248248
*
249249
* @param string $host
250-
* @return \phpbu\App\Cli\Executable\Pgdump
250+
* @return Pgdump
251251
*/
252252
public function useHost(string $host) : Pgdump
253253
{
@@ -259,7 +259,7 @@ public function useHost(string $host) : Pgdump
259259
* Set the postgreSQL port.
260260
*
261261
* @param int $port
262-
* @return \phpbu\App\Cli\Executable\Pgdump
262+
* @return Pgdump
263263
*/
264264
public function usePort(int $port) : Pgdump
265265
{
@@ -271,7 +271,7 @@ public function usePort(int $port) : Pgdump
271271
* Define njobs tables simultaneously..
272272
*
273273
* @param int $jobs
274-
* @return \phpbu\App\Cli\Executable\Pgdump
274+
* @return Pgdump
275275
*/
276276
public function dumpJobs(int $jobs): Pgdump
277277
{
@@ -301,7 +301,7 @@ public function sslMode(string $sslMode): Pgdump
301301
* Set database to dump.
302302
*
303303
* @param string $database
304-
* @return \phpbu\App\Cli\Executable\Pgdump
304+
* @return Pgdump
305305
*/
306306
public function dumpDatabase(string $database) : Pgdump
307307
{
@@ -314,7 +314,7 @@ public function dumpDatabase(string $database) : Pgdump
314314
* Works only on format=plain-text.
315315
*
316316
* @param boolean $bool
317-
* @return \phpbu\App\Cli\Executable\Pgdump
317+
* @return Pgdump
318318
*/
319319
public function addDropStatements(bool $bool) : Pgdump
320320
{
@@ -326,7 +326,7 @@ public function addDropStatements(bool $bool) : Pgdump
326326
* Add the --no-owner option so no ownership setting commands will be added.
327327
*
328328
* @param boolean $bool
329-
* @return \phpbu\App\Cli\Executable\Pgdump
329+
* @return Pgdump
330330
*/
331331
public function skipOwnerCommands(bool $bool) : Pgdump
332332
{
@@ -338,7 +338,7 @@ public function skipOwnerCommands(bool $bool) : Pgdump
338338
* Set schemas to dump.
339339
*
340340
* @param array $schemas
341-
* @return \phpbu\App\Cli\Executable\Pgdump
341+
* @return Pgdump
342342
*/
343343
public function dumpSchemas(array $schemas) : Pgdump
344344
{
@@ -350,7 +350,7 @@ public function dumpSchemas(array $schemas) : Pgdump
350350
* Set schemas to exclude.
351351
*
352352
* @param array $schemas
353-
* @return \phpbu\App\Cli\Executable\Pgdump
353+
* @return Pgdump
354354
*/
355355
public function excludeSchemas(array $schemas) : Pgdump
356356
{
@@ -362,7 +362,7 @@ public function excludeSchemas(array $schemas) : Pgdump
362362
* Set tables to dump.
363363
*
364364
* @param array $tables
365-
* @return \phpbu\App\Cli\Executable\Pgdump
365+
* @return Pgdump
366366
*/
367367
public function dumpTables(array $tables) : Pgdump
368368
{
@@ -374,7 +374,7 @@ public function dumpTables(array $tables) : Pgdump
374374
* Set tables to ignore.
375375
*
376376
* @param array $tables
377-
* @return \phpbu\App\Cli\Executable\Pgdump
377+
* @return Pgdump
378378
*/
379379
public function excludeTables(array $tables) : Pgdump
380380
{
@@ -386,7 +386,7 @@ public function excludeTables(array $tables) : Pgdump
386386
* Set tables where no data is dumped.
387387
*
388388
* @param array $tables
389-
* @return \phpbu\App\Cli\Executable\Pgdump
389+
* @return Pgdump
390390
*/
391391
public function excludeTableData(array $tables) : Pgdump
392392
{
@@ -398,8 +398,8 @@ public function excludeTableData(array $tables) : Pgdump
398398
* Dump only the schema information.
399399
*
400400
* @param boolean $bool
401-
* @return \phpbu\App\Cli\Executable\Pgdump
402-
* @throws \phpbu\App\Exception
401+
* @return Pgdump
402+
* @throws Exception
403403
*/
404404
public function dumpSchemaOnly(bool $bool) : Pgdump
405405
{
@@ -414,8 +414,8 @@ public function dumpSchemaOnly(bool $bool) : Pgdump
414414
* Dump no schema information.
415415
*
416416
* @param boolean $bool
417-
* @return \phpbu\App\Cli\Executable\Pgdump
418-
* @throws \phpbu\App\Exception
417+
* @return Pgdump
418+
* @throws Exception
419419
*/
420420
public function dumpDataOnly(bool $bool) : Pgdump
421421
{
@@ -430,7 +430,7 @@ public function dumpDataOnly(bool $bool) : Pgdump
430430
* Set the dump target path.
431431
*
432432
* @param string $path
433-
* @return \phpbu\App\Cli\Executable\Pgdump
433+
* @return Pgdump
434434
*/
435435
public function dumpTo(string $path) : Pgdump
436436
{
@@ -442,8 +442,8 @@ public function dumpTo(string $path) : Pgdump
442442
* Set the dump format.
443443
*
444444
* @param string $format
445-
* @return \phpbu\App\Cli\Executable\Pgdump
446-
* @throws \phpbu\App\Exception
445+
* @return Pgdump
446+
* @throws Exception
447447
*/
448448
public function dumpFormat(string $format) : Pgdump
449449
{
@@ -459,7 +459,7 @@ public function dumpFormat(string $format) : Pgdump
459459
* --no-owner
460460
*
461461
* @param bool $bool
462-
* @return \phpbu\App\Cli\Executable\Pgdump
462+
* @return Pgdump
463463
*/
464464
public function dumpNoOwner(bool $bool) : Pgdump
465465
{
@@ -472,7 +472,7 @@ public function dumpNoOwner(bool $bool) : Pgdump
472472
* --no-tablespaces
473473
*
474474
* @param bool $bool
475-
* @return \phpbu\App\Cli\Executable\Pgdump
475+
* @return Pgdump
476476
*/
477477
public function dumpNoTablespaces(bool $bool) : Pgdump
478478
{
@@ -485,7 +485,7 @@ public function dumpNoTablespaces(bool $bool) : Pgdump
485485
* --no-acl
486486
*
487487
* @param boolean $bool
488-
* @return \phpbu\App\Cli\Executable\Pgdump
488+
* @return Pgdump
489489
*/
490490
public function dumpNoPrivileges(bool $bool) : Pgdump
491491
{
@@ -496,7 +496,7 @@ public function dumpNoPrivileges(bool $bool) : Pgdump
496496
* Set the output file encoding.
497497
*
498498
* @param string $encoding
499-
* @return \phpbu\App\Cli\Executable\Pgdump
499+
* @return Pgdump
500500
*/
501501
public function encode(string $encoding) : Pgdump
502502
{
@@ -507,14 +507,15 @@ public function encode(string $encoding) : Pgdump
507507
/**
508508
* Pgdump CommandLine generator.
509509
*
510-
* @return \SebastianFeldmann\Cli\CommandLine
510+
* @return CommandLine
511511
*/
512512
protected function createCommandLine() : CommandLine
513513
{
514514
$process = new CommandLine();
515-
$password = $this->password ? 'PGPASSWORD=' . escapeshellarg($this->password) . ' ' : '';
516-
$sslMode = $this->sslMode ? 'PGSSLMODE=' . escapeshellarg($this->sslMode) . ' ' : '';
517-
$cmd = new Cmd($sslMode . $password . $this->binary);
515+
$cmd = new Cmd($this->binary);
516+
517+
$this->handleVariables($cmd);
518+
518519
$process->addCommand($cmd);
519520

520521
// always disable password prompt
@@ -532,7 +533,7 @@ protected function createCommandLine() : CommandLine
532533
$cmd->addOptionIfNotEmpty('--encoding', $this->encoding);
533534
$cmd->addOptionIfNotEmpty('--no-tablespaces', $this->noTablespaces, false);
534535
$cmd->addOptionIfNotEmpty('--no-acl', $this->noPrivileges, false);
535-
536+
536537

537538
$this->handleSchemas($cmd);
538539
$this->handleTables($cmd);
@@ -542,10 +543,25 @@ protected function createCommandLine() : CommandLine
542543
return $process;
543544
}
544545

546+
/**
547+
* This handles all command variables e.g SSLMODE or PASSWORD
548+
*
549+
* @param Cmd $cmd
550+
*/
551+
private function handleVariables(Cmd $cmd): void
552+
{
553+
if ($this->password) {
554+
$cmd->addVar('PGPASSWORD', $this->password);
555+
}
556+
if ($this->sslMode) {
557+
$cmd->addVar('PGSSLMODE', $this->sslMode);
558+
}
559+
}
560+
545561
/**
546562
* Handle command schema settings.
547563
*
548-
* @param \SebastianFeldmann\Cli\Command\Executable $cmd
564+
* @param Cmd $cmd
549565
*/
550566
protected function handleSchemas(Cmd $cmd)
551567
{
@@ -561,7 +577,7 @@ protected function handleSchemas(Cmd $cmd)
561577
/**
562578
* Handle command table settings.
563579
*
564-
* @param \SebastianFeldmann\Cli\Command\Executable $cmd
580+
* @param Cmd $cmd
565581
*/
566582
protected function handleTables(Cmd $cmd)
567583
{

tests/phpbu/Backup/Source/PgdumpTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function testSslMode()
5050
$executable = $pgDump->getExecutable($target);
5151

5252
$this->assertEquals(
53-
'"' . 'PGSSLMODE=\'require\' ' . PHPBU_TEST_BIN . '/pg_dump" -w --file=\'foo.sql\' --format=\'p\'',
53+
'PGSSLMODE=\'require\' "' . PHPBU_TEST_BIN . '/pg_dump" -w --file=\'foo.sql\' --format=\'p\'',
5454
$executable->getCommand()
5555
);
5656
}

tests/phpbu/Cli/Executable/PgdumpTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testWithUserAndPassword()
4444
->dumpTo($file);
4545

4646
$this->assertEquals(
47-
'"PGPASSWORD=\'secret\' ' . PHPBU_TEST_BIN . '/pg_dump" -w --username=\'mrx\' --file=\'/tmp/foo\'',
47+
'PGPASSWORD=\'secret\' "' . PHPBU_TEST_BIN . '/pg_dump" -w --username=\'mrx\' --file=\'/tmp/foo\'',
4848
$pgdump->getCommand()
4949
);
5050
}

0 commit comments

Comments
 (0)