76bcf88e6f11d584c2dcd4bcca42ed248b40d74f
[yaffs-website] / vendor / drupal / console / src / Command / Module / UpdateCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Module\UpdateCommand.
6  */
7
8 namespace Drupal\Console\Command\Module;
9
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputOption;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Console\Command\Command;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Command\Shared\ProjectDownloadTrait;
18 use Drupal\Console\Core\Utils\ShellProcess;
19
20 class UpdateCommand extends Command
21 {
22     use CommandTrait;
23     use ProjectDownloadTrait;
24
25
26     /**
27  * @var ShellProcess
28 */
29     protected $shellProcess;
30
31     /**
32      * @var string
33      */
34     protected $root;
35
36     /**
37      * UpdateCommand constructor.
38      *
39      * @param ShellProcess $shellProcess
40      * @param $root
41      */
42     public function __construct(
43         ShellProcess $shellProcess,
44         $root
45     ) {
46         $this->shellProcess = $shellProcess;
47         $this->root = $root;
48         parent::__construct();
49     }
50     protected function configure()
51     {
52         $this
53             ->setName('module:update')
54             ->setDescription($this->trans('commands.module.update.description'))
55             ->addArgument(
56                 'module',
57                 InputArgument::IS_ARRAY,
58                 $this->trans('commands.module.update.arguments.module')
59             )
60             ->addOption(
61                 'composer',
62                 '',
63                 InputOption::VALUE_NONE,
64                 $this->trans('commands.module.update.options.composer')
65             )
66             ->addOption(
67                 'simulate',
68                 '',
69                 InputOption::VALUE_NONE,
70                 $this->trans('commands.module.update.options.simulate')
71             );
72     }
73
74     /**
75      * {@inheritdoc}
76      */
77     protected function interact(InputInterface $input, OutputInterface $output)
78     {
79         $io = new DrupalStyle($input, $output);
80         $composer = $input->getOption('composer');
81         $module = $input->getArgument('module');
82
83         if (!$composer) {
84             $io->error($this->trans('commands.module.update.messages.only-composer'));
85
86             return 1;
87         }
88
89         if (!$module) {
90             $module = $this->modulesQuestion($io);
91             $input->setArgument('module', $module);
92         }
93     }
94
95     /**
96      * {@inheritdoc}
97      */
98     protected function execute(InputInterface $input, OutputInterface $output)
99     {
100         $io = new DrupalStyle($input, $output);
101
102         $modules = $input->getArgument('module');
103         $composer = $input->getOption('composer');
104         $simulate = $input->getOption('simulate');
105
106         if (!$composer) {
107             $io->error($this->trans('commands.module.update.messages.only-composer'));
108
109             return 1;
110         }
111
112         if (!$modules) {
113             $io->error(
114                 $this->trans('commands.module.update.messages.missing-module')
115             );
116
117             return 1;
118         }
119
120         if (count($modules) > 1) {
121             $modules = " drupal/" . implode(" drupal/", $modules);
122         } else {
123             $modules = " drupal/" . current($modules);
124         }
125
126         if ($composer) {
127             // Register composer repository
128             $command = "composer config repositories.drupal composer https://packagist.drupal-composer.org";
129             $this->shellProcess->exec($command, $this->root);
130
131             $command = 'composer update ' . $modules . ' --optimize-autoloader --prefer-dist --no-dev --root-reqs ';
132
133             if ($simulate) {
134                 $command .= " --dry-run";
135             }
136
137             if ($this->shellProcess->exec($command, $this->root)) {
138                 $io->success(
139                     sprintf(
140                         $this->trans('commands.module.update.messages.composer'),
141                         trim($modules)
142                     )
143                 );
144             }
145         }
146
147         return 0;
148     }
149 }