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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / doctrine-dbal-travis-matrix-builder.php
Last active October 20, 2020 18:20
Builds the Travis CI job matrix for Doctrine DBAL - See doctrine/dbal#3347
<?php
/**
* This script builds the .travis.yml build matrix, around these variables:
*
* - PHP versions
* - database drivers
* - platform versions
*
* With the following requirements:
@BenMorel
BenMorel / remove-mysql-8-root-password.sh
Created November 9, 2018 13:24
(Medium) Remove MySQL root password *DO NOT USE THIS ON A PRODUCTION SERVER*
password=$(grep -oP 'temporary password(.*): \K(\S+)' /var/log/mysqld.log)
mysqladmin --user=root --password="$password" password aaBB@@cc1122
mysql --user=root --password=aaBB@@cc1122 -e "UNINSTALL COMPONENT 'file://component_validate_password';"
mysqladmin --user=root --password="aaBB@@cc1122" password ""
@BenMorel
BenMorel / Font-Awesome-4-to-5-Migration.php
Last active November 15, 2018 13:23
Automatically migrates Font Awesome 4 class names to Font Awesome 5 class names
<?php
/**
* Automatically migrates Font Awesome 4 class names to Font Awesome 5 class names.
*
* WARNING: THIS WILL REWRITE YOUR FILES.
* BACK UP YOUR FILES BEFORE RUNNING THIS SCRIPT.
* YOU HAVE BEEN WARNED.
*
* Note: the migration CSV file was parsed from the official migration documentation: