Yaffs site version 1.1
[yaffs-website] / vendor / drupal / console / src / Command / Site / ImportLocalCommand.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Console\Command\Site\ImportLocalCommand.
6  */
7
8 namespace Drupal\Console\Command\Site;
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 Symfony\Component\Console\Command\Command;
15 use Drupal\Console\Core\Command\Shared\CommandTrait;
16 use Drupal\Console\Core\Style\DrupalStyle;
17 use Drupal\Console\Core\Utils\ConfigurationManager;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Symfony\Component\Yaml\Yaml;
20
21 /**
22  * Class ImportLocalCommand
23  *
24  * @package Drupal\Console\Command\Site
25  */
26 class ImportLocalCommand extends Command
27 {
28     use CommandTrait;
29
30     /**
31      * @var string
32      */
33     protected $appRoot;
34
35     /**
36      * @var ConfigurationManager
37      */
38     protected $configurationManager;
39
40     /**
41      * ImportLocalCommand constructor.
42      *
43      * @param $appRoot
44      * @param ConfigurationManager $configurationManager
45      */
46     public function __construct(
47         $appRoot,
48         ConfigurationManager $configurationManager
49     ) {
50         $this->appRoot = $appRoot;
51         $this->configurationManager = $configurationManager;
52         parent::__construct();
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     protected function configure()
59     {
60         $this
61             ->setName('site:import:local')
62             ->setDescription($this->trans('commands.site.import.local.description'))
63             ->addArgument(
64                 'name',
65                 InputArgument::REQUIRED,
66                 $this->trans('commands.site.import.local.arguments.name')
67             )
68             ->addArgument(
69                 'directory',
70                 InputArgument::REQUIRED,
71                 $this->trans('commands.site.import.local.arguments.directory')
72             )
73             ->addOption(
74                 'environment',
75                 null,
76                 InputOption::VALUE_OPTIONAL,
77                 $this->trans('commands.site.import.local.options.environment')
78             )
79             ->setHelp($this->trans('commands.site.import.local.help'));
80         ;
81     }
82
83     /**
84      * {@inheritdoc}
85      */
86     protected function execute(InputInterface $input, OutputInterface $output)
87     {
88         $io = new DrupalStyle($input, $output);
89
90         $siteName = $input->getArgument('name');
91         $directory = $input->getArgument('directory');
92
93         $fileSystem = new Filesystem();
94         if (!$fileSystem->exists($directory)) {
95             $io->error(
96                 sprintf(
97                     $this->trans('commands.site.import.local.messages.error-missing'),
98                     $directory
99                 )
100             );
101
102             return 1;
103         }
104         
105         $environment = $input->getOption('environment')?:'local';
106
107         $siteConfig = [
108           $environment => [
109             'root' => $this->appRoot,
110             'host' => 'local',
111           ],
112         ];
113
114         $yaml = new Yaml();
115         $dump = $yaml::dump($siteConfig);
116
117         $userPath = sprintf('%s/.console/sites', $this->configurationManager->getHomeDirectory());
118         $configFile = sprintf('%s/%s.yml', $userPath, $siteName);
119
120         try {
121             $fileSystem->dumpFile($configFile, $dump);
122         } catch (\Exception $e) {
123             $io->error(
124                 sprintf(
125                     $this->trans('commands.site.import.local.messages.error-writing'),
126                     $e->getMessage()
127                 )
128             );
129
130             return 1;
131         }
132
133         $io->success(
134             sprintf(
135                 $this->trans('commands.site.import.local.messages.imported')
136             )
137         );
138     }
139
140     /**
141      * {@inheritdoc}
142      */
143     protected function interact(InputInterface $input, OutputInterface $output)
144     {
145         $io = new DrupalStyle($input, $output);
146
147         $directory = $input->getArgument('directory');
148         if (!$directory) {
149             $directory = $io->ask(
150                 $this->trans('commands.site.import.local.questions.directory'),
151                 getcwd()
152             );
153             $input->setArgument('directory', $directory);
154         }
155
156         $name = $input->getArgument('name');
157         if (!$name) {
158             $name = $io->ask(
159                 $this->trans('commands.site.import.local.questions.name')
160             );
161             $input->setArgument('name', $name);
162         }
163     }
164 }