Security update for Core, with self-updated composer
[yaffs-website] / vendor / drupal / console-core / src / Command / Chain / ChainCommand.php
index 5fdf71113ef2c023018b1cbdf1e942b91559597a..6625327c1bce43ecfe370d62d4e0b40be38894ad 100644 (file)
@@ -13,23 +13,19 @@ use Symfony\Component\Console\Input\InputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Input\ArrayInput;
 use Symfony\Component\Console\Input\InputOption;
-use Symfony\Component\Console\Command\Command;
 use Symfony\Component\Filesystem\Filesystem;
 use Symfony\Component\Yaml\Parser;
 use Drupal\Console\Core\Utils\ChainQueue;
 use Drupal\Console\Core\Utils\ChainDiscovery;
 use Drupal\Console\Core\Command\Shared\InputTrait;
-use Drupal\Console\Core\Style\DrupalStyle;
-use Drupal\Console\Core\Command\Shared\CommandTrait;
 
 /**
  * Class ChainCommand
  *
  * @package Drupal\Console\Core\Command\Chain
  */
-class ChainCommand extends Command
+class ChainCommand extends BaseCommand
 {
-    use CommandTrait;
     use InputTrait;
 
     /**
@@ -37,11 +33,6 @@ class ChainCommand extends Command
      */
     protected $chainQueue;
 
-    /**
-     * @var ChainDiscovery
-     */
-    protected $chainDiscovery;
-
     /**
      * ChainCommand constructor.
      *
@@ -53,9 +44,9 @@ class ChainCommand extends Command
         ChainDiscovery $chainDiscovery
     ) {
         $this->chainQueue = $chainQueue;
-        $this->chainDiscovery = $chainDiscovery;
 
-        parent::__construct();
+        parent::__construct($chainDiscovery);
+        $this->ignoreValidationErrors();
     }
 
     /**
@@ -71,12 +62,6 @@ class ChainCommand extends Command
                 null,
                 InputOption::VALUE_OPTIONAL,
                 $this->trans('commands.chain.options.file')
-            )
-            ->addOption(
-                'placeholder',
-                null,
-                InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
-                $this->trans('commands.chain.options.placeholder')
             );
     }
 
@@ -85,50 +70,50 @@ class ChainCommand extends Command
      */
     protected function interact(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-        $file = $input->getOption('file');
+        $file = $this->getFileOption();
 
-        if (!$file) {
-            $files = $this->chainDiscovery->getChainFiles(true);
-
-            $file = $io->choice(
-                $this->trans('commands.chain.questions.chain-file'),
-                array_values($files)
+        $chainContent = $this->chainDiscovery
+            ->parseContent(
+                $file,
+                $this->getOptionsAsArray()
             );
-        }
-
-        $file = calculateRealPath($file);
-        $input->setOption('file', $file);
-
-        $chainContent = $this->getFileContents($file);
-
-        $placeholder = $input->getOption('placeholder');
 
-        $inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);
-
-        if (!$placeholder && $inlinePlaceHolders) {
-            foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
-                $inlinePlaceHolderDefault = '';
-                if (strpos($inlinePlaceHolder, '|')>0) {
-                    $placeholderParts = explode('|', $inlinePlaceHolder);
-                    $inlinePlaceHolder = $placeholderParts[0];
-                    $inlinePlaceHolderDefault = $placeholderParts[1];
-                    $inlinePlaceHolders[$key] = $inlinePlaceHolder;
-                }
+        $inlinePlaceHolders = $this->chainDiscovery
+            ->extractInlinePlaceHolders($chainContent);
+
+        foreach ($inlinePlaceHolders as $inlinePlaceHolder => $inlinePlaceHolderValue) {
+            if (is_array($inlinePlaceHolderValue)) {
+                $placeHolderValue = $this->getIo()->choice(
+                    sprintf(
+                        $this->trans('commands.chain.messages.select-value-for-placeholder'),
+                        $inlinePlaceHolder
+                    ),
+                    $inlinePlaceHolderValue,
+                    current($inlinePlaceHolderValue)
+                );
+            } else {
+                $placeHolderValue = $this->getIo()->ask(
+                    sprintf(
+                        $this->trans(
+                            'commands.chain.messages.enter-value-for-placeholder'
+                        ),
+                        $inlinePlaceHolder
+                    ),
+                    $inlinePlaceHolderValue
+                );
+            }
 
-                $placeholder[] = sprintf(
-                    '%s:%s',
+            if (!$input->hasOption($inlinePlaceHolder)) {
+                $this->addOption(
                     $inlinePlaceHolder,
-                    $io->ask(
-                        sprintf(
-                            'Enter value for %s placeholder',
-                            $inlinePlaceHolder
-                        ),
-                        $inlinePlaceHolderDefault
-                    )
+                    null,
+                    InputOption::VALUE_OPTIONAL,
+                    null,
+                    null
                 );
             }
-            $input->setOption('placeholder', $placeholder);
+
+            $input->setOption($inlinePlaceHolder, $placeHolderValue);
         }
     }
 
@@ -137,14 +122,11 @@ class ChainCommand extends Command
      */
     protected function execute(InputInterface $input, OutputInterface $output)
     {
-        $io = new DrupalStyle($input, $output);
-
         $interactive = false;
-        $learning = $input->hasOption('learning')?$input->getOption('learning'):false;
 
         $file = $input->getOption('file');
         if (!$file) {
-            $io->error($this->trans('commands.chain.messages.missing_file'));
+            $this->getIo()->error($this->trans('commands.chain.messages.missing-file'));
 
             return 1;
         }
@@ -153,9 +135,9 @@ class ChainCommand extends Command
         $file = calculateRealPath($file);
 
         if (!$fileSystem->exists($file)) {
-            $io->error(
+            $this->getIo()->error(
                 sprintf(
-                    $this->trans('commands.chain.messages.invalid_file'),
+                    $this->trans('commands.chain.messages.invalid-file'),
                     $file
                 )
             );
@@ -163,135 +145,84 @@ class ChainCommand extends Command
             return 1;
         }
 
-        $placeholder = $input->getOption('placeholder');
-        if ($placeholder) {
-            $placeholder = $this->inlineValueAsArray($placeholder);
-        }
-        $placeHolderOptions = [];
-        foreach ($placeholder as $placeholderItem) {
-            $placeHolderOptions[] = key($placeholderItem);
-        }
+        $chainContent = $this->chainDiscovery
+            ->parseContent(
+                $file,
+                $this->getOptionsAsArray()
+            );
 
-        $chainContent = $this->getFileContents($file);
-        $inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);
+        // Resolve inlinePlaceHolders
+        $inlinePlaceHolders = $this->chainDiscovery
+            ->extractInlinePlaceHolders($chainContent);
 
         if ($inlinePlaceHolders) {
-            foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
-                if (!strpos($inlinePlaceHolder, '|')) {
-                    continue;
-                }
-
-                $placeholderParts = explode('|', $inlinePlaceHolder);
-                $inlinePlaceHolder = $placeholderParts[0];
-                $inlinePlaceHolderDefault = $placeholderParts[1];
-
-                if (!$inlinePlaceHolderDefault) {
-                    continue;
-                }
-
-                if (in_array($inlinePlaceHolder, $placeHolderOptions)) {
-                    continue;
-                }
-
-                $placeholder[] = [$inlinePlaceHolder => $inlinePlaceHolderDefault];
-            }
-        }
-
-        $environmentPlaceHolders = $this->extractEnvironmentPlaceHolders($chainContent);
-        $envPlaceHolderMap = [];
-        $missingEnvironmentPlaceHolders = [];
-        foreach ($environmentPlaceHolders as $envPlaceHolder) {
-            if (!getenv($envPlaceHolder)) {
-                $missingEnvironmentPlaceHolders[$envPlaceHolder] = sprintf(
-                    'export %s=%s_VALUE',
-                    $envPlaceHolder,
-                    strtoupper($envPlaceHolder)
-                );
-
-                continue;
-            }
-            $envPlaceHolderMap[$envPlaceHolder] = getenv($envPlaceHolder);
-        }
-
-        if ($missingEnvironmentPlaceHolders) {
-            $io->error(
+            $this->getIo()->error(
                 sprintf(
-                    $this->trans('commands.chain.messages.missing-environment-placeholders'),
-                    implode(', ', array_keys($missingEnvironmentPlaceHolders))
+                    $this->trans('commands.chain.messages.missing-inline-placeholders'),
+                    implode(', ', array_keys($inlinePlaceHolders))
                 )
             );
 
-            $io->info($this->trans('commands.chain.messages.set-environment-placeholders'));
-            $io->block(array_values($missingEnvironmentPlaceHolders));
-
-            return 1;
-        }
-
-        $envPlaceHolderData = new ArrayDataSource($envPlaceHolderMap);
-        $placeholderResolver = new RegexPlaceholderResolver($envPlaceHolderData, '${{', '}}');
-        $chainContent = $placeholderResolver->resolvePlaceholder($chainContent);
-
-        $inlinePlaceHolders = $this->extractInlinePlaceHolders($chainContent);
-
-        $inlinePlaceHoldersReplacements = [];
-        foreach ($inlinePlaceHolders as $key => $inlinePlaceHolder) {
-            if (strpos($inlinePlaceHolder, '|') > 0) {
-                $placeholderParts = explode('|', $inlinePlaceHolder);
-                $inlinePlaceHoldersReplacements[] = $placeholderParts[0];
-                continue;
-            }
-            $inlinePlaceHoldersReplacements[] = $inlinePlaceHolder;
-        }
-
-        $chainContent = str_replace(
-            $inlinePlaceHolders,
-            $inlinePlaceHoldersReplacements,
-            $chainContent
-        );
-
-        $inlinePlaceHolders = $inlinePlaceHoldersReplacements;
-
-        $inlinePlaceHolderMap = [];
-        foreach ($placeholder as $key => $placeholderItem) {
-            $inlinePlaceHolderMap = array_merge($inlinePlaceHolderMap, $placeholderItem);
-        }
-
-        $missingInlinePlaceHolders = [];
+            $this->getIo()->info(
+                $this->trans(
+                    'commands.chain.messages.set-inline-placeholders'
+                )
+            );
 
-        foreach ($inlinePlaceHolders as $inlinePlaceHolder) {
-            if (!array_key_exists($inlinePlaceHolder, $inlinePlaceHolderMap)) {
-                $missingInlinePlaceHolders[$inlinePlaceHolder] = sprintf(
-                    '--placeholder="%s:%s_VALUE"',
+            foreach ($inlinePlaceHolders as $inlinePlaceHolder => $inlinePlaceHolderValue) {
+                $missingInlinePlaceHoldersMessage = sprintf(
+                    '--%s="%s_VALUE"',
                     $inlinePlaceHolder,
                     strtoupper($inlinePlaceHolder)
                 );
+
+                $this->getIo()->block($missingInlinePlaceHoldersMessage);
             }
+
+            return 1;
         }
 
-        if ($missingInlinePlaceHolders) {
-            $io->error(
+        // Resolve environmentPlaceHolders
+        $environmentPlaceHolders = $this->chainDiscovery
+            ->extractEnvironmentPlaceHolders($chainContent);
+        if ($environmentPlaceHolders) {
+            $this->getIo()->error(
                 sprintf(
-                    $this->trans('commands.chain.messages.missing-inline-placeholders'),
-                    implode(', ', array_keys($missingInlinePlaceHolders))
+                    $this->trans(
+                        'commands.chain.messages.missing-environment-placeholders'
+                    ),
+                    implode(
+                        ', ',
+                        array_values($environmentPlaceHolders)
+                    )
                 )
             );
 
-            $io->info($this->trans('commands.chain.messages.set-inline-placeholders'));
-            $io->block(array_values($missingInlinePlaceHolders));
+            $this->getIo()->info(
+                $this->trans(
+                    'commands.chain.messages.set-environment-placeholders'
+                )
+            );
+
+            foreach ($environmentPlaceHolders as $envPlaceHolder) {
+                $missingEnvironmentPlaceHoldersMessage = sprintf(
+                    'export %s=%s_VALUE',
+                    $envPlaceHolder,
+                    strtoupper($envPlaceHolder)
+                );
+
+                $this->getIo()->block($missingEnvironmentPlaceHoldersMessage);
+            }
 
             return 1;
         }
 
-        $inlinePlaceHolderData = new ArrayDataSource($inlinePlaceHolderMap);
-        $placeholderResolver = new RegexPlaceholderResolver($inlinePlaceHolderData, '%{{', '}}');
-        $chainContent = $placeholderResolver->resolvePlaceholder($chainContent);
-
         $parser = new Parser();
-        $configData = $parser->parse($chainContent);
+        $chainData = $parser->parse($chainContent);
 
         $commands = [];
-        if (array_key_exists('commands', $configData)) {
-            $commands = $configData['commands'];
+        if (array_key_exists('commands', $chainData)) {
+            $commands = $chainData['commands'];
         }
 
         $chainInlineOptions = $input->getOptions();
@@ -329,8 +260,8 @@ class ChainCommand extends Command
                 continue;
             }
 
-            $io->text($command['command']);
-            $io->newLine();
+            $this->getIo()->text($command['command']);
+            $this->getIo()->newLine();
 
             $input = new ArrayInput($moduleInputs);
             if (!is_null($interactive)) {
@@ -339,10 +270,10 @@ class ChainCommand extends Command
 
             $allowFailure = array_key_exists('allow_failure', $command)?$command['allow_failure']:false;
             try {
-                $callCommand->run($input, $io);
+                $callCommand->run($input, $this->getIo());
             } catch (\Exception $e) {
                 if (!$allowFailure) {
-                    $io->error($e->getMessage());
+                    $this->getIo()->error($e->getMessage());
                     return 1;
                 }
             }
@@ -350,50 +281,4 @@ class ChainCommand extends Command
 
         return 0;
     }
-
-    /**
-     * Helper to load and clean up the chain file.
-     *
-     * @param string $file The file name
-     *
-     * @return string $contents The contents of the file
-     */
-    private function getFileContents($file)
-    {
-        $contents = file_get_contents($file);
-
-        // Remove lines with comments.
-        $contents = preg_replace('![ \t]*#.*[ \t]*[\r|\r\n|\n]!', PHP_EOL, $contents);
-        //  Strip blank lines
-        $contents = preg_replace("/(^[\r\n]*|[\r\n]+)[\t]*[\r\n]+/", PHP_EOL, $contents);
-
-        return $contents;
-    }
-
-    private function extractPlaceHolders($chainContent, $identifier)
-    {
-        $placeHolders = [];
-        $regex = '/\\'.$identifier.'{{(.*?)}}/';
-        preg_match_all(
-            $regex,
-            $chainContent,
-            $placeHolders
-        );
-
-        if (!$placeHolders) {
-            return [];
-        }
-
-        return array_unique($placeHolders[1]);
-    }
-
-    private function extractInlinePlaceHolders($chainContent)
-    {
-        return $this->extractPlaceHolders($chainContent, '%');
-    }
-
-    private function extractEnvironmentPlaceHolders($chainContent)
-    {
-        return $this->extractPlaceHolders($chainContent, '$');
-    }
 }