-
-
Save totten/9ad0c41ef1d5e53fa9ec to your computer and use it in GitHub Desktop.
PSEUDOCODE: Deploy a new composer.json document
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 | |
/** | |
* @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