StackFormation\BlueprintFactory::findByStackname PHP Метод

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

public findByStackname ( $stackname )
    public function findByStackname($stackname)
    {
        foreach ($this->config->getBlueprintNames() as $blueprintName) {
            if (strpos($blueprintName, '{env:') !== false) {
                $regex = preg_replace('/\\{env:([^:\\}\\{]+?)\\}/', '(?P<$1>\\w+)', $blueprintName);
                $matches = [];
                if (preg_match('/' . $regex . '/', $stackname, $matches)) {
                    foreach ($matches as $key => $value) {
                        if (is_int($key)) {
                            unset($matches[$key]);
                        }
                    }
                    return ['blueprint' => $blueprintName, 'envvars' => $matches];
                }
            }
        }
        return false;
    }

Usage Example

Пример #1
0
 protected function interactAskForBlueprint(InputInterface $input, OutputInterface $output)
 {
     $blueprint = $input->getArgument('blueprint');
     if (empty($blueprint) || strpos($blueprint, '*') !== false || strpos($blueprint, '?') !== false) {
         $choices = $this->blueprintFactory->getBlueprintLabels($blueprint ? $blueprint : null);
         if (count($choices) == 0) {
             throw new \Exception('No matching blueprints found');
         } elseif (count($choices) == 1) {
             $blueprint = end($choices);
         } else {
             $helper = $this->getHelper('question');
             $question = new ChoiceQuestion('Please select a blueprint', $choices);
             $question->setErrorMessage('Blueprint %s is invalid.');
             $blueprint = $helper->ask($input, $output, $question);
         }
         $output->writeln('Selected blueprint: ' . $blueprint);
         list($blueprint) = explode(' ', $blueprint);
     } elseif (!empty($blueprint) && !$this->blueprintFactory->blueprintExists($blueprint)) {
         if ($result = $this->blueprintFactory->findByStackname($blueprint)) {
             $output->writeln('Blueprint reverse match found: <fg=green>' . $result['blueprint'] . '</>');
             $output->writeln('With ENV vars: <fg=green>' . Helper\Div::assocArrayToString($result['envvars']) . '</>');
             $helper = $this->getHelper('question');
             $question = new ConfirmationQuestion("Use this blueprint and set env vars? [y/N] ", false);
             if (!$helper->ask($input, $output, $question)) {
                 throw new \Exception('Operation aborted');
             }
             $blueprint = $result['blueprint'];
             foreach ($result['envvars'] as $var => $value) {
                 $output->writeln("Setting env var: {$var}={$value}");
                 putenv("{$var}={$value}");
             }
         }
     }
     $input->setArgument('blueprint', $blueprint);
     return $blueprint;
 }