Platformsh\Cli\Command\CommandBase::getCurrentEnvironment PHP Method

getCurrentEnvironment() public method

Get the current environment if the user is in a project directory.
public getCurrentEnvironment ( Platformsh\Client\Model\Project $expectedProject = null, boolean | null $refresh = null ) : Platformsh\Client\Model\Environment | false
$expectedProject Platformsh\Client\Model\Project The expected project.
$refresh boolean | null Whether to refresh the environments or projects cache.
return Platformsh\Client\Model\Environment | false The current environment.
    public function getCurrentEnvironment(Project $expectedProject = null, $refresh = null)
    {
        if (!($projectRoot = $this->getProjectRoot()) || !($project = $this->getCurrentProject()) || $expectedProject !== null && $expectedProject->id !== $project->id) {
            return false;
        }
        $gitHelper = $this->getHelper('git');
        $gitHelper->setDefaultRepositoryDir($this->getProjectRoot());
        $config = $this->localProject->getProjectConfig($projectRoot);
        // Check if there is a manual mapping set for the current branch.
        if (!empty($config['mapping']) && ($currentBranch = $gitHelper->getCurrentBranch()) && !empty($config['mapping'][$currentBranch])) {
            $environment = $this->api()->getEnvironment($config['mapping'][$currentBranch], $project, $refresh);
            if ($environment) {
                $this->debug('Found mapped environment for branch ' . $currentBranch . ': ' . $environment->id);
                return $environment;
            } else {
                unset($config['mapping'][$currentBranch]);
                $this->localProject->writeCurrentProjectConfig($config, $projectRoot);
            }
        }
        // Check whether the user has a Git upstream set to a remote environment
        // ID.
        $upstream = $gitHelper->getUpstream();
        if ($upstream && strpos($upstream, '/') !== false) {
            list(, $potentialEnvironment) = explode('/', $upstream, 2);
            $environment = $this->api()->getEnvironment($potentialEnvironment, $project, $refresh);
            if ($environment) {
                $this->debug('Selected environment ' . $potentialEnvironment . ', based on Git upstream: ' . $upstream);
                return $environment;
            }
        }
        // There is no Git remote set. Fall back to trying the current branch
        // name.
        if (!empty($currentBranch) || ($currentBranch = $gitHelper->getCurrentBranch())) {
            $environment = $this->api()->getEnvironment($currentBranch, $project, $refresh);
            if (!$environment) {
                // Try a sanitized version of the branch name too.
                $currentBranchSanitized = Environment::sanitizeId($currentBranch);
                $environment = $this->api()->getEnvironment($currentBranchSanitized, $project, $refresh);
            }
            if ($environment) {
                $this->debug('Selected environment ' . $environment->id . ' based on branch name: ' . $currentBranch);
                return $environment;
            }
        }
        return false;
    }

Usage Example

 /**
  * Get a list of environments IDs that can be checked out.
  *
  * @return string[]
  */
 public function getEnvironmentsForCheckout()
 {
     $project = $this->platformCommand->getCurrentProject();
     if (!$project) {
         return [];
     }
     try {
         $currentEnvironment = $this->platformCommand->getCurrentEnvironment($project);
     } catch (\Exception $e) {
         $currentEnvironment = false;
     }
     $environments = $this->platformCommand->getEnvironments($project, false, false);
     if ($currentEnvironment) {
         $environments = array_filter($environments, function ($environment) use($currentEnvironment) {
             return $environment->id !== $currentEnvironment->id;
         });
     }
     return array_keys($environments);
 }