mirror of
https://github.com/shlinkio/shlink.git
synced 2026-03-10 17:23:12 +08:00
Migrate dev-specific configuration to env vars via .env file
This commit is contained in:
19
module/Core/src/Config/Options/AppOptions.php
Normal file
19
module/Core/src/Config/Options/AppOptions.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use function sprintf;
|
||||
|
||||
final class AppOptions
|
||||
{
|
||||
public function __construct(public string $name = 'Shlink', public string $version = '3.0.0')
|
||||
{
|
||||
}
|
||||
|
||||
public function __toString(): string
|
||||
{
|
||||
return sprintf('%s:v%s', $this->name, $this->version);
|
||||
}
|
||||
}
|
||||
28
module/Core/src/Config/Options/DeleteShortUrlsOptions.php
Normal file
28
module/Core/src/Config/Options/DeleteShortUrlsOptions.php
Normal file
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_DELETE_SHORT_URL_THRESHOLD;
|
||||
|
||||
final readonly class DeleteShortUrlsOptions
|
||||
{
|
||||
public function __construct(
|
||||
public int $visitsThreshold = DEFAULT_DELETE_SHORT_URL_THRESHOLD,
|
||||
public bool $checkVisitsThreshold = true,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
$threshold = EnvVars::DELETE_SHORT_URL_THRESHOLD->loadFromEnv();
|
||||
|
||||
return new self(
|
||||
visitsThreshold: (int) ($threshold ?? DEFAULT_DELETE_SHORT_URL_THRESHOLD),
|
||||
checkVisitsThreshold: $threshold !== null,
|
||||
);
|
||||
}
|
||||
}
|
||||
57
module/Core/src/Config/Options/NotFoundRedirectOptions.php
Normal file
57
module/Core/src/Config/Options/NotFoundRedirectOptions.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
use Shlinkio\Shlink\Core\Config\NotFoundRedirectConfigInterface;
|
||||
|
||||
final readonly class NotFoundRedirectOptions implements NotFoundRedirectConfigInterface
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $invalidShortUrl = null,
|
||||
public ?string $regular404 = null,
|
||||
public ?string $baseUrl = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self(
|
||||
invalidShortUrl: EnvVars::DEFAULT_INVALID_SHORT_URL_REDIRECT->loadFromEnv(),
|
||||
regular404: EnvVars::DEFAULT_REGULAR_404_REDIRECT->loadFromEnv(),
|
||||
baseUrl: EnvVars::DEFAULT_BASE_URL_REDIRECT->loadFromEnv(),
|
||||
);
|
||||
}
|
||||
|
||||
public function invalidShortUrlRedirect(): ?string
|
||||
{
|
||||
return $this->invalidShortUrl;
|
||||
}
|
||||
|
||||
public function hasInvalidShortUrlRedirect(): bool
|
||||
{
|
||||
return $this->invalidShortUrl !== null;
|
||||
}
|
||||
|
||||
public function regular404Redirect(): ?string
|
||||
{
|
||||
return $this->regular404;
|
||||
}
|
||||
|
||||
public function hasRegular404Redirect(): bool
|
||||
{
|
||||
return $this->regular404 !== null;
|
||||
}
|
||||
|
||||
public function baseUrlRedirect(): ?string
|
||||
{
|
||||
return $this->baseUrl;
|
||||
}
|
||||
|
||||
public function hasBaseUrlRedirect(): bool
|
||||
{
|
||||
return $this->baseUrl !== null;
|
||||
}
|
||||
}
|
||||
47
module/Core/src/Config/Options/QrCodeOptions.php
Normal file
47
module/Core/src/Config/Options/QrCodeOptions.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_BG_COLOR;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_COLOR;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ERROR_CORRECTION;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_FORMAT;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_MARGIN;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_ROUND_BLOCK_SIZE;
|
||||
use const Shlinkio\Shlink\DEFAULT_QR_CODE_SIZE;
|
||||
|
||||
final readonly class QrCodeOptions
|
||||
{
|
||||
public function __construct(
|
||||
public int $size = DEFAULT_QR_CODE_SIZE,
|
||||
public int $margin = DEFAULT_QR_CODE_MARGIN,
|
||||
public string $format = DEFAULT_QR_CODE_FORMAT,
|
||||
public string $errorCorrection = DEFAULT_QR_CODE_ERROR_CORRECTION,
|
||||
public bool $roundBlockSize = DEFAULT_QR_CODE_ROUND_BLOCK_SIZE,
|
||||
public bool $enabledForDisabledShortUrls = DEFAULT_QR_CODE_ENABLED_FOR_DISABLED_SHORT_URLS,
|
||||
public string $color = DEFAULT_QR_CODE_COLOR,
|
||||
public string $bgColor = DEFAULT_QR_CODE_BG_COLOR,
|
||||
public ?string $logoUrl = null,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self(
|
||||
size: (int) EnvVars::DEFAULT_QR_CODE_SIZE->loadFromEnv(),
|
||||
margin: (int) EnvVars::DEFAULT_QR_CODE_MARGIN->loadFromEnv(),
|
||||
format: EnvVars::DEFAULT_QR_CODE_FORMAT->loadFromEnv(),
|
||||
errorCorrection: EnvVars::DEFAULT_QR_CODE_ERROR_CORRECTION->loadFromEnv(),
|
||||
roundBlockSize: (bool) EnvVars::DEFAULT_QR_CODE_ROUND_BLOCK_SIZE->loadFromEnv(),
|
||||
enabledForDisabledShortUrls: (bool) EnvVars::QR_CODE_FOR_DISABLED_SHORT_URLS->loadFromEnv(),
|
||||
color: EnvVars::DEFAULT_QR_CODE_COLOR->loadFromEnv(),
|
||||
bgColor: EnvVars::DEFAULT_QR_CODE_BG_COLOR->loadFromEnv(),
|
||||
logoUrl: EnvVars::DEFAULT_QR_CODE_LOGO_URL->loadFromEnv(),
|
||||
);
|
||||
}
|
||||
}
|
||||
19
module/Core/src/Config/Options/RabbitMqOptions.php
Normal file
19
module/Core/src/Config/Options/RabbitMqOptions.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
|
||||
final readonly class RabbitMqOptions
|
||||
{
|
||||
public function __construct(public bool $enabled = false)
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self((bool) EnvVars::RABBITMQ_ENABLED->loadFromEnv());
|
||||
}
|
||||
}
|
||||
36
module/Core/src/Config/Options/RedirectOptions.php
Normal file
36
module/Core/src/Config/Options/RedirectOptions.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
use Shlinkio\Shlink\Core\Util\RedirectStatus;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_REDIRECT_CACHE_LIFETIME;
|
||||
use const Shlinkio\Shlink\DEFAULT_REDIRECT_STATUS_CODE;
|
||||
|
||||
final readonly class RedirectOptions
|
||||
{
|
||||
public RedirectStatus $redirectStatusCode;
|
||||
public int $redirectCacheLifetime;
|
||||
|
||||
public function __construct(
|
||||
int $redirectStatusCode = StatusCodeInterface::STATUS_FOUND,
|
||||
int $redirectCacheLifetime = DEFAULT_REDIRECT_CACHE_LIFETIME,
|
||||
) {
|
||||
$this->redirectStatusCode = RedirectStatus::tryFrom($redirectStatusCode) ?? DEFAULT_REDIRECT_STATUS_CODE;
|
||||
$this->redirectCacheLifetime = $redirectCacheLifetime > 0
|
||||
? $redirectCacheLifetime
|
||||
: DEFAULT_REDIRECT_CACHE_LIFETIME;
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self(
|
||||
redirectStatusCode: (int) EnvVars::REDIRECT_STATUS_CODE->loadFromEnv(),
|
||||
redirectCacheLifetime: (int) EnvVars::REDIRECT_CACHE_LIFETIME->loadFromEnv(),
|
||||
);
|
||||
}
|
||||
}
|
||||
33
module/Core/src/Config/Options/RobotsOptions.php
Normal file
33
module/Core/src/Config/Options/RobotsOptions.php
Normal file
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
|
||||
use function count;
|
||||
use function Shlinkio\Shlink\Core\splitByComma;
|
||||
|
||||
final readonly class RobotsOptions
|
||||
{
|
||||
/**
|
||||
* @param string[] $userAgents
|
||||
*/
|
||||
public function __construct(public bool $allowAllShortUrls = false, public array $userAgents = [])
|
||||
{
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self(
|
||||
allowAllShortUrls: (bool) EnvVars::ROBOTS_ALLOW_ALL_SHORT_URLS->loadFromEnv(),
|
||||
userAgents: splitByComma(EnvVars::ROBOTS_USER_AGENTS->loadFromEnv()),
|
||||
);
|
||||
}
|
||||
|
||||
public function hasUserAgents(): bool
|
||||
{
|
||||
return count($this->userAgents) > 0;
|
||||
}
|
||||
}
|
||||
62
module/Core/src/Config/Options/TrackingOptions.php
Normal file
62
module/Core/src/Config/Options/TrackingOptions.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
|
||||
use function array_key_exists;
|
||||
use function Shlinkio\Shlink\Core\splitByComma;
|
||||
|
||||
final readonly class TrackingOptions
|
||||
{
|
||||
/**
|
||||
* @param string[] $disableTrackingFrom
|
||||
*/
|
||||
public function __construct(
|
||||
// Tells if IP addresses should be anonymized before persisting, to fulfil data protection regulations.
|
||||
// This applies only if IP address tracking is enabled
|
||||
public bool $anonymizeRemoteAddr = true,
|
||||
// Tells if visits to not-found URLs should be tracked. The disableTracking option takes precedence
|
||||
public bool $trackOrphanVisits = true,
|
||||
// A query param that, if provided, will disable tracking of one particular visit. Always takes precedence over
|
||||
// other options
|
||||
public ?string $disableTrackParam = null,
|
||||
// If true, visits will not be tracked at all
|
||||
public bool $disableTracking = false,
|
||||
// If true, visits will be tracked, but neither the IP address, nor the location will be resolved
|
||||
public bool $disableIpTracking = false,
|
||||
// If true, the referrers will not be tracked
|
||||
public bool $disableReferrerTracking = false,
|
||||
// If true, the user agent will not be tracked
|
||||
public bool $disableUaTracking = false,
|
||||
// A list of IP addresses, patterns or CIDR blocks from which tracking is disabled by default
|
||||
public array $disableTrackingFrom = [],
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
return new self(
|
||||
anonymizeRemoteAddr: (bool) EnvVars::ANONYMIZE_REMOTE_ADDR->loadFromEnv(),
|
||||
trackOrphanVisits: (bool) EnvVars::TRACK_ORPHAN_VISITS->loadFromEnv(),
|
||||
disableTrackParam: EnvVars::DISABLE_TRACK_PARAM->loadFromEnv(),
|
||||
disableTracking: (bool) EnvVars::DISABLE_TRACKING->loadFromEnv(),
|
||||
disableIpTracking: (bool) EnvVars::DISABLE_IP_TRACKING->loadFromEnv(),
|
||||
disableReferrerTracking: (bool) EnvVars::DISABLE_REFERRER_TRACKING->loadFromEnv(),
|
||||
disableUaTracking: (bool) EnvVars::DISABLE_UA_TRACKING->loadFromEnv(),
|
||||
disableTrackingFrom: splitByComma(EnvVars::DISABLE_TRACKING_FROM->loadFromEnv()),
|
||||
);
|
||||
}
|
||||
|
||||
public function hasDisableTrackingFrom(): bool
|
||||
{
|
||||
return ! empty($this->disableTrackingFrom);
|
||||
}
|
||||
|
||||
public function queryHasDisableTrackParam(array $query): bool
|
||||
{
|
||||
return $this->disableTrackParam !== null && array_key_exists($this->disableTrackParam, $query);
|
||||
}
|
||||
}
|
||||
62
module/Core/src/Config/Options/UrlShortenerOptions.php
Normal file
62
module/Core/src/Config/Options/UrlShortenerOptions.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Shlinkio\Shlink\Core\Config\Options;
|
||||
|
||||
use Shlinkio\Shlink\Core\Config\EnvVars;
|
||||
use Shlinkio\Shlink\Core\ShortUrl\Model\ShortUrlMode;
|
||||
|
||||
use function max;
|
||||
|
||||
use const Shlinkio\Shlink\DEFAULT_SHORT_CODES_LENGTH;
|
||||
use const Shlinkio\Shlink\MIN_SHORT_CODES_LENGTH;
|
||||
|
||||
final readonly class UrlShortenerOptions
|
||||
{
|
||||
/**
|
||||
* @param array{schema: ?string, hostname: ?string} $domain
|
||||
*/
|
||||
public function __construct(
|
||||
public array $domain = ['schema' => null, 'hostname' => null],
|
||||
public int $defaultShortCodesLength = DEFAULT_SHORT_CODES_LENGTH,
|
||||
public bool $autoResolveTitles = false,
|
||||
public bool $appendExtraPath = false,
|
||||
public bool $multiSegmentSlugsEnabled = false,
|
||||
public bool $trailingSlashEnabled = false,
|
||||
public ShortUrlMode $mode = ShortUrlMode::STRICT,
|
||||
) {
|
||||
}
|
||||
|
||||
public static function fromEnv(): self
|
||||
{
|
||||
$shortCodesLength = max(
|
||||
(int) EnvVars::DEFAULT_SHORT_CODES_LENGTH->loadFromEnv(),
|
||||
MIN_SHORT_CODES_LENGTH,
|
||||
);
|
||||
$mode = EnvVars::SHORT_URL_MODE->loadFromEnv();
|
||||
|
||||
return new self(
|
||||
domain: [
|
||||
'schema' => ((bool) EnvVars::IS_HTTPS_ENABLED->loadFromEnv()) ? 'https' : 'http',
|
||||
'hostname' => EnvVars::DEFAULT_DOMAIN->loadFromEnv(),
|
||||
],
|
||||
defaultShortCodesLength: $shortCodesLength,
|
||||
autoResolveTitles: (bool) EnvVars::AUTO_RESOLVE_TITLES->loadFromEnv(),
|
||||
appendExtraPath: (bool) EnvVars::REDIRECT_APPEND_EXTRA_PATH->loadFromEnv(),
|
||||
multiSegmentSlugsEnabled: (bool) EnvVars::MULTI_SEGMENT_SLUGS_ENABLED->loadFromEnv(),
|
||||
trailingSlashEnabled: (bool) EnvVars::SHORT_URL_TRAILING_SLASH->loadFromEnv(),
|
||||
mode: ShortUrlMode::tryFrom($mode) ?? ShortUrlMode::STRICT,
|
||||
);
|
||||
}
|
||||
|
||||
public function isLooseMode(): bool
|
||||
{
|
||||
return $this->mode === ShortUrlMode::LOOSE;
|
||||
}
|
||||
|
||||
public function defaultDomain(): string
|
||||
{
|
||||
return $this->domain['hostname'] ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user