Skip to content

Instantly share code, notes, and snippets.

@totten

totten/ex.php Secret

Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totten/9ad0c41ef1d5e53fa9ec to your computer and use it in GitHub Desktop.
Save totten/9ad0c41ef1d5e53fa9ec to your computer and use it in GitHub Desktop.
PSEUDOCODE: Deploy a new composer.json document
<?php
/**
* @param string $projectPath
* The basedir containing "composer.json" and "vendor".
* @param array $composerDoc
* Composer specification (per composer.json).
*/
function deploy($projectPath, $composerDoc) {
// Prepare an upgrade script
$tmpPrefix = get_sys_tmpdir() . '/composer-deploy-' . md5(openssl_random_pseudo_bytes(32));
$tmpJson = "{$tmpPrefix}.json";
file_put_contents($tmpJson, json_encode($composer));
$tmpScript = "{$tmpPrefix}.sh";
file_put_contents($tmpScript, "
cd $projectPath
mv composer.json composer.json.bak
cp $tmpJson composer.json
if ! composer install ; then
mv composer.json.bak composer.json
exit 1
fi
");
// Execute the script, or ask the user to execute it.
// Pseudocode uses switch() and echo() for brevity. Real code would be very different.
switch (pickDeploymentStrategy()) {
case 'exec':
// Simple strategy. Great for local/single-user dev.
exec($tmpScript, $output, $return);
if ($return) echo "Error: <pre>$output</pre>"
break;
case 'ssh':
// You'll need to either store a password or ask for a password.
$ssh = new Net_SSH(...);
$ssh->exec($tmpScript);
if (...error...) echo "Error: ...";
case 'prompt':
default:
echo "Please open the CLI and run <tt>$tmpScript</tt>."
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment