Normalize a commit SHA for API and caching purposes.

@param \Platformsh\Client\Model\Environment $environment
@param string|null                          $sha

@return string|null
428421
private function normalizeSha(Environment $environment, $sha = null)
    {
        if ($environment->head_commit === null) {
            return null;
        }
        if ($sha === null) {
            return $environment->head_commit;
        }
        if (strpos($sha, 'HEAD') === 0) {
            $sha = $environment->head_commit . substr($sha, 4);
        }

        return $sha;
    }
1189160
public function getEnv($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_ENV;
        }

        return (isset($_ENV[$key])) ? $_ENV[$key] : $default;
    }
78581
public function get($key, $env = NULL)
    {
        if (!$env) {
            $env = self::$current_env;
        }

        return isset(self::$params[$env][$key]) ? self::$params[$env][$key] : NULL;
    }
41115
private function getCommitByShaHash(Environment $environment, $sha)
    {
        $cacheKey = $environment->project . ':' . $sha;
        $client = $this->api->getHttpClient();
        if ($cached = $this->cache->fetch($cacheKey)) {
            return new Commit($cached['data'], $cached['uri'], $client, true);
        }
        $baseUrl = Project::getProjectBaseFromUrl($environment->getUri()) . '/git/commits';
        $commit = \Platformsh\Client\Model\Git\Commit::get($sha, $baseUrl, $client);
        if ($commit === false) {
            return false;
        }
        $data = $commit->getData();
        // No need to cache API metadata.
        if (isset($data['_links']['self']['meta'])) {
            unset($data['_links']['self']['meta']);
        }
        $this->cache->save($cacheKey, [
            'data' => $data,
            'uri' => $baseUrl,
        ], 0);

        return $commit;
    }
228502
public static function createFromEnvironment(Environment $environment)
    {
        $data = [];
        $environment = self::determineAuthorization($environment);
        foreach ($environment as $key => $value) {
            $key = strtoupper($key);
            if (isset(static::$special[$key]) || strpos($key, 'HTTP_') === 0) {
                if ($key !== 'HTTP_CONTENT_LENGTH') {
                    $data[$key] =  $value;
                }
            }
        }

        return new static($data);
    }
37404
public static function env($key, $value = null)
    {
        if (is_string($value) || is_bool($value) || is_numeric($value)) {
            self::$configs[$key] = $value;
        } elseif ($value === null && isset(self::$configs[$key])) {
            return self::$configs[$key];
        }
    }
720982
public function setup(string $environment = null)
    {
        if (isset($this->initialized['setup'])) {
            return $this;
        }

        $this->initialized['setup'] = true;

        $this->measureTime('_setup', 'Site Setup', function () use ($environment) {
            // Force environment if passed to the method.
            if ($environment) {
                Setup::$environment = $environment;
            }

            $this['setup'];
            $this['streams'];
        });

        return $this;
    }
903382
public function detectEnvironment($environments)
    {
        $r = Request::getInstance();
        $pos = stripos($r->server->get('SCRIPT_NAME'), DISPATCHER_FILENAME);
        if ($pos > 0) {
            //we do this because in CLI circumstances (and some random ones) we would end up with index.ph instead of index.php
            $pos = $pos - 1;
        }
        $home = substr($r->server->get('SCRIPT_NAME'), 0, $pos);
        $this['app_relative_path'] = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $home), '/');

        $args = $this->isRunThroughCommandLineInterface() && isset($_SERVER['argv']) ? $_SERVER['argv'] : null;

        $detector = new EnvironmentDetector();

        return $this->environment = $detector->detect($environments, $args);
    }
612866
public function deploy(DNEnvironment $environment, $sha, DeploynautLogFile $log, DNProject $project, $leaveMaintenancePage = false) {
		$this->extend('deployStart', $environment, $sha, $log, $project);

		$file = sprintf('%s/%s.deploy-history.txt', DEPLOYNAUT_LOG_PATH, $environment->getFullName());
		$CLI_file = escapeshellarg($file);
		$CLI_line = escapeshellarg(date('Y-m-d H:i:s') . " => $sha");

		// Put maintenance page up
		$this->enableMaintenance($environment, $log, $project);

		// Do the deployment
		$log->write("Demo deployment: echo $CLI_line >> $CLI_file");
		`echo $CLI_line >> $CLI_file`;
		$log->write("Arbitrary pause for 10s");
		sleep(10);
		$log->write("Well, that was a waste of time");

		// Once the deployment has run it's necessary to update the maintenance page status
		if($leaveMaintenancePage) {
			$this->enableMaintenance($environment, $log, $project);
		} else {
			// Remove maintenance page if we want it to
			$this->disableMaintenance($environment, $log, $project);
		}

		$this->extend('deployEnd', $environment, $sha, $log, $project);
	}
457743
public static function detectEnv(array $environments, $uri)
        {
            foreach ($environments as $environment => $patterns) {
                // Essentially we just want to loop through each environment pattern
                // and determine if the current URI matches the pattern and if so
                // we will simply return the environment for that URI pattern.
                foreach ($patterns as $pattern) {
                    if (Inflector::is($pattern, $uri) or $pattern == gethostname()) {
                        return $environment;
                    }
                }
            }
        }
1192024
def config_for_env(hash)
      return from_environment_key(hash) if environment_keys?(hash)

      hash.each_with_object(IndifferentHash[]) do |(k, v), acc|
        if environment_keys?(v)
          acc.merge!(k => v[environment.to_s]) if v.key?(environment.to_s)
        else
          acc.merge!(k => v)
        end
      end
    end
1009773
public static function init($locale, $environment, $version)
    {
        $envFilename = Registry::get('applicationPath') . '/.env';
        if (file_exists($envFilename)) {
            $env = Ini::parse($envFilename, true, $locale . '-' . $environment . '-' . $version, 'common', false);
            $env = self::mergeEnvVariables($env);
        } else {
            $env = new \StdClass();
        }
        self::$data = Ini::bindArrayToObject($env);
    }
983531
public function parseHeaders(EnvironmentInterface $environment)
    {
        foreach ($environment as $key => $value) {
            $key = strtoupper($key);

            if (strpos($key, 'HTTP_') === 0 || in_array($key, $this->special)) {
                if ($key === 'HTTP_CONTENT_TYPE' || $key === 'HTTP_CONTENT_LENGTH') {
                    continue;
                }

                parent::set($this->normalizeKey($key), array($value));
            }
        }
    }
856355
public function saveEnvironment($environment) {
        $environment = (object) $environment;

        $environment_config = (array) $environment;
        unset($environment_config['app']);
        $this->config['apps'][$environment->app]['environments'][$environment->name] = $environment_config;
    }
887091
public static function setEnvironment($environment) {
        if (!empty($environment)) {
            foreach (array_values(static::$registeredInstances) as $instance) {
                $instance->setEnvironment($environment);
            }
            static::$ENVIRONMENT = $environment;
        } 
        
    }
496800
public function env(string $key)
    {
        $result = null;
        $break  = false;
        $keys   = explode('.', $key);
        $env    = $this->config->env;
        foreach ($keys as $key) {
            if (isset($env[$key])) {
                $env = $env[$key];
            } else {
                $break = true;
                break;
            }
        }
        if (!$break) {
            $result = $env;
        }
        return $result;
    }
686697
public function getSingleCommit($owner, $repo, $sha)
	{
		$data = array();
		
		return $this->client->request("/repos/$owner/$repo/commits/$sha", 'GET', $data, 200, 'GitHubFullCommit', true);
	}