*/ private static array $readableProps = [ 'environment_scope', 'key', 'value', 'protected', 'masked', 'raw', 'isDefault', ]; /** * CdEnvVariable * * @param \App\Enums\EnvironmentScope $environment_scope The scope on which the variable applies. * @param string $key The variable name. * @param string $value The variable value. * @param boolean $protected Export variable to pipelines running on * protected branches and tags only. * @param boolean $masked Variable will be masked in job logs. * Requires values to meet regular expression requirements. * @param boolean $raw Char '$' will be treated as the start of a reference * to another variable. * @param string $variable_type Should always be 'env_var' for now. * @return void */ public function __construct( protected EnvironmentScope $environment_scope, protected string $key, protected string $value, protected bool $protected = false, protected bool $masked = false, protected bool $raw = true, protected string $variable_type = 'env_var', ) { } /** * Make a CdEnvVariable * * @param array $params * @return self */ public static function make( array $params ): self { $wasPrefixed = Str::startsWith(Arr::get($params, 'key'), self::$k8sPrefix); self::removeVarNamePrefixes($params); $var = new self(...$params); if ($wasPrefixed) { $var->wasPrefixed(); } return $var; } /** * Property accessor * * @param string $name * @return boolean|string|\App\Enums\EnvironmentScope * @throws \Error If the property is not accessible. */ public function __get(string $name): bool|string|EnvironmentScope { if (\in_array($name, self::$readableProps)) { return $this->{$name}; } throw new \Error('No property ' . __CLASS__ . "::{$name}"); } /** * Set is default. * * @return self */ public function isProjectDefault(): self { $this->isDefault = true; return $this; } /** * Set was prefixed. * * @return self */ public function wasPrefixed(): self { $this->wasPrefixed = true; return $this; } /** * Normalize var name * * @return string */ public function getFormatedGitlabKey(): string { $key = $this->key; // * Format with wanted prefixes if ($this->isDefault) { $key = sprintf( '%s_%s', self::$prefix, $key ); } if ($this->wasPrefixed) { $key = sprintf( '%s_%s', self::$k8sPrefix, $key ); } return Str::upper($key); } // * PRIVATE /** * Remove var name prefixes * * @param array $params * @return void */ private static function removeVarNamePrefixes(array &$params): void { // * Force remove k8sPrefix if (Str::startsWith(Arr::get($params, 'key'), self::$k8sPrefix)) { Arr::set($params, 'key', Str::replaceStart(self::$k8sPrefix . '_', '', Arr::get($params, 'key'))); } // * Force remove prefix if (Str::startsWith(Arr::get($params, 'key'), self::$prefix)) { Arr::set($params, 'key', Str::replaceStart(self::$prefix . '_', '', Arr::get($params, 'key'))); } } /** * String representation of the env variable * * @return string */ public function __toString(): string { $rowLength = 40; $titlePad = 20; $printStr = Str::of('')->newLine(); // * TOP ROW $printStr = $printStr->append(\str_pad(" VAR `{$this->key}` ", $rowLength, '_', \STR_PAD_BOTH))->newLine(); $printStr = $printStr->append(\str_pad('value', $titlePad, pad_type: \STR_PAD_LEFT)) ->append(' => '); $value = $this->masked ? '*********' : $this->value; $printStr = $printStr->append("`{$value}`")->newLine(); $printStr = $printStr->append(\str_pad('environment_scope', $titlePad, pad_type: \STR_PAD_LEFT)) ->append(' => ')->append('`') ->append($this->environment_scope->label()) ->append('`')->newLine(); if ($this->protected or $this->masked or $this->raw) { $printStr = $printStr->append(\str_pad('options', $titlePad, pad_type: \STR_PAD_LEFT)) ->append(' => ') ->append(collect([ $this->protected ? 'protected' : null, $this->masked ? 'masked' : null, $this->raw ? 'raw' : null, ])->filter()->implode(','))->newLine(); } $printStr = $printStr->append(\str_pad('variable_type', $titlePad, pad_type: \STR_PAD_LEFT)) ->append(' => ') ->append("`{$this->variable_type}`"); // * BOTTOM ROW $printStr = $printStr->newLine() ->append(\str_pad('', $rowLength, '_', \STR_PAD_LEFT)) ->newLine(); return $printStr->toString(); } }