Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env bash
VERBOSE=1
IPS_FILE="/tmp/elb_ips"
BACKEND_VCL="/etc/varnish/backend.vcl"
# Does not keep newlines, but it's ok
read -r -d '' BACKEND_TEMPLATE <<'EOF'
backend internal_elb_{ELB_NUM} {
.host = "{ELB_IP}";
.port = "80";
@borisguery
borisguery / upload_parameters.rb
Created July 9, 2013 09:58
Upload parameters according to the current stage in capistrano
task :upload_parameters do
desc 'Upload stage parameters'
capifony_pretty_print '--> Uploading stage parameters'
origin_file = "app/config/parameters.#{fetch(:stage)}.yml"
destination_file = deploy_to + '/' + shared_dir + '/app/config/parameters.yml'
run "sh -c 'if [ ! -d #{File.dirname(destination_file)} ] ; then mkdir -p #{File.dirname(destination_file)}; fi'"
top.upload(origin_file, destination_file)
@borisguery
borisguery / gist:5908055
Last active December 19, 2015 05:59 — forked from armetiz/gist:5908044
.myTransform(@argument : none) {
-webkit-transform: ~"@argument"(10px);
-moz-transform: ~"-moz-@{argument}"(10px);
etc..
}
@borisguery
borisguery / formatbytes.php
Created December 20, 2012 14:16
format bytes to the best fitted unit
#!/usr/bin/env php
<?php
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
@borisguery
borisguery / utf8urldecode.php
Created October 2, 2012 11:07
utf8 url decode/encode
<?php
function utf8_urldecode($str) {
$str = preg_replace("/%u([0-9a-f]{3,4})/i","&#x\\1;",urldecode($str));
return html_entity_decode($str,null,'UTF-8');;
}
// Main
$options = getopt('de');
@borisguery
borisguery / guid-benchmark.php
Created September 28, 2012 09:01
GUID generation benchmark
<?php
$options = getopt('i:c:');
if (!isset($options['i']) && !isset($options['c'])) {
printf("Usage: %s -i iterations -c count\n", ltrim($argv[0], './'));
exit(0);
}
$iterations = $options['i'];
@borisguery
borisguery / gist:3795209
Created September 27, 2012 17:14
PHP byteArray?
<?php
/** @link http://madskristensen.net/post/A-shorter-and-URL-friendly-GUID.aspx */
function fromBase64($input) {
$segments = unpack('C*', base64_decode(str_replace(array('_', '-'), array('/', '+'), $input)));
array_walk($segments, function(&$segment, $key) {
$segment = dechex($segment);
});
@borisguery
borisguery / directorySplit.php
Created September 25, 2012 15:28
Split a directory into sub-directories
<?php
function directorySplit($dir, $segmentSize = 2, $maxSegment = 3, $dirSeparator = DIRECTORY_SEPARATOR)
{
$segmentedDir = '';
for ($i = 0, $segmentOffset = 0; $i < $maxSegment && strlen($dir) > $segmentOffset; ++$i, $segmentOffset = $segmentSize * $i) {
$segment = substr($dir, $segmentOffset, $segmentSize) . $dirSeparator;
if (strlen($segment) < $segmentSize) {
break;
}
@borisguery
borisguery / version2integer.php
Created September 21, 2012 08:45
Version to Integer
<?php
function version2integer($version) {
$matches = array();
preg_match('/([0-9.]+)/', $version, $matches);
if (count($matches) > 1) {
$maxSequences = 4; // v12.34.56.78.90 will stop at 78
$matches = explode('.', $matches[1]);
$matches = $matches + array(0, 0, 0, 0);
@borisguery
borisguery / gist:3134871
Created July 18, 2012 07:43
SplFixedArray vs Native Array lookup performance
<?php
ini_set('memory_limit', -1);
$count = (int)$argv[1];
function fixed_array_search_foreach($stack, $counter, $lookup, $type) {
$start = microtime(true);
foreach ($stack as $key => $value) {
if ($lookup === $value) {