Skip to content

Instantly share code, notes, and snippets.

@jdecool
Last active August 29, 2015 13:56
Show Gist options
  • Save jdecool/9288375 to your computer and use it in GitHub Desktop.
Save jdecool/9288375 to your computer and use it in GitHub Desktop.
Silex Skeleton installation
#!/usr/bin/env php
<?php
define('ARG_NAME', 1);
define('ARG_VERSION', 2);
define('ARG_DESTINATION', 3);
installSkeleton($argv);
function installSkeleton($argv) {
checkRequirements();
checkOptions($argv);
downloadFiles($argv);
}
function checkRequirements() {
$extensionRequirements = array('zip');
$missing = array();
foreach ($extensionRequirements as $ext) {
if (false === phpversion($ext)) {
$missing[] = $ext;
}
}
if (count($missing) > 0) {
echo sprintf('This extensions are required : %s', implode(', ', $missing)), PHP_EOL;
die;
}
}
function checkOptions($argv) {
if (count($argv) < 4) {
usageHelp($argv);
exit;
}
$version = $argv[ARG_VERSION];
$destination = $argv[ARG_DESTINATION];
if (file_exists($destination) && !is_dir($destination)) {
echo 'Destination is not a folder', PHP_EOL;
exit;
}
if (!file_exists($destination) && !mkdir($destination, 0777, true)) {
echo 'Unable to create destination folder', PHP_EOL;
exit;
}
}
function downloadFiles($argv) {
$version = $argv[ARG_VERSION];
$destination = $argv[ARG_DESTINATION];
$tmpFilename = sprintf('/tmp/skeleton-%s.zip', md5(time()));
$url = sprintf('https://github.com/jdecool/SilexSkeleton/archive/%s.zip', $version);
if (false === copy($url, $tmpFilename)) {
echo sprintf('Unable to download project'), PHP_EOL;
exit;
}
$archive = new ZipArchive();
if (false === ($code = $archive->open($tmpFilename))) {
echo sprintf('Unable to extract skeleton [%s]', $code), PHP_EOL;
exit;
}
if (false === $archive->extractTo($destination)) {
echo sprintf('Unable to extract archive to destination folder'), PHP_EOL;
exit;
}
$archive->close();
}
function usageHelp($argv) {
echo sprintf('%s [name] [version] [destination]', $argv[0]), PHP_EOL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment