Created
April 5, 2024 17:21
-
-
Save cornelisonc/6e51d6d945e4f5e252152c04995c1c80 to your computer and use it in GitHub Desktop.
maybeAddCustomCommands() deployer example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace DeployerCommands; | |
use Deployer\Command\InitCommand as ParentInit; | |
use Symfony\Component\Console\Command\Command; | |
use Symfony\Component\Console\Input\InputInterface; | |
use Symfony\Component\Console\Input\InputOption; | |
use Symfony\Component\Console\Output\OutputInterface; | |
use Symfony\Component\Console\Style\SymfonyStyle; | |
use Symfony\Component\Process\Exception\RuntimeException; | |
use Symfony\Component\Process\PhpProcess; | |
use Symfony\Component\Process\Process as Process; | |
class InitCommand extends ParentInit | |
{ | |
// Example: Custom class var | |
private $domain; | |
// Example: Customize command description | |
protected function configure(): void | |
{ | |
$this | |
->setName('init') | |
->setDescription('Custom initialization of deployer in your project') | |
->addOption('path', 'p', InputOption::VALUE_OPTIONAL, 'Recipe path'); | |
} | |
// Example: Adding new setDomain() question to dep init | |
protected function setTemplateVars(InputInterface $input, OutputInterface $output): void | |
{ | |
$io = new SymfonyStyle($input, $output); | |
$this->setRecipePath($input, $io); | |
$this->setTemplate($io); | |
$this->setRepository($io); | |
$this->setProject($io); | |
$this->setDomain($io); | |
$this->guessHost(); | |
$this->setHosts($io); | |
} | |
// Example: Query for domain in init process | |
private function setDomain($io): void | |
{ | |
$default = ''; | |
try { | |
$process = Process::fromShellCommandline('basename "$PWD"'); | |
$default = $process->mustRun()->getOutput(); | |
$default = trim(rtrim($default, '.com')) . '.com'; | |
} catch (RuntimeException $e) { | |
//@TODO Throw error $e->getMessage(); | |
} | |
$this->domain = $io->ask('Domain', $default); | |
} | |
// Example: overridden guessHost() based on new setDomain() | |
// Assumes that in our custom hosting stack, there will be | |
// a file called 'host.php' which will return the site's | |
// hostname; if not, guess failed. | |
protected function guessHost(): void | |
{ | |
if ($this->domain) { | |
$this->tempHostFile = tempnam(sys_get_temp_dir(), 'temp-host-file'); | |
$ch = curl_init("https://$this->domain/host.php"); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_TIMEOUT, 5); | |
$result = curl_exec($ch); | |
curl_close($ch); | |
if (filter_var($result, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME)) { | |
file_put_contents($this->tempHostFile, $result); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment