Skip to content

Instantly share code, notes, and snippets.

View BenMorel's full-sized avatar
🤝
Open to work. Drop me an email!

Benjamin Morel BenMorel

🤝
Open to work. Drop me an email!
View GitHub Profile
@BenMorel
BenMorel / new-vs-cached-ReflectionClass-instance.php
Last active February 7, 2019 14:05
Benchmarks creating/reading/writing PHP objects using various techniques
<?php
/**
* This script benchmarks creating a new ReflectionClass instance vs using a cached instance.
*
* Results on my machine:
*
* New ReflectionClass instance: 0.270 s
* Cached ReflectionClass instance: 0.147 s
*/
@BenMorel
BenMorel / bench_join.php
Last active January 9, 2019 17:56
(Medium) JOIN vs WHERE IN vs N+1 benchmark
<?php
/** @var PDO $pdo */
$pdo = require __DIR__ . '/common.php';
for ($n = 1; $n <= BENCHMARK_RECORDS; $n++) {
$statement = $pdo->prepare(<<<SQL
SELECT salaries.*, employees.*
FROM salaries INNER JOIN employees ON employees.emp_no = salaries.emp_no
LIMIT $n
@BenMorel
BenMorel / SearchMySQL.php
Created January 24, 2019 18:45
(StackOverflow) Update MediaWiki's SearchMySQL to handle accents (needs the searchindex table to be converted to utf8)
<?php
/**
* MySQL search engine
*
* Copyright (C) 2004 Brion Vibber <brion@pobox.com>
* https://www.mediawiki.org/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
@BenMorel
BenMorel / packages.txt
Created September 23, 2019 20:09
Composer packages survey - true as a type (union types RFC)
drupal/console
drupal/core
google/auth
johnpbloch/wordpress-core
khanamiryan/qrcode-detector-decoder
magento/zendframework1
pear/archive_tar
phing/phing
php-di/php-di
php-http/message
@BenMorel
BenMorel / 1-letsencrypt-setup.sh
Last active September 11, 2021 14:25
Set up letsencrypt certificate with Apache and renew automatically
# Note: this must be executed on the server hosting the domain name!
# Install required packages on RHEL / Fedora:
# EPEL might be required on RHEL: https://fedoraproject.org/wiki/EPEL
sudo yum install -y certbot python3-certbot-apache
# Set up letsencrypt: validates the domain, generates a certificate, and updates Apache config
# Replace example.tld with your domain name
# Add multiple -d if you need to support multiple domains
sudo certbot --apache -d example.tld -d example2.tld
@BenMorel
BenMorel / reset-rds-mysql-replication.txt
Last active October 5, 2019 10:13
Reset RDS MySQL replication
# On slave
# ========
mysql> CALL mysql.rds_stop_replication;
mysql> CALL mysql.rds_reset_external_master;
# Drop & recreate replicated databases
mysql > DROP DATABASE <xxx>;
mysql > CREATE DATABASE <xxx>;
@BenMorel
BenMorel / increase_memory_limit.php
Last active December 28, 2020 23:05
Increases PHP memory limit. Keeps the current setting if the current limit is large enough.
<?php
function _parse_memory_limit(string $value): int
{
if (preg_match('/^([0-9]+)([KMG]?)$/', strtoupper($value), $matches) !== 1) {
throw new RuntimeException('Invalid value for memory_limit: ' . $value);
}
[, $number, $multiplier] = $matches;
@BenMorel
BenMorel / iptables-config-script
Created October 17, 2019 22:57 — forked from LouWii/iptables-config-script
Bash script to configure iptables for a web server. Some rules can be removed depending on used services.
#!/bin/sh
# Empty all rules
sudo iptables -t filter -F
sudo iptables -t filter -X
# Set up default rules
sudo iptables -t filter -P INPUT DROP
sudo iptables -t filter -P FORWARD DROP
sudo iptables -t filter -P OUTPUT ACCEPT
@BenMorel
BenMorel / JsonCanonicalizer.php
Created January 27, 2020 23:45
Simple canonicalization of a JSON string.
<?php
/**
* Simple canonicalization of a JSON string.
*
* - removes formatting
* - sorts object properties
*/
class JsonCanonicalizer
{
@BenMorel
BenMorel / DoctrineCompilerPass.php
Created January 28, 2020 21:11
A Symfony compiler pass to set the default transaction isolation level on Doctrine\DBAL\Connection
<?php
declare(strict_types=1);
namespace App;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\TransactionIsolationLevel;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;