Platformsh\Cli\Api::getEnvironments PHP Method

getEnvironments() public method

Return the user's environments.
public getEnvironments ( Platformsh\Client\Model\Project $project, boolean | null $refresh = null, boolean $events = true ) : Platformsh\Client\Model\Environment[]
$project Platformsh\Client\Model\Project The project.
$refresh boolean | null Whether to refresh the list.
$events boolean Whether to update Drush aliases if the list changes.
return Platformsh\Client\Model\Environment[] The user's environments.
    public function getEnvironments(Project $project, $refresh = null, $events = true)
    {
        $projectId = $project->getProperty('id');
        static $staticEnvironmentsCache;
        if (!$refresh && isset($staticEnvironmentsCache[$projectId])) {
            return $staticEnvironmentsCache[$projectId];
        }
        $cacheKey = 'environments:' . $projectId;
        $cached = self::$cache->fetch($cacheKey);
        if ($refresh === false && !$cached) {
            return [];
        } elseif ($refresh || !$cached) {
            $environments = [];
            $toCache = [];
            foreach ($project->getEnvironments() as $environment) {
                $environments[$environment->id] = $environment;
                $toCache[$environment->id] = $environment->getData();
            }
            // Dispatch an event if the list of environments has changed.
            if (isset($this->dispatcher) && $events && (!$cached || array_diff_key($environments, $cached))) {
                $this->dispatcher->dispatch('environments_changed', new EnvironmentsChangedEvent($project, $environments));
            }
            self::$cache->save($cacheKey, $toCache, $this->config->get('api.environments_ttl'));
        } else {
            $environments = [];
            $endpoint = $project->hasLink('self') ? $project->getLink('self', true) : $project->getProperty('endpoint');
            $guzzleClient = $this->getClient()->getConnector()->getClient();
            foreach ((array) $cached as $id => $data) {
                $environments[$id] = new Environment($data, $endpoint, $guzzleClient, true);
            }
        }
        $staticEnvironmentsCache[$projectId] = $environments;
        return $environments;
    }

Usage Example

コード例 #1
0
 /**
  * Get a list of environment IDs.
  *
  * @return string[]
  */
 public function getEnvironments()
 {
     $project = $this->getProject();
     if (!$project) {
         return [];
     }
     return array_keys($this->api->getEnvironments($project, false, false));
 }