Skip to content

Instantly share code, notes, and snippets.

@stefpe
stefpe / glob.php
Created April 15, 2019 19:33
find .h and .c files in a directory
<?php
$files = glob('*.{h,c}', GLOB_BRACE);
if (!$files) {
die('error');
}
foreach ($files as $file) {
printf("%s -> %d bytes\n", $file, filesize($file));
@stefpe
stefpe / cache_key.php
Created April 15, 2019 18:50
func_get_args hash cache key example
<?php
function getProductData(int $productId, string ... $predicates)
{
$args = func_get_args();
$hash = '';
foreach ($args as $arg){
$hash .= (string)($arg);
}
<?php
$a = 'oldValue';
extract(
[
'a' => 'test',
'b' => 'foo'
],
EXTR_SKIP
<?php
function validateMail(string $email): bool
{
$parts = explode("@", $email);
$host = end($parts);
return checkdnsrr($host, 'MX');
}
$email = '[email protected]';
@stefpe
stefpe / switch_php_osx.sh
Created February 28, 2019 14:11
switch php default version with homebrew
brew link --overwrite --force [email protected]
@stefpe
stefpe / db_copy.php
Created February 27, 2019 15:01
Simple script to copy by id from one to another db table
<?php
$con = mysqli_connect("host","user", "password");
mysqli_select_db($con, "dbname");
$stepSize = 100000;
foreach(range(1,50000000, $stepSize) as $step){
$sql = sprintf('insert into tmp_table select * from table where id >= %d and id < %d', $step, $step + $stepSize);
mysqli_query($con, $sql);
@stefpe
stefpe / feed_es.sh
Created February 13, 2019 12:44
Feed elasticsearch with curl bulk request
curl -XPOST http://localhost:9200/_bulk?pretty --data-binary @/tmp/all.txt -H 'Content-Type: application/json'
@stefpe
stefpe / queue_threading.py
Last active February 13, 2019 09:57
queue based threadings
import queue
import threading
NUM_THREADS = 10
def worker(channel):
while True:
value = channel.get()
if value is False:
break
@stefpe
stefpe / build.php
Created January 24, 2019 13:17
phar build file
<?php
$dir = dirname(__FILE__);
$exclude = [
'bin',
'build',
'data',
'docs',
'docs-api',
@stefpe
stefpe / init.php
Created January 24, 2019 13:11
robo as a framework
<?php
const APP_NAME = "MyRunner";
const APP_VERSION = '0.0.1';
// If we're running from phar load the phar autoload file.
$pharPath = \Phar::running(true);
if ($pharPath) {
$autoloaderPath = "$pharPath/vendor/autoload.php";
} else {