|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This command will clone the code from any available Pantheon site environment to your local system. |
| 5 | + * |
| 6 | + * See README.md for usage information. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace TerminusPluginProject\TerminusCode\Commands; |
| 10 | + |
| 11 | +use Pantheon\Terminus\Commands\Site\SiteCommand; |
| 12 | +use Pantheon\Terminus\Exceptions\TerminusNotFoundException; |
| 13 | + |
| 14 | +class CodeCommand extends SiteCommand |
| 15 | +{ |
| 16 | + /** |
| 17 | + * Clones the code from any available site environment. |
| 18 | + * |
| 19 | + * @authorize |
| 20 | + * |
| 21 | + * @command site:env:code |
| 22 | + * @aliases env:code code |
| 23 | + * |
| 24 | + * @usage terminus site:env:code | env:code | code <site>.<env> |
| 25 | + * Clone the code of <site>.<env> locally. Example: terminus code my-site.dev. |
| 26 | + */ |
| 27 | + public function code($site_env = false) |
| 28 | + { |
| 29 | + |
| 30 | + if (!$site_env) { |
| 31 | + throw new TerminusNotFoundException('Usage: terminus site:env:code | env:code | code <site>.<env>'); |
| 32 | + } |
| 33 | + |
| 34 | + $this->sites()->fetch( |
| 35 | + [ |
| 36 | + 'org_id' => null, |
| 37 | + 'team_only' => false, |
| 38 | + ] |
| 39 | + ); |
| 40 | + |
| 41 | + $sites = $this->sites->serialize(); |
| 42 | + |
| 43 | + if (empty($sites)) { |
| 44 | + throw new TerminusNotFoundException('You have no sites.'); |
| 45 | + } |
| 46 | + |
| 47 | + // Validate if site.env exists. |
| 48 | + $site_env_exists = false; |
| 49 | + $site_env_frozen = false; |
| 50 | + foreach ($sites as $site) { |
| 51 | + if ($environments = $this->getSite($site['name'])->getEnvironments()->serialize()) { |
| 52 | + foreach ($environments as $environment) { |
| 53 | + if ($environment['initialized'] == 'true') { |
| 54 | + $site_environment = $site['name'] . '.' . $environment['id']; |
| 55 | + if ($site_env == $site_environment) { |
| 56 | + $site_env_exists = true; |
| 57 | + $site_env_frozen = ($site['frozen'] == 'true'); |
| 58 | + break; |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + if ($site_env_exists) { |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (!$site_env_exists) { |
| 69 | + $message = 'Site environment {site_env} does not exist.'; |
| 70 | + throw new TerminusNotFoundException($message, ['site_env' => $site_env]); |
| 71 | + } |
| 72 | + |
| 73 | + if ($site_env_frozen) { |
| 74 | + $message = 'Site environment {site_env} is frozen.'; |
| 75 | + throw new TerminusNotFoundException($message, ['site_env' => $site_env]); |
| 76 | + } |
| 77 | + |
| 78 | + $command = '$(terminus connection:info ' . $site_env . ' --field=git_command)'; |
| 79 | + $this->execute($command); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Executes the command. |
| 84 | + */ |
| 85 | + protected function execute($cmd) |
| 86 | + { |
| 87 | + $process = proc_open( |
| 88 | + $cmd, |
| 89 | + [ |
| 90 | + 0 => STDIN, |
| 91 | + 1 => STDOUT, |
| 92 | + 2 => STDERR, |
| 93 | + ], |
| 94 | + $pipes |
| 95 | + ); |
| 96 | + proc_close($process); |
| 97 | + } |
| 98 | +} |
0 commit comments