5 * Contains \Drupal\AppConsole\Command\Multisite\NewCommand.
8 namespace Drupal\Console\Command\Multisite;
10 use Drupal\Console\Core\Command\Shared\CommandTrait;
11 use Drupal\Console\Core\Style\DrupalStyle;
12 use Symfony\Component\Console\Command\Command;
13 use Symfony\Component\Console\Input\InputArgument;
14 use Symfony\Component\Console\Input\InputInterface;
15 use Symfony\Component\Console\Input\InputOption;
16 use Symfony\Component\Console\Output\OutputInterface;
17 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
18 use Symfony\Component\Filesystem\Filesystem;
19 use Symfony\Component\Filesystem\Exception\FileNotFoundException;
24 * @package Drupal\Console\Command\Multisite
26 class NewCommand extends Command
33 * DebugCommand constructor.
37 public function __construct($appRoot)
39 $this->appRoot = $appRoot;
40 parent::__construct();
51 protected $directory = '';
56 public function configure()
58 $this->setName('multisite:new')
59 ->setDescription($this->trans('commands.multisite.new.description'))
60 ->setHelp($this->trans('commands.multisite.new.help'))
63 InputArgument::REQUIRED,
64 $this->trans('commands.multisite.new.arguments.directory')
68 InputArgument::REQUIRED,
69 $this->trans('commands.multisite.new.arguments.uri')
74 InputOption::VALUE_NONE,
75 $this->trans('commands.multisite.new.options.copy-default')
82 protected function execute(InputInterface $input, OutputInterface $output)
84 $io = new DrupalStyle($input, $output);
85 $this->fs = new Filesystem();
86 $this->directory = $input->getArgument('directory');
88 if (!$this->directory) {
89 $io->error($this->trans('commands.multisite.new.errors.subdir-empty'));
94 if ($this->fs->exists($this->appRoot . '/sites/' . $this->directory)) {
97 $this->trans('commands.multisite.new.errors.subdir-exists'),
105 if (!$this->fs->exists($this->appRoot . '/sites/default')) {
106 $io->error($this->trans('commands.multisite.new.errors.default-missing'));
112 $this->fs->mkdir($this->appRoot . '/sites/' . $this->directory, 0755);
113 } catch (IOExceptionInterface $e) {
116 $this->trans('commands.multisite.new.errors.mkdir-fail'),
124 $uri = $input->getArgument('uri');
126 $this->addToSitesFile($io, $uri);
127 } catch (\Exception $e) {
128 $io->error($e->getMessage());
133 $this->createFreshSite($io);
139 * Adds line to sites.php that is needed for the new site to be recognized.
141 * @param DrupalStyle $output
144 * @throws FileNotFoundException
146 protected function addToSitesFile(DrupalStyle $output, $uri)
148 if ($this->fs->exists($this->appRoot . '/sites/sites.php')) {
149 $sites_is_dir = is_dir($this->appRoot . '/sites/sites.php');
150 $sites_readable = is_readable($this->appRoot . '/sites/sites.php');
151 if ($sites_is_dir || !$sites_readable) {
152 throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-invalid'));
154 $sites_file_contents = file_get_contents($this->appRoot . '/sites/sites.php');
155 } elseif ($this->fs->exists($this->appRoot . '/sites/example.sites.php')) {
156 $sites_file_contents = file_get_contents($this->appRoot . '/sites/example.sites.php');
157 $sites_file_contents .= "\n\$sites = [];";
159 throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-missing'));
162 $sites_file_contents .= "\n\$sites['$this->directory'] = '$this->directory';";
165 $this->fs->dumpFile($this->appRoot . '/sites/sites.php', $sites_file_contents);
166 $this->fs->chmod($this->appRoot . '/sites/sites.php', 0640);
167 } catch (IOExceptionInterface $e) {
168 $output->error('commands.multisite.new.errors.sites-other');
173 * Copies detected default install alters settings.php to fit the new directory.
175 * @param DrupalStyle $io
177 protected function copyExistingInstall(DrupalStyle $io)
179 if (!$this->fs->exists($this->appRoot . '/sites/default/settings.php')) {
182 $this->trans('commands.multisite.new.errors.file-missing'),
183 'sites/default/settings.php'
189 if ($this->fs->exists($this->appRoot . '/sites/default/files')) {
192 $this->appRoot . '/sites/default/files',
193 $this->appRoot . '/sites/' . $this->directory . '/files'
195 } catch (IOExceptionInterface $e) {
198 $this->trans('commands.multisite.new.errors.copy-fail'),
199 'sites/default/files',
200 'sites/' . $this->directory . '/files'
206 $io->warning($this->trans('commands.multisite.new.warnings.missing-files'));
209 $settings = file_get_contents($this->appRoot . '/sites/default/settings.php');
210 $settings = str_replace('sites/default', 'sites/' . $this->directory, $settings);
214 $this->appRoot . '/sites/' . $this->directory . '/settings.php',
217 } catch (IOExceptionInterface $e) {
220 $this->trans('commands.multisite.new.errors.write-fail'),
221 'sites/' . $this->directory . '/settings.php'
227 $this->chmodSettings($io);
231 $this->trans('commands.multisite.new.messages.copy-default'),
238 * Creates site folder with clean settings.php file.
240 * @param DrupalStyle $io
242 protected function createFreshSite(DrupalStyle $io)
244 if ($this->fs->exists($this->appRoot . '/sites/default/default.settings.php')) {
247 $this->appRoot . '/sites/default/default.settings.php',
248 $this->appRoot . '/sites/' . $this->directory . '/settings.php'
250 } catch (IOExceptionInterface $e) {
253 $this->trans('commands.multisite.new.errors.copy-fail'),
254 $this->appRoot . '/sites/default/default.settings.php',
255 $this->appRoot . '/sites/' . $this->directory . '/settings.php'
263 $this->trans('commands.multisite.new.errors.file-missing'),
264 'sites/default/default.settings.php'
270 $this->chmodSettings($io);
274 $this->trans('commands.multisite.new.messages.fresh-site'),
283 * Changes permissions of settings.php to 640.
285 * The copy will have 444 permissions by default, which makes it readable by
286 * anyone. Also, Drupal likes being able to write to it during, for example,
289 * @param DrupalStyle $io
291 protected function chmodSettings(DrupalStyle $io)
294 $this->fs->chmod($this->appRoot . '/sites/' . $this->directory . '/settings.php', 0640);
295 } catch (IOExceptionInterface $e) {
298 $this->trans('commands.multisite.new.errors.chmod-fail'),
299 $this->appRoot . '/sites/' . $this->directory . '/settings.php'