Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/php 8.1 #164

Merged
merged 5 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ on:
jobs:
ci:
uses: shlinkio/github-actions/.github/workflows/php-lib-ci.yml@main
with:
with-php-eight: false
secrets:
INFECTION_BADGE_API_KEY: ${{ secrets.INFECTION_BADGE_API_KEY }}
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
* *Nothing*

### Removed
* *Nothing*
* [#161](https://github.com/shlinkio/shlink-installer/issues/161) Dropped support for PHP 8.0

### Fixed
* *Nothing*
Expand Down
18 changes: 9 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,26 @@
}
],
"require": {
"php": "^8.0",
"php": "^8.1",
"laminas/laminas-config": "^3.7",
"laminas/laminas-config-aggregator": "^1.7",
"laminas/laminas-servicemanager": "^3.11.2",
"laminas/laminas-stdlib": "^3.7",
"laminas/laminas-config-aggregator": "^1.8",
"laminas/laminas-servicemanager": "^3.16",
"laminas/laminas-stdlib": "^3.11",
"lstrojny/functional-php": "^1.17",
"shlinkio/shlink-config": "^1.6",
"symfony/console": "^6.0",
"symfony/filesystem": "^6.0",
"symfony/process": "^6.0"
"symfony/console": "^6.1",
"symfony/filesystem": "^6.1",
"symfony/process": "^6.1"
},
"require-dev": {
"devster/ubench": "^2.0",
"infection/infection": "^0.26",
"phpspec/prophecy-phpunit": "^2.0",
"phpstan/phpstan": "^1.5",
"phpstan/phpstan": "^1.8",
"phpunit/phpunit": "^9.5",
"roave/security-advisories": "dev-master",
"shlinkio/php-coding-standard": "~2.3.0",
"symfony/var-dumper": "^6.0"
"symfony/var-dumper": "^6.1"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 4 additions & 4 deletions config/config.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ return [

'installer' => [
'installation_commands' => [
InstallationCommand::DB_CREATE_SCHEMA => [
InstallationCommand::DB_CREATE_SCHEMA->value => [
'command' => '-v', // Just print PHP version
],
InstallationCommand::DB_MIGRATE => [
InstallationCommand::DB_MIGRATE->value => [
'command' => null, // Skip
],
InstallationCommand::ORM_PROXIES => [
InstallationCommand::ORM_PROXIES->value => [
'command' => '-v', // Just print PHP version
],
InstallationCommand::ORM_CLEAR_CACHE => [
InstallationCommand::ORM_CLEAR_CACHE->value => [
'command' => '-v', // Just print PHP version
],
],
Expand Down
10 changes: 5 additions & 5 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,31 +222,31 @@
'enabled_options' => null,

'installation_commands' => [
InstallationCommand::DB_CREATE_SCHEMA => [
InstallationCommand::DB_CREATE_SCHEMA->value => [
'command' => 'vendor/doctrine/orm/bin/doctrine.php orm:schema-tool:create',
'initMessage' => 'Initializing database...',
'errorMessage' => 'Error generating database.',
'failOnError' => true,
],
InstallationCommand::DB_MIGRATE => [
InstallationCommand::DB_MIGRATE->value => [
'command' => 'vendor/doctrine/migrations/bin/doctrine-migrations.php migrations:migrate',
'initMessage' => 'Updating database...',
'errorMessage' => 'Error updating database.',
'failOnError' => true,
],
InstallationCommand::ORM_PROXIES => [
InstallationCommand::ORM_PROXIES->value => [
'command' => 'vendor/doctrine/orm/bin/doctrine.php orm:generate-proxies',
'initMessage' => 'Generating proxies...',
'errorMessage' => 'Error generating proxies.',
'failOnError' => true,
],
InstallationCommand::ORM_CLEAR_CACHE => [
InstallationCommand::ORM_CLEAR_CACHE->value => [
'command' => 'vendor/doctrine/orm/bin/doctrine.php orm:clear-cache:metadata',
'initMessage' => 'Clearing entities cache...',
'errorMessage' => 'Error clearing entities cache.',
'failOnError' => false,
],
InstallationCommand::GEOLITE_DOWNLOAD_DB => [
InstallationCommand::GEOLITE_DOWNLOAD_DB->value => [
'command' => null, // Disabled by default, to avoid dependency on consumer (Shlink)
'initMessage' => 'Downloading GeoLite2 db file...',
'errorMessage' => 'Error downloading GeoLite2 db.',
Expand Down
9 changes: 6 additions & 3 deletions src/Command/AbstractInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$importedConfig = $this->resolvePreviousConfig($io);
if ($this->isUpdate()) {
$this->assetsHandler->importShlinkAssetsFromPath($io, $importedConfig->importPath());
$this->assetsHandler->importShlinkAssetsFromPath($io, $importedConfig->importPath);
}
$config = $this->configGenerator->generateConfigInteractively($io, $importedConfig->importedConfig());
$config = $this->configGenerator->generateConfigInteractively($io, $importedConfig->importedConfig);
$configArray = Utils::normalizeAndKeepEnvVarKeys($config->toArray());

// Generate config params files
Expand Down Expand Up @@ -77,7 +77,10 @@ private function execPostInstallCommands(SymfonyStyle $io): bool
? InstallationCommand::POST_UPDATE_COMMANDS
: InstallationCommand::POST_INSTALL_COMMANDS;

return every($commands, fn (string $commandName) => $this->commandsRunner->execPhpCommand($commandName, $io));
return every(
$commands,
fn (InstallationCommand $command) => $this->commandsRunner->execPhpCommand($command->value, $io),
);
}

abstract protected function isUpdate(): bool;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@

namespace Shlinkio\Shlink\Installer\Config\Option\Database;

use Shlinkio\Shlink\Installer\Config\Util\DatabaseDriver;

use function str_contains;

abstract class AbstractNonSqliteDependentConfigOption extends AbstractDriverDependentConfigOption
{
protected function shouldBeAskedForDbDriver(string $dbDriver): bool
{
// DEPRECATED.
// Should just compare with strict equality ($dbDriver === DatabaseDriverConfigOption::SQLITE_DRIVER)
// Should just compare with strict equality (DatabaseDriver::tryFrom($dbDriver) === DatabaseDriver::SQLITE)
// Using str_contains instead for backwards compatibility when importing the pdo_sqlite value
return ! str_contains($dbDriver, DatabaseDriverConfigOption::SQLITE_DRIVER);
return ! str_contains($dbDriver, DatabaseDriver::SQLITE->value);
}
}
17 changes: 7 additions & 10 deletions src/Config/Option/Database/DatabaseDriverConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Shlinkio\Shlink\Config\Collection\PathCollection;
use Shlinkio\Shlink\Installer\Config\Option\BaseConfigOption;
use Shlinkio\Shlink\Installer\Config\Util\DatabaseDriver;
use Symfony\Component\Console\Style\StyleInterface;

use function array_keys;
Expand All @@ -14,16 +15,12 @@ class DatabaseDriverConfigOption extends BaseConfigOption
{
public const ENV_VAR = 'DB_DRIVER';
public const CONFIG_PATH = [self::ENV_VAR];
public const MYSQL_DRIVER = 'mysql';
public const POSTGRES_DRIVER = 'postgres';
public const SQLITE_DRIVER = 'sqlite';
public const MSSQL_DRIVER = 'mssql';
private const DATABASE_DRIVERS = [
'MySQL' => self::MYSQL_DRIVER,
'MariaDB' => self::MYSQL_DRIVER,
'PostgreSQL' => self::POSTGRES_DRIVER,
'MicrosoftSQL' => self::MSSQL_DRIVER,
'SQLite' => self::SQLITE_DRIVER,
'MySQL' => DatabaseDriver::MYSQL,
'MariaDB' => DatabaseDriver::MYSQL,
'PostgreSQL' => DatabaseDriver::POSTGRES,
'MicrosoftSQL' => DatabaseDriver::MSSQL,
'SQLite' => DatabaseDriver::SQLITE,
];

public function getDeprecatedPath(): array
Expand All @@ -40,6 +37,6 @@ public function ask(StyleInterface $io, PathCollection $currentOptions): string
{
$databases = array_keys(self::DATABASE_DRIVERS);
$dbType = $io->choice('Select database type', $databases, $databases[0]);
return self::DATABASE_DRIVERS[$dbType];
return self::DATABASE_DRIVERS[$dbType]->value;
}
}
3 changes: 2 additions & 1 deletion src/Config/Option/Database/DatabaseHostConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shlinkio\Shlink\Installer\Config\Option\Database;

use Shlinkio\Shlink\Config\Collection\PathCollection;
use Shlinkio\Shlink\Installer\Config\Util\DatabaseDriver;
use Symfony\Component\Console\Style\StyleInterface;

class DatabaseHostConfigOption extends AbstractNonSqliteDependentConfigOption
Expand All @@ -22,7 +23,7 @@ public function getEnvVar(): string
public function ask(StyleInterface $io, PathCollection $currentOptions): string
{
$dbDriver = $currentOptions->getValueInPath(DatabaseDriverConfigOption::CONFIG_PATH);
$extra = $dbDriver === DatabaseDriverConfigOption::POSTGRES_DRIVER ? ' (or unix socket)' : '';
$extra = DatabaseDriver::tryFrom($dbDriver) === DatabaseDriver::POSTGRES ? ' (or unix socket)' : '';

return $io->ask('Database host' . $extra, 'localhost');
}
Expand Down
9 changes: 2 additions & 7 deletions src/Config/Option/Database/DatabasePortConfigOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
namespace Shlinkio\Shlink\Installer\Config\Option\Database;

use Shlinkio\Shlink\Config\Collection\PathCollection;
use Shlinkio\Shlink\Installer\Config\Util\DatabaseDriver;
use Symfony\Component\Console\Style\StyleInterface;

class DatabasePortConfigOption extends AbstractNonSqliteDependentConfigOption
{
private const DRIVER_PORT_MAPPING = [
DatabaseDriverConfigOption::MYSQL_DRIVER => '3306',
DatabaseDriverConfigOption::POSTGRES_DRIVER => '5432',
DatabaseDriverConfigOption::MSSQL_DRIVER => '1433',
];

public function getDeprecatedPath(): array
{
return ['entity_manager', 'connection', 'port'];
Expand All @@ -34,6 +29,6 @@ public function ask(StyleInterface $io, PathCollection $currentOptions): string

private function getDefaultDbPortForDriver(string $driver): string
{
return self::DRIVER_PORT_MAPPING[$driver] ?? '';
return DatabaseDriver::tryFrom($driver)?->defaultPort() ?? '';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Shlinkio\Shlink\Installer\Config\Option\Database;

use Shlinkio\Shlink\Config\Collection\PathCollection;
use Shlinkio\Shlink\Installer\Config\Util\DatabaseDriver;
use Symfony\Component\Console\Style\StyleInterface;

class DatabaseUnixSocketConfigOption extends AbstractDriverDependentConfigOption
Expand All @@ -26,6 +27,6 @@ public function ask(StyleInterface $io, PathCollection $currentOptions): ?string

protected function shouldBeAskedForDbDriver(string $dbDriver): bool
{
return $dbDriver === DatabaseDriverConfigOption::MYSQL_DRIVER;
return DatabaseDriver::tryFrom($dbDriver) === DatabaseDriver::MYSQL;
}
}
23 changes: 23 additions & 0 deletions src/Config/Util/DatabaseDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace Shlinkio\Shlink\Installer\Config\Util;

enum DatabaseDriver: string
{
case MYSQL = 'mysql';
case POSTGRES = 'postgres';
case MSSQL = 'mssql';
case SQLITE = 'sqlite';

public function defaultPort(): ?string
{
return match ($this) {
self::MYSQL => '3306',
self::POSTGRES => '5432',
self::MSSQL => '1433',
self::SQLITE => null,
};
}
}
12 changes: 1 addition & 11 deletions src/Model/ImportedConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

final class ImportedConfig
{
private function __construct(private string $importPath, private array $importedConfig)
private function __construct(public readonly string $importPath, public readonly array $importedConfig)
{
}

Expand All @@ -19,14 +19,4 @@ public static function imported(string $importPath, array $importedConfig): self
{
return new self($importPath, $importedConfig);
}

public function importPath(): string
{
return $this->importPath;
}

public function importedConfig(): array
{
return $this->importedConfig;
}
}
12 changes: 6 additions & 6 deletions src/Util/InstallationCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace Shlinkio\Shlink\Installer\Util;

final class InstallationCommand
enum InstallationCommand: string
{
public const DB_CREATE_SCHEMA = 'db_create_schema';
public const DB_MIGRATE = 'db_migrate';
public const ORM_PROXIES = 'orm_proxies';
public const ORM_CLEAR_CACHE = 'orm_clear_cache';
public const GEOLITE_DOWNLOAD_DB = 'geolite_download_db';
case DB_CREATE_SCHEMA = 'db_create_schema';
case DB_MIGRATE = 'db_migrate';
case ORM_PROXIES = 'orm_proxies';
case ORM_CLEAR_CACHE = 'orm_clear_cache';
case GEOLITE_DOWNLOAD_DB = 'geolite_download_db';

public const POST_INSTALL_COMMANDS = [
self::DB_CREATE_SCHEMA,
Expand Down
11 changes: 7 additions & 4 deletions test/Command/InstallCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Process\PhpExecutableFinder;

use function count;
use function Functional\map;

class InstallCommandTest extends TestCase
{
Expand Down Expand Up @@ -65,10 +66,12 @@ public function setUp(): void
public function commandIsExecutedAsExpected(): void
{
$execPhpCommand = $this->commandsRunner->execPhpCommand(
Argument::that(function (string $command) {
Assert::assertContains($command, InstallationCommand::POST_INSTALL_COMMANDS);

return $command;
Argument::that(function (string $commandName) {
Assert::assertContains($commandName, map(
InstallationCommand::POST_INSTALL_COMMANDS,
fn (InstallationCommand $command) => $command->value,
));
return true;
}),
Argument::cetera(),
)->willReturn(true);
Expand Down
11 changes: 7 additions & 4 deletions test/Command/UpdateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Process\PhpExecutableFinder;

use function count;
use function Functional\map;

class UpdateCommandTest extends TestCase
{
Expand Down Expand Up @@ -65,10 +66,12 @@ public function setUp(): void
public function commandIsExecutedAsExpected(): void
{
$execPhpCommand = $this->commandsRunner->execPhpCommand(
Argument::that(function (string $command) {
Assert::assertContains($command, InstallationCommand::POST_UPDATE_COMMANDS);

return $command;
Argument::that(function (string $commandName) {
Assert::assertContains($commandName, map(
InstallationCommand::POST_UPDATE_COMMANDS,
fn (InstallationCommand $command) => $command->value,
));
return true;
}),
Argument::cetera(),
)->willReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Prophecy\PhpUnit\ProphecyTrait;
use Shlinkio\Shlink\Config\Collection\PathCollection;
use Shlinkio\Shlink\Installer\Config\Option\Database\DatabaseDriverConfigOption;
use Shlinkio\Shlink\Installer\Config\Util\DatabaseDriver;
use Symfony\Component\Console\Style\StyleInterface;

class DatabaseDriverConfigOptionTest extends TestCase
Expand All @@ -31,7 +32,7 @@ public function returnsExpectedConfig(): void
/** @test */
public function expectedQuestionIsAsked(): void
{
$expectedAnswer = DatabaseDriverConfigOption::SQLITE_DRIVER;
$expectedAnswer = DatabaseDriver::SQLITE->value;
$io = $this->prophesize(StyleInterface::class);
$choice = $io->choice(
'Select database type',
Expand Down
Loading