Skip to content

ExpandProperty env. default value. #22

@williamroboly

Description

@williamroboly

I have a proposed patch for the Expander method expandProperty() which currently checks and interpolates for env. variables. I found a need where certain processes require a default value in my yaml files for certain environment variables depending on whether I am working in a remote environment or local environment and whether a particular case has the variable defined or not. That being said, I rolled up a patch that does this:

if (str_starts_with($property_name, "env.")) {
  $env_spec = substr($property_name, 4);
  $parts = explode(':', $env_spec, 2);
  $env_key = $parts[0];
  $default = $parts[1] ?? null;
  
  if (!$data->has($property_name)) {
    $env_value = $_SERVER[$env_key] ?? getenv($env_key) ?: false;
    if ($env_value !== false && $env_value !== null && $env_value !== '') {
      $data->set($property_name, $env_value);
    } elseif ($default !== null) {
      $property_name = 'env.' . $env_key;
      $data->set($property_name, $default);
    }
  }
}

gramash-expander-property-default.patch

If its agreeable, I can create a PR for this and suggest a test as well.

I thought others might find it useful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions