|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Pantheon\TerminusFiler\Commands; |
| 4 | + |
| 5 | +use Pantheon\Terminus\Collections\Domains; |
| 6 | +use Pantheon\Terminus\Collections\Environments; |
| 7 | +use Pantheon\Terminus\Commands\TerminusCommand; |
| 8 | +use Pantheon\Terminus\Models\Environment; |
| 9 | +use Pantheon\Terminus\Models\Site; |
| 10 | +use Pantheon\Terminus\Site\SiteAwareInterface; |
| 11 | +use Pantheon\Terminus\Site\SiteAwareTrait; |
| 12 | +use Pantheon\Terminus\Exceptions\TerminusProcessException; |
| 13 | + |
| 14 | +/** |
| 15 | + * Class StoogesCommand |
| 16 | + * Searches for and destroys (replaces) all instances of a pantheonsite.io domain. |
| 17 | + */ |
| 18 | +class StoogesCommand extends TerminusCommand implements SiteAwareInterface |
| 19 | +{ |
| 20 | + |
| 21 | + use SiteAwareTrait; |
| 22 | + |
| 23 | + /** |
| 24 | + * Replaces platform and vanity domains with the chosen custom domain. |
| 25 | + * |
| 26 | + * @authorize |
| 27 | + * |
| 28 | + * @command site:stooges |
| 29 | + * @aliases sar |
| 30 | + * @aliases search-and-replace |
| 31 | + * @aliases search-replace |
| 32 | + * @aliases search-and-destroy |
| 33 | + * @aliases stooges |
| 34 | + * |
| 35 | + * @param string $site_env Site & environment in the format `site-name.env` |
| 36 | + * |
| 37 | + * @usage terminus site:stooges <site>.<env> |
| 38 | + */ |
| 39 | + public function stooges($site_env) { |
| 40 | + |
| 41 | + /** @var Environment $env */ |
| 42 | + /** @var Site $site */ |
| 43 | + list($site, $env) = $this->getSiteEnv($site_env); |
| 44 | + if ($site->get('framework') != 'wordpress') { |
| 45 | + $this->logger->notice('Refusing to act on site as it is not WordPress.'); |
| 46 | + return; |
| 47 | + } |
| 48 | + /** @var Domains $domains */ |
| 49 | + $domains = $env->getDomains()->serialize(); |
| 50 | + |
| 51 | + $replaceable_domains = []; |
| 52 | + $target_domains = []; |
| 53 | + foreach ($domains as $domain => $data) { |
| 54 | + if ('custom' == $data['type']) { |
| 55 | + $target_domains[] = $domain; |
| 56 | + } |
| 57 | + else { |
| 58 | + $replaceable_domains[] = $domain; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (count($target_domains) < 1) { |
| 63 | + $this->logger->notice('Refusing to act on site as it has no custom domains available.'); |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + $target_domain = $this->io()->choice('What domain would you like to use as the replacement domain?', $target_domains); |
| 68 | + |
| 69 | + $echoOutputFn = function ($type, $buffer) { |
| 70 | + }; |
| 71 | + |
| 72 | + foreach ($replaceable_domains as $replaceable_domain) { |
| 73 | + $command_line = 'wp search-replace ' . $replaceable_domain . ' ' . $target_domain; |
| 74 | + $this->io()->text('Replacing domain ' . $replaceable_domain . ' with ' . $target_domain); |
| 75 | + $result = $env->sendCommandViaSsh($command_line, $echoOutputFn, false); |
| 76 | + $output = $result['output']; |
| 77 | + $exit = $result['exit_code']; |
| 78 | + |
| 79 | + if ($exit != 0) { |
| 80 | + throw new TerminusProcessException($output); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Replaces platform and vanity domains from all environments with the chosen |
| 88 | + * custom domain. |
| 89 | + * |
| 90 | + * @authorize |
| 91 | + * |
| 92 | + * @command site:stooges:sparky |
| 93 | + * @aliases sparky |
| 94 | + * |
| 95 | + * @param string $site_id Site in the format `site-name` |
| 96 | + * |
| 97 | + * @usage terminus site:stooges:sparky <site> |
| 98 | + */ |
| 99 | + public function sparky($site_id) { |
| 100 | + /** @var Site $site */ |
| 101 | + $site = $this->getSite($site_id); |
| 102 | + |
| 103 | + if ($site->get('framework') != 'wordpress') { |
| 104 | + $this->logger->notice('Refusing to act on site as it is not WordPress.'); |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + /** @var Environments $environments */ |
| 109 | + $environments = $site->getEnvironments()->fetch(); |
| 110 | + $environment_ids = $environments->ids(); |
| 111 | + /** @var Environment $live_environment */ |
| 112 | + $live_environment = $environments->get('live'); |
| 113 | + $replaceable_domains = []; |
| 114 | + $target_domains = []; |
| 115 | + foreach ($environment_ids as $environment_id) { |
| 116 | + /** @var Environment $environment */ |
| 117 | + $environment = $environments->get($environment_id); |
| 118 | + /** @var Domains $domains */ |
| 119 | + $domains = $environment->getDomains()->serialize(); |
| 120 | + foreach ($domains as $domain => $data) { |
| 121 | + if ('custom' == $data['type']) { |
| 122 | + $target_domains[] = $domain; |
| 123 | + } |
| 124 | + else { |
| 125 | + $replaceable_domains[] = $domain; |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + if (count($target_domains) == 1) { |
| 131 | + $target_domain = array_pop($target_domains); |
| 132 | + } |
| 133 | + elseif (count($target_domains) == 0) { |
| 134 | + $this->logger->notice('Refusing to act on site as their are no custom domains.'); |
| 135 | + return; |
| 136 | + } |
| 137 | + else { |
| 138 | + $target_domain = $this->io()->choice('What domain would you like to use as the replacement domain?', $target_domains); |
| 139 | + } |
| 140 | + |
| 141 | + $echoOutputFn = function ($type, $buffer) { |
| 142 | + }; |
| 143 | + |
| 144 | + foreach ($replaceable_domains as $replaceable_domain) { |
| 145 | + $command_line = 'wp search-replace ' . $replaceable_domain . ' ' . $target_domain; |
| 146 | + $this->io()->text('Replacing domain ' . $replaceable_domain . ' with ' . $target_domain); |
| 147 | + $result = $live_environment->sendCommandViaSsh($command_line, $echoOutputFn, false); |
| 148 | + $output = $result['output']; |
| 149 | + $exit = $result['exit_code']; |
| 150 | + |
| 151 | + if ($exit != 0) { |
| 152 | + throw new TerminusProcessException($output); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments