Skip to content

Commit 6ed9120

Browse files
committed
add install option db-port
1 parent 3dc4ca6 commit 6ed9120

File tree

10 files changed

+30
-8
lines changed

10 files changed

+30
-8
lines changed

psalm-baseline.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</PossiblyNullIterator>
4949
</file>
5050
<file src="src/Command/InstallCommand.php">
51-
<PossiblyInvalidArgument occurrences="12">
51+
<PossiblyInvalidArgument occurrences="13">
5252
<code>$input-&gt;getOption('plugin')</code>
5353
<code>$pluginsDir</code>
5454
<code>$input-&gt;getOption('moodle')</code>
@@ -59,6 +59,7 @@
5959
<code>$input-&gt;getOption('db-user')</code>
6060
<code>$input-&gt;getOption('db-pass')</code>
6161
<code>$input-&gt;getOption('db-host')</code>
62+
<code>$input-&gt;getOption('db-port')</code>
6263
<code>$input-&gt;getOption('not-paths')</code>
6364
<code>$input-&gt;getOption('not-names')</code>
6465
</PossiblyInvalidArgument>

res/template/config.php.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ $CFG->dbname = '{{DBNAME}}';
1111
$CFG->dbuser = '{{DBUSER}}';
1212
$CFG->dbpass = '{{DBPASS}}';
1313
$CFG->prefix = 'mdl_';
14-
$CFG->dboptions = [];
14+
$CFG->dboptions = [
15+
'dbport' => '{{DBPORT}}',
16+
];
1517

1618
$CFG->wwwroot = '{{WWWROOT}}';
1719
$CFG->dataroot = '{{DATAROOT}}';

src/Bridge/MoodleConfig.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function createContents(AbstractDatabase $database, $dataDir)
3737
'{{DBTYPE}}' => $database->type,
3838
'{{DBLIBRARY}}' => $database->library,
3939
'{{DBHOST}}' => $database->host,
40+
'{{DBPORT}}' => $database->port,
4041
'{{DBNAME}}' => $database->name,
4142
'{{DBUSER}}' => $database->user,
4243
'{{DBPASS}}' => $database->pass,

src/Command/InstallCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ protected function configure()
9494
->addOption('db-pass', null, InputOption::VALUE_REQUIRED, 'Database pass', '')
9595
->addOption('db-name', null, InputOption::VALUE_REQUIRED, 'Database name', 'moodle')
9696
->addOption('db-host', null, InputOption::VALUE_REQUIRED, 'Database host', 'localhost')
97+
->addOption('db-port', null, InputOption::VALUE_REQUIRED, 'Database port', '')
9798
->addOption('not-paths', null, InputOption::VALUE_REQUIRED, 'CSV of file paths to exclude', $paths)
9899
->addOption('not-names', null, InputOption::VALUE_REQUIRED, 'CSV of file names to exclude', $names)
99100
->addOption('extra-plugins', null, InputOption::VALUE_REQUIRED, 'Directory of extra plugins to install', $extra)
@@ -174,7 +175,8 @@ public function initializeInstallerFactory(InputInterface $input)
174175
$input->getOption('db-name'),
175176
$input->getOption('db-user'),
176177
$input->getOption('db-pass'),
177-
$input->getOption('db-host')
178+
$input->getOption('db-host'),
179+
$input->getOption('db-port')
178180
);
179181

180182
return $factory;

src/Installer/Database/AbstractDatabase.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ abstract class AbstractDatabase
4545
*/
4646
public $host = 'localhost';
4747

48+
/**
49+
* Database port.
50+
*
51+
* @var string
52+
*/
53+
public $port = '';
54+
4855
/**
4956
* Moodle database type.
5057
*

src/Installer/Database/DatabaseResolver.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class DatabaseResolver
2323
* @param string|null $user
2424
* @param string|null $pass
2525
* @param string|null $host
26+
* @param string|null $port
2627
*
2728
* @return AbstractDatabase
2829
*/
29-
public function resolveDatabase($type, $name = null, $user = null, $pass = null, $host = null)
30+
public function resolveDatabase($type, $name = null, $user = null, $pass = null, $host = null, $port = null)
3031
{
3132
$database = $this->resolveDatabaseType($type);
3233

@@ -42,6 +43,9 @@ public function resolveDatabase($type, $name = null, $user = null, $pass = null,
4243
if ($host !== null) {
4344
$database->host = $host;
4445
}
46+
if ($port !== null) {
47+
$database->port = $port;
48+
}
4549

4650
return $database;
4751
}

src/Installer/Database/MySQLDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ public function getCreateDatabaseCommand()
2424
$passOpt = !empty($this->pass) ? ' --password='.escapeshellarg($this->pass) : '';
2525
$user = escapeshellarg($this->user);
2626
$host = escapeshellarg($this->host);
27+
$port = !empty($this->port) ? ' --port='.escapeshellarg($this->port) : '';
2728
$createDB = escapeshellarg(sprintf('CREATE DATABASE `%s` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;', $this->name));
2829

29-
return sprintf('mysql -u %s%s -h %s -e %s', $user, $passOpt, $host, $createDB);
30+
return sprintf('mysql -u %s%s -h %s%s -e %s', $user, $passOpt, $host, $port, $createDB);
3031
}
3132
}

src/Installer/Database/PostgresDatabase.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public function getCreateDatabaseCommand()
2525
$pass = !empty($this->pass) ? 'env PGPASSWORD='.escapeshellarg($this->pass).' ' : '';
2626
$user = escapeshellarg($this->user);
2727
$host = escapeshellarg($this->host);
28+
$port = !empty($this->port) ? ' --port '.escapeshellarg($this->port) : '';
2829
$createDB = escapeshellarg(sprintf('CREATE DATABASE "%s";', $this->name));
2930

30-
return sprintf('%spsql -c %s -U %s -h %s', $pass, $createDB, $user, $host);
31+
return sprintf('%spsql -c %s -U %s -h %s%s', $pass, $createDB, $user, $host, $port);
3132
}
3233
}

tests/Fixture/example-config.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
$CFG->dbuser = 'root';
1212
$CFG->dbpass = '';
1313
$CFG->prefix = 'mdl_';
14-
$CFG->dboptions = [];
14+
$CFG->dboptions = [
15+
'dbport' => '',
16+
];
1517

1618
$CFG->wwwroot = 'http://localhost/moodle';
1719
$CFG->dataroot = '/path/to/moodledata';

tests/Installer/Database/DatabaseResolverTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ public function testOptions()
4949
$user = 'TestUser';
5050
$pass = 'TestPass';
5151
$host = 'TestHost';
52+
$port = 'TestPort';
5253

53-
$database = $resolver->resolveDatabase('mysqli', $name, $user, $pass, $host);
54+
$database = $resolver->resolveDatabase('mysqli', $name, $user, $pass, $host, $port);
5455

5556
$this->assertInstanceOf(
5657
'MoodlePluginCI\Installer\Database\MySQLDatabase',

0 commit comments

Comments
 (0)