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