StackFormation\StackFactory::getStacksFromApi PHP Method

getStacksFromApi() public method

public getStacksFromApi ( boolean $fresh = false, null $nameFilter = null, null $statusFilter = null ) : Stack[]
$fresh boolean
$nameFilter null
$statusFilter null
return Stack[]
    public function getStacksFromApi($fresh = false, $nameFilter = null, $statusFilter = null)
    {
        if ($fresh || is_null($this->stacksCache)) {
            $this->stacksCache = [];
            $nextToken = '';
            do {
                $res = $this->cfnClient->describeStacks($nextToken ? ['NextToken' => $nextToken] : null);
                foreach ($res->get('Stacks') as $stack) {
                    $this->stacksCache[$stack['StackName']] = new Stack($stack, $this->cfnClient);
                }
                $nextToken = $res->get('NextToken');
            } while ($nextToken);
        }
        $stacks = $this->stacksCache;
        ksort($stacks);
        if (is_null($nameFilter)) {
            if ($filter = getenv('STACKFORMATION_NAME_FILTER')) {
                $nameFilter = $filter;
            }
        }
        // filter names
        if (!is_null($nameFilter)) {
            foreach (array_keys($stacks) as $stackName) {
                if (!preg_match($nameFilter, $stackName)) {
                    unset($stacks[$stackName]);
                }
            }
        }
        // filter status
        if (!is_null($statusFilter)) {
            foreach ($stacks as $stackName => $stack) {
                /* @var $stack Stack */
                if (!preg_match($statusFilter, $stack->getStatus())) {
                    unset($stacks[$stackName]);
                }
            }
        }
        return $stacks;
    }