5 * Contains \Drupal\AppConsole\Command\Multisite\NewCommand.
8 namespace Drupal\Console\Command\Multisite;
10 use Drupal\Console\Core\Command\Command;
11 use Symfony\Component\Console\Input\InputArgument;
12 use Symfony\Component\Console\Input\InputInterface;
13 use Symfony\Component\Console\Input\InputOption;
14 use Symfony\Component\Console\Output\OutputInterface;
15 use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
16 use Symfony\Component\Filesystem\Filesystem;
17 use Symfony\Component\Filesystem\Exception\FileNotFoundException;
22 * @package Drupal\Console\Command\Multisite
24 class NewCommand extends Command
29 * DebugCommand constructor.
33 public function __construct($appRoot)
35 $this->appRoot = $appRoot;
36 parent::__construct();
47 protected $directory = '';
52 public function configure()
54 $this->setName('multisite:new')
55 ->setDescription($this->trans('commands.multisite.new.description'))
56 ->setHelp($this->trans('commands.multisite.new.help'))
59 InputArgument::REQUIRED,
60 $this->trans('commands.multisite.new.arguments.directory')
64 InputArgument::REQUIRED,
65 $this->trans('commands.multisite.new.arguments.uri')
70 InputOption::VALUE_NONE,
71 $this->trans('commands.multisite.new.options.copy-default')
73 ->setAliases(['mun']);
79 protected function execute(InputInterface $input, OutputInterface $output)
81 $this->fs = new Filesystem();
82 $this->directory = $input->getArgument('directory');
84 if (!$this->directory) {
85 $this->getIo()->error($this->trans('commands.multisite.new.errors.subdir-empty'));
90 if ($this->fs->exists($this->appRoot . '/sites/' . $this->directory)) {
91 $this->getIo()->error(
93 $this->trans('commands.multisite.new.errors.subdir-exists'),
101 if (!$this->fs->exists($this->appRoot . '/sites/default')) {
102 $this->getIo()->error($this->trans('commands.multisite.new.errors.default-missing'));
108 $this->fs->mkdir($this->appRoot . '/sites/' . $this->directory, 0755);
109 } catch (IOExceptionInterface $e) {
110 $this->getIo()->error(
112 $this->trans('commands.multisite.new.errors.mkdir-fail'),
120 $uri = $input->getArgument('uri');
122 $this->addToSitesFile($uri);
123 } catch (\Exception $e) {
124 $this->getIo()->error($e->getMessage());
129 $this->createFreshSite();
135 * Adds line to sites.php that is needed for the new site to be recognized.
139 * @throws FileNotFoundException
141 protected function addToSitesFile($uri)
143 if ($this->fs->exists($this->appRoot . '/sites/sites.php')) {
144 $sites_is_dir = is_dir($this->appRoot . '/sites/sites.php');
145 $sites_readable = is_readable($this->appRoot . '/sites/sites.php');
146 if ($sites_is_dir || !$sites_readable) {
147 throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-invalid'));
149 $sites_file_contents = file_get_contents($this->appRoot . '/sites/sites.php');
150 } elseif ($this->fs->exists($this->appRoot . '/sites/example.sites.php')) {
151 $sites_file_contents = file_get_contents($this->appRoot . '/sites/example.sites.php');
152 $sites_file_contents .= "\n\$sites = [];";
154 throw new FileNotFoundException($this->trans('commands.multisite.new.errors.sites-missing'));
157 $sites_file_contents .= "\n\$sites['$this->directory'] = '$this->directory';";
160 $this->fs->dumpFile($this->appRoot . '/sites/sites.php', $sites_file_contents);
161 $this->fs->chmod($this->appRoot . '/sites/sites.php', 0640);
162 } catch (IOExceptionInterface $e) {
163 $this->getIo()->error('commands.multisite.new.errors.sites-other');
168 * Copies detected default install alters settings.php to fit the new directory.
170 protected function copyExistingInstall()
172 if (!$this->fs->exists($this->appRoot . '/sites/default/settings.php')) {
173 $this->getIo()->error(
175 $this->trans('commands.multisite.new.errors.file-missing'),
176 'sites/default/settings.php'
182 if ($this->fs->exists($this->appRoot . '/sites/default/files')) {
185 $this->appRoot . '/sites/default/files',
186 $this->appRoot . '/sites/' . $this->directory . '/files'
188 } catch (IOExceptionInterface $e) {
189 $this->getIo()->error(
191 $this->trans('commands.multisite.new.errors.copy-fail'),
192 'sites/default/files',
193 'sites/' . $this->directory . '/files'
199 $this->getIo()->warning($this->trans('commands.multisite.new.warnings.missing-files'));
202 $settings = file_get_contents($this->appRoot . '/sites/default/settings.php');
203 $settings = str_replace('sites/default', 'sites/' . $this->directory, $settings);
207 $this->appRoot . '/sites/' . $this->directory . '/settings.php',
210 } catch (IOExceptionInterface $e) {
211 $this->getIo()->error(
213 $this->trans('commands.multisite.new.errors.write-fail'),
214 'sites/' . $this->directory . '/settings.php'
220 $this->chmodSettings();
222 $this->getIo()->success(
224 $this->trans('commands.multisite.new.messages.copy-default'),
231 * Creates site folder with clean settings.php file.
233 protected function createFreshSite()
235 if ($this->fs->exists($this->appRoot . '/sites/default/default.settings.php')) {
238 $this->appRoot . '/sites/default/default.settings.php',
239 $this->appRoot . '/sites/' . $this->directory . '/settings.php'
241 } catch (IOExceptionInterface $e) {
242 $this->getIo()->error(
244 $this->trans('commands.multisite.new.errors.copy-fail'),
245 $this->appRoot . '/sites/default/default.settings.php',
246 $this->appRoot . '/sites/' . $this->directory . '/settings.php'
252 $this->getIo()->error(
254 $this->trans('commands.multisite.new.errors.file-missing'),
255 'sites/default/default.settings.php'
261 $this->chmodSettings();
263 $this->getIo()->success(
265 $this->trans('commands.multisite.new.messages.fresh-site'),
274 * Changes permissions of settings.php to 640.
276 * The copy will have 444 permissions by default, which makes it readable by
277 * anyone. Also, Drupal likes being able to write to it during, for example,
280 protected function chmodSettings()
283 $this->fs->chmod($this->appRoot . '/sites/' . $this->directory . '/settings.php', 0640);
284 } catch (IOExceptionInterface $e) {
285 $this->getIo()->error(
287 $this->trans('commands.multisite.new.errors.chmod-fail'),
288 $this->appRoot . '/sites/' . $this->directory . '/settings.php'