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

getCurrentProject() public method

Get the current project if the user is in a project directory.
public getCurrentProject ( ) : Platformsh\Client\Model\Project | false
return Platformsh\Client\Model\Project | false The current project
    public function getCurrentProject()
    {
        if (isset($this->currentProject)) {
            return $this->currentProject;
        }
        if (!($projectRoot = $this->getProjectRoot())) {
            return false;
        }
        $project = false;
        $config = $this->localProject->getProjectConfig($projectRoot);
        if ($config) {
            $project = $this->api()->getProject($config['id'], isset($config['host']) ? $config['host'] : null);
            // There is a chance that the project isn't available.
            if (!$project) {
                if (isset($config['host'])) {
                    $projectUrl = sprintf('https://%s/projects/%s', $config['host'], $config['id']);
                    $message = "Project not found: " . $projectUrl . "\nThe project probably no longer exists.";
                } else {
                    $message = "Project not found: " . $config['id'] . "\nEither you do not have access to the project or it no longer exists.";
                }
                throw new ProjectNotFoundException($message);
            }
            $this->debug('Selecting project ' . $config['id'] . ' based on project root');
        }
        $this->currentProject = $project;
        return $project;
    }

Usage Example

 /**
  * Get the preferred project for autocompletion.
  *
  * The project is either defined by an ID that the user has specified in
  * the command (via the 'id' argument of 'get', or the '--project' option),
  * or it is determined from the current path.
  *
  * @return \Platformsh\Client\Model\Project|false
  */
 protected function getProject()
 {
     if (!$this->projects) {
         return false;
     }
     $commandLine = $this->handler->getContext()->getCommandLine();
     $currentProjectId = $this->getProjectIdFromCommandLine($commandLine);
     if (!$currentProjectId && ($currentProject = $this->welcomeCommand->getCurrentProject())) {
         return $currentProject;
     } elseif (isset($this->projects[$currentProjectId])) {
         return $this->projects[$currentProjectId];
     }
     return false;
 }
All Usage Examples Of Platformsh\Cli\Command\CommandBase::getCurrentProject