|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @file |
| 5 | + * Contains \DennisDigital\Drupal\Console\Command\SiteSettingsLocalCommand. |
| 6 | + * |
| 7 | + * Creates Local configurations. |
| 8 | + */ |
| 9 | + |
| 10 | +namespace DennisDigital\Drupal\Console\Command; |
| 11 | + |
| 12 | +use Symfony\Component\Console\Input\InputOption; |
| 13 | +use Symfony\Component\Console\Input\InputInterface; |
| 14 | +use Symfony\Component\Console\Output\OutputInterface; |
| 15 | +use DennisDigital\Drupal\Console\Exception\SiteCommandException; |
| 16 | + |
| 17 | +/** |
| 18 | + * Class SiteSettingsLocalCommand |
| 19 | + * |
| 20 | + * @package DennisDigital\Drupal\Console\Command |
| 21 | + */ |
| 22 | +class SiteSettingsLocalCommand extends SiteBaseCommand { |
| 23 | + |
| 24 | + /** |
| 25 | + * The file name to generate. |
| 26 | + * |
| 27 | + * @var |
| 28 | + */ |
| 29 | + protected $filename = 'settings.local.php'; |
| 30 | + |
| 31 | + /** |
| 32 | + * {@inheritdoc} |
| 33 | + */ |
| 34 | + protected function configure() { |
| 35 | + parent::configure(); |
| 36 | + |
| 37 | + $this->setName('site:settings:local') |
| 38 | + // @todo use: ->setDescription($this->trans('commands.site.settings.local.description')) |
| 39 | + ->setDescription('Generates settings.local.php for a given site.'); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + protected function interact(InputInterface $input, OutputInterface $output) { |
| 46 | + parent::interact($input, $output); |
| 47 | + |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * {@inheritdoc} |
| 52 | + */ |
| 53 | + protected function execute(InputInterface $input, OutputInterface $output) { |
| 54 | + parent::execute($input, $output); |
| 55 | + |
| 56 | + $this->destination = $this->settingsPhpDirectory(); |
| 57 | + |
| 58 | + // Validation. |
| 59 | + if (!$this->fileExists($this->destination . '../example.' . $this->filename)) { |
| 60 | + $message = sprintf('The file example.settings.local.php is missing.', |
| 61 | + $this->destination |
| 62 | + ); |
| 63 | + throw new SiteCommandException($message); |
| 64 | + } |
| 65 | + |
| 66 | + // Remove existing file. |
| 67 | + $file = $this->destination . $this->filename; |
| 68 | + if ($this->fileExists($file)) { |
| 69 | + $this->fileUnlink($file); |
| 70 | + } |
| 71 | + |
| 72 | + // Copy example. |
| 73 | + $command = sprintf('cd %s && cp -n ../example.%s %s', |
| 74 | + $this->shellPath($this->destination), |
| 75 | + $this->filename, |
| 76 | + $this->filename |
| 77 | + ); |
| 78 | + $shellProcess = $this->getShellProcess(); |
| 79 | + if (!$shellProcess->exec($command, TRUE)) { |
| 80 | + throw new SiteCommandException(sprintf('Error generating %s', |
| 81 | + $this->filename |
| 82 | + ) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + // Load the file. |
| 87 | + $content = $this->fileGetContents($file); |
| 88 | + |
| 89 | + $host= $this->config['host']; |
| 90 | + $cdn = $this->config['cdn']; |
| 91 | + |
| 92 | + // Append configuration. |
| 93 | + $content .= <<<EOF |
| 94 | +
|
| 95 | +// Set Stage file proxy origin. |
| 96 | +\$config['stage_file_proxy.settings']['origin'] = '$cdn'; |
| 97 | +
|
| 98 | +// Change CDN domain to local. |
| 99 | +\$config['cdn.settings']['mapping']['domain'] = '$host'; |
| 100 | +
|
| 101 | +EOF; |
| 102 | + |
| 103 | + $this->filePutContents($file, $content); |
| 104 | + |
| 105 | + // Check file. |
| 106 | + if ($this->fileExists($file)) { |
| 107 | + $this->io->success(sprintf('Generated %s', |
| 108 | + $file) |
| 109 | + ); |
| 110 | + } |
| 111 | + else { |
| 112 | + throw new SiteCommandException(sprintf('Error generating %s', |
| 113 | + $file |
| 114 | + ) |
| 115 | + ); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments