StackFormation\Diff::compare PHP Метод

compare() публичный Метод

public compare ( )
    public function compare()
    {
        if (empty($this->stack)) {
            throw new \InvalidArgumentException('Stack not set');
        }
        if (empty($this->blueprint)) {
            throw new \InvalidArgumentException('Blueprint not set');
        }
        $tmp = [];
        try {
            // parameters
            if ($this->output->isVerbose()) {
                $this->output->writeln($this->stack->getName() . ': Comparing parameters');
            }
            $parametersStack = $this->stack->getParameters();
            $parametersBlueprint = $this->blueprint->getParameters(true);
            $parametersBlueprint = Div::flatten($parametersBlueprint, 'ParameterKey', 'ParameterValue');
            if ($this->parametersAreEqual($parametersStack, $parametersBlueprint)) {
                $tmp['parameters'] = "<fg=green>equal</>";
            } else {
                $tmp['parameters'] = "<fg=red>different</>";
                $tmp['error'] = true;
            }
            // template
            if ($this->output->isVerbose()) {
                $this->output->writeln($this->stack->getName() . ': Comparing template');
            }
            $templateStack = trim($this->stack->getTemplate());
            $templateBlueprint = trim($this->blueprint->getPreprocessedTemplate());
            $templateStack = $this->normalizeJson($templateStack);
            $templateBlueprint = $this->normalizeJson($templateBlueprint);
            if ($templateStack === $templateBlueprint) {
                $tmp['template'] = "<fg=green>equal</>";
            } else {
                $tmp['template'] = "<fg=red>different</>";
                $tmp['error'] = true;
            }
        } catch (CloudFormationException $e) {
            $tmp['parameters'] = 'Stack not found';
            $tmp['template'] = 'Stack not found';
            $tmp['error'] = true;
        } catch (\Exception $e) {
            $tmp['parameters'] = '<fg=red>EXCEPTION: ' . $e->getMessage() . '</>';
            $tmp['template'] = 'EXCEPTION';
            $tmp['error'] = true;
        }
        return $tmp;
    }

Usage Example

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $nameFilter = $input->getOption('nameFilter');
     $stacks = $this->getStackFactory()->getStacksFromApi(false, $nameFilter);
     $error = false;
     $data = [];
     foreach ($stacks as $stackName => $stack) {
         /* @var $stack Stack */
         $this->dependencyTracker->reset();
         $tmp = [];
         $tmp['stackName'] = $stackName;
         $tmp['status'] = Helper\Decorator::decorateStatus($stack->getStatus());
         $tmp['blueprintName'] = '';
         $tmp['parameters'] = '';
         $tmp['template'] = '';
         $diff = new Diff($output);
         try {
             $blueprint = $this->blueprintFactory->getBlueprintByStack($stack);
             $env = $stack->getUsedEnvVars();
             $diff->setStack($stack);
             $diff->setBlueprint($blueprint);
             $tmp['blueprintName'] = '<fg=green>' . $blueprint->getName() . '</>';
             if (count($env)) {
                 $tmp['blueprintName'] .= "\n" . wordwrap(Div::assocArrayToString($stack->getUsedEnvVars()), 80, "\n");
             }
             $tmp = array_merge($tmp, $diff->compare());
             if (isset($tmp['error']) && $tmp['error'] === true) {
                 $error = true;
             }
         } catch (BlueprintReferenceNotFoundException $e) {
             $tmp['blueprintName'] = '-';
             $tmp['parameters'] = '<fg=red>not found</>';
             $tmp['template'] = '<fg=red>not found</>';
             $error = true;
         } catch (BlueprintNotFoundException $e) {
             $tmp['blueprintName'] = '<fg=red>Not found: ' . $e->getBlueprintName() . '</>';
             $tmp['parameters'] = '<fg=red>not found</>';
             $tmp['template'] = '<fg=red>not found</>';
             $error = true;
         } catch (\Exception $e) {
             $tmp['blueprintName'] = '<fg=red>Exception: ' . $e->getMessage() . '</>';
             $tmp['parameters'] = '<fg=red>exception</>';
             $tmp['template'] = '<fg=red>exception</>';
             $error = true;
         }
         if (isset($tmp['error'])) {
             // so the column doesn't show on in the output table
             unset($tmp['error']);
         }
         $data[] = $tmp;
     }
     $output->writeln('');
     $table = new Table($output);
     $table->setHeaders(['Stack', 'Status', 'Blueprint', 'Parameters', 'Template']);
     $table->setRows($data);
     $table->render();
     $output->writeln('');
     $output->writeln("-> Run this to show a diff for a specific stack:");
     $output->writeln("{$GLOBALS['argv'][0]} stack:diff <stackName>");
     $output->writeln('');
     $output->writeln("-> Run this to update a live stack:");
     $output->writeln("{$GLOBALS['argv'][0]} blueprint:deploy -o <blueprintName>");
     $output->writeln('');
     return $error === true ? 1 : 0;
 }