Skip to content

Instantly share code, notes, and snippets.

@phcorp
phcorp / Velocity format CamelCase into normal string
Last active November 9, 2016 13:12
Translating CamelCase into human readable string in PHPStorm
## Format CamelCase to text
## fooBar => Foo bar
## fooBAR => Foo bar
## References:
## https://velocity.apache.org/engine/devel/user-guide.html
## https://www.jetbrains.com/help/phpstorm/2016.1/template-languages-velocity-and-freemarker.html
#set ($formattedName = $NAME)
#set ($formattedName = $formattedName.replaceAll('([A-Z]+)', ' $1'))
#set ($formattedName = $formattedName.toLowerCase())
@phcorp
phcorp / prestashop-1.6-deploy-database.sh
Last active December 22, 2016 18:35
Prestashop database synchronization from local to remote staging server.
#!/usr/bin/env bash
ssh_address="example@domain.com";
dump_filename="dump.sql";
site_remote_path="/home/example";
site_remote_domain="staging.example.com"
local_db_host=$(php -r 'include("config/settings.inc.php");echo _DB_SERVER_;');
local_db_name=$(php -r 'include("config/settings.inc.php");echo _DB_NAME_;');
local_db_user=$(php -r 'include("config/settings.inc.php");echo _DB_USER_;');
@phcorp
phcorp / pre-commit
Last active June 17, 2017 00:08
php-cs-fixer pre-commit hook
#!/bin/sh
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"
PHP_CS_FIXER="bin/php-cs-fixer"
if [ -x $PHP_CS_FIXER ]; then
git status --porcelain | grep -e '^ [AM] \(src\/.*\)\.php' | cut -c 4- | while read line; do
$PHP_CS_FIXER fix "$line" --level=symfony --verbose;
git add "$line";
done
@phcorp
phcorp / curl_oauth.sh
Created March 21, 2017 14:36
Bash oauth with curl on OSX
#!/usr/bin/env bash
# parameters
url="https://example.com"
client_id="foo"
client_secret="bar"
# request access token
oauth_path="/oauth2/access_token"
access_token=$(curl -s -H "Accept: application/json" -H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" -H "Authorization: Basic $(echo "$client_id:$client_secret" | tr -d "\n" | base64)" -X POST -d "grant_type=client_credentials" "$url$oauth_path" | sed -e 's/^.*"access_token":"\([^"]*\)".*$/\1/')
@phcorp
phcorp / api_functions.php
Created April 27, 2017 13:52
API functions
<?php
/**
* @param string $path prepended with a slash
* @param string $data to send
* @param string $method default POST
* @param string $header to send
*
* @return string response
*/
@phcorp
phcorp / doctrine-migrations-checkout.sh
Last active June 17, 2017 00:11
Scripts that reverts entities changes to the state they were during migrations definitions
#!/bin/bash
safeRun() {
typeset cmnd="$*"
typeset ret_code
echo cmnd=$cmnd
eval $cmnd
ret_code=$?
if [ $ret_code != 0 ]; then
printf "Error : [%d] when executing command: '$cmnd'" $ret_code
@phcorp
phcorp / form-data-to-object.js
Created August 10, 2017 15:45
Vanilla Javascript FormData to object
/*
* Converts FormData to object.
*
* @param {FormData} data
* To convert.
* @return {string}
* FormData converted to object.
*/
function formDataToObject(data) {
let value;
@phcorp
phcorp / backup-clean.sh
Last active February 3, 2024 13:18
Backup files cleaning script
#!/bin/bash
##############################################################################
# This script is meant to be run once a day.
#
# Backup files name format must be: database-%Y-%m-%dT%H:%M.bz2
#
# It deletes backup files that does not match one of these conditions:
# - backup every 15 minutes during one week
# - backup every day during 3 months
@phcorp
phcorp / apple-touch-startup-image-polyfill.es6.js
Last active December 8, 2017 22:59
Polyfill for apple-touch-startup-image
/* global window, Image */
/**
* Class AppleTouchStartupImage.
*
* Insert a script in the html head at the 1st position containing:
* const startupImage = new AppleTouchStartupImage();
* startupImage.install();
*
* @prop {string} image - source
@phcorp
phcorp / replace-in-path.php
Created March 16, 2018 15:48
PHP replace in path
<?php
$root = './src';
$find = 'foo';
$replace = 'bar';
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));
$count = 0;
foreach ($iterator as $file) {
if ($file->isDir() || 'php' !== $file->getExtension() || '.' === $file->getFilename()[0]) {
continue;