jpcercal / environment

A simple library (with all methods covered by php unit tests) to increase the power of your environment variables.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Method to get all env variables

timkelty opened this issue · comments

My use case is I want to get an array of all env vars that start with PHP_INI_, and loop through them with ini_set.

If I could get an array of all env vars, that would help.

Or in this case, should I just use $_ENV?

Hello @timkelty, I think that this code will help you.

<?php

class FakeClass
{
    public function getEnvironmentVariables()
    {
        return array_merge($_ENV, $_SERVER);
    }

    public function getEnvVarsThatBeginsWith($envKey)
    {
        $callback = function($key) use ($envKey) {
            return stripos($key, $envKey) === 0;
        };

        return array_filter($this->getEnvironmentVariables(), $callback, ARRAY_FILTER_USE_KEY);
    }
}

$envvar = new FakeClass();

print_r($envvar->getEnvVarsThatBeginsWith('PHP'));

I'm thinking if this solution can be interesting for other developers and if true, I will create a pull request and a new release of this library.

Yep, that's basically what I'm doing!

It think it would be nice/more flexible if the param was a regex.

Then you could just do something like:
print_r($envvar->getEnvironmentVariablesRegEx('/^PHP_/'));

Something like this:
Allows you do pass a filter of arrays of keys, or a string regex:

public function getAll($filter = null, $defaultValue = null)
{
  $all = $this->getEnvironmentVariables();
  $vars = array_filter($all, function($varName) use($all, $filter) {
    if (is_array($filter)) {
      return in_array($varName, $all);
    } elseif (is_string($filter)) {
      return preg_match($filter, $varName);
    }

    return true;
  });

  $vars = array_map(function($rawValue) use($defaultValue) {
    if ($rawValue === null) {
        return $defaultValue;
    }

    return (new Resource($rawValue))->process();
  }, $vars);

  return $vars;
}

Other solution that you can use is create only one environment variable that contains all configurations that you need.

<?php

require 'vendor/autoload.php';

putenv('PHP_INI=["error_reporting"=>true]');

$configs = Cekurte\Environment\Environment::get('PHP_INI');

foreach ($configs as $key => $value) {
    echo $key . '=' . $value;
}

As you can see this configuration is an array, so when you get this environment variable with this library you should have an array. Why don't you used it?

Your suggestion is interesting.

Hey guy, I created a draft of your suggestion with a piece of sugar =)

So, you can get all data and filter them by environment variable name or value.

<?php

$configs = Cekurte\Environment\Environment::getAll([
    new Cekurte\Environment\Filter\KeyRegexFilter('/PROCESSOR/'),
    new Cekurte\Environment\Filter\ValueRegexFilter('/6/'),
]);

You can see all changes here:
https://github.com/jpcercal/environment/tree/feature/issue-9

Yeah, looks great! 😸

In that example, the results would be filtered by everything in the passed array, right? So you would only get the PROCESSOR=6 var?

Yeap!

Considering that exists only one environment variable with the name PROCESSOR and with the value six.

Love it! :shipit:

@timkelty I created a new release with this feature 6be36b0. Thanks a lot!