Skip to content

Instantly share code, notes, and snippets.

@andreyserdjuk
andreyserdjuk / flatten_exceptions.php
Created January 17, 2022 13:21
flatten parent exceptions
<?php
function a() {
throw new \LogicException('bad logic in A');
}
function b() {
try {
a();
} catch (\LogicException $e) {
@andreyserdjuk
andreyserdjuk / xdebug_toggle.sh
Last active May 22, 2019 19:24
xdebug toggle script
unamestr=`uname`
PHP_INI_PATH=$(php --ini | egrep -o '\S+php.ini$')
if [ -z $(php -m | grep xdebug) ]; then
if [[ "$unamestr" == 'Linux' ]]; then
sudo sed -i "s/^;zend_extension/zend_extension/" $PHP_INI_PATH
elif [[ "$unamestr" == 'Darwin' ]]; then
sudo sed -i "" "s/^;zend_extension/zend_extension/" $PHP_INI_PATH
fi
echo 'Xdebug enabled'
@andreyserdjuk
andreyserdjuk / detect_unicode.php
Created July 28, 2017 10:38
is unicode string?
<?php
$message = 'test';
if ('ASCII' !== mb_detect_encoding($message, 'ASCII', true)) {
echo 'Unicode' . PHP_EOL;
} else {
echo 'ASCII' . PHP_EOL;
}
@andreyserdjuk
andreyserdjuk / cheerio_example.js
Created November 8, 2016 19:36
cheerio parser example
const fetch = require('node-fetch');
const cheerio = require('cheerio');
const fs = require('fs');
let url = "http://loveread.ec/read_book.php?id=56443&p=";
let requests = [];
let startTime = new Date().getTime();
for (let counter = 1; counter < 36; ++counter) {
requests.push(
https://git-scm.com/docs/git-remote
git remote prune --dry-run origin
git remote update origin --prune
# get all removed branches on server side
git branch -vv | grep -Po '\s(feature\S+)(?=.*\s+gone\])'
# so you can remove all "gone" branches:
git branch -D $(b -vv | grep -Po '\s(feature\S+)(?=.*\s+gone\])')
@andreyserdjuk
andreyserdjuk / pcntl_signal_example.php
Created August 16, 2016 20:49
pcntl_signal() handler usage example
<?php
declare(ticks = 1);
pcntl_signal(SIGTERM, 'signalHandler'); // Termination ('kill' was called)
pcntl_signal(SIGHUP, 'signalHandler'); // Terminal log-out
pcntl_signal(SIGINT, 'signalHandler'); // Interrupted (Ctrl-C is pressed)
$seconds = 0;
$total = 10;
@andreyserdjuk
andreyserdjuk / prepare-commit-msg
Last active August 16, 2016 06:43
change commit message on branch example "feature/SPRINT-123" to message: "#SPRINT-123: commit message"
#!/bin/sh
#
# Automatically adds branch name and branch description to every commit message.
# changes commit message on branch example "feature/SPRINT-123" to message: "#SPRINT-123: commit message"
#
# INSTALLATION:
# - copy to .git/hooks/prepare-commit-msg
# - run chmod 555 .git/hooks/prepare-commit-msg
#
NAME=$(git branch | grep '*' | grep -Po '\w+-\d+' | sed -r 's/(.*)/#\1/')
@andreyserdjuk
andreyserdjuk / docker-compose.yml
Created January 2, 2016 20:45
docker-compose example
percona2:
container_name: percona-compose
image: percona:latest
environment:
- MYSQL_ROOT_PASSWORD=12345
php53:
container_name: php53-compose
image: markfletcher/php5.3-zend
links:
@andreyserdjuk
andreyserdjuk / read_file_from_jar.java
Last active September 4, 2015 19:51
read file content from jar
import org.apache.commons.io.IOUtils;
try {
// approach 1
String extractProducts = IOUtils.toString(getClass().getResourceAsStream("/extractProducts.js"));
// approach 2
byte[] bytes = Files.readAllBytes(Paths.get(getClass().getResource("/extractProducts.js").getPath()));
String js_extractProducts = new String(bytes);
} catch (IOException e) {
@andreyserdjuk
andreyserdjuk / json_to_hashmap.java
Last active August 29, 2015 14:20
json to hashmap
/*
jackson-mapper here:
http://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl
*/
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;