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 / generate-jit-php-ini.php
Last active January 15, 2023 00:03
Generates a php.ini file compatible with PHP JIT compiler
<?php
/**
* Generates a php.ini file from runtime ini directives, excluding extensions incompatible with JIT.
* I did not find a way to disable an extension from the command line, so this script is a workaround.
*
* Use it this way:
*
* php generate-jit-php-ini.php > php.ini
* php -n -c php.ini
@BenMorel
BenMorel / stream.php
Last active September 7, 2022 13:46
Stream a file that is being written to by another process
<?php
/**
* This script streams a file that is being written to by another process.
* It will continue fread()ing past EOF until explicitly stopped with CTRL+C.
*
* The script will exit with a status code of 0 if and only if the stream is stopped *after* EOF has been reached,
* i.e. if actual EOF *MAY* have been reached.
*
* The output of the streamed file is sent to STDOUT, and the output of the script is sent to STDERR,
@BenMorel
BenMorel / symfony-deserialize-json-to-dto.php
Created January 2, 2022 22:50
Symfony Serializer: deserializing JSON to DTO example
<?php
require 'vendor/autoload.php';
use Symfony\Component\PropertyInfo\Extractor\ConstructorExtractor;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
@BenMorel
BenMorel / SafeCast.php
Last active October 8, 2024 16:52
Safe casting to int (WIP)
<?php
final class SafeCast
{
/**
* Converts the given variable to an integer.
*
* Allowed types are int, float and string.
*
* - int values are returned as is;
{
"require": {
"doctrine/orm": "^2.8",
"brick/geo": "^0.4.0"
}
}
@BenMorel
BenMorel / generate-Psalm-CallMap.php
Last active December 28, 2020 21:42
Generates entries for Psalm's CallMap from Reflection
<?php
// Configure here the classes to export. Example:
$classes = [
'GEOSGeometry',
'GEOSWKBReader',
'GEOSWKBWriter',
'GEOSWKTReader',
'GEOSWKTWriter'
];
@BenMorel
BenMorel / tokenize-php-script.php
Last active December 9, 2020 14:01
Tokenizes a PHP script with token_get_all(), and outputs a readable representation.
<?php
/**
* Tokenizes a PHP script with token_get_all(), and outputs a readable representation.
*
* Usage: tokenize-php-script.php FILE.php
*
* Sample output:
*
* 1 T_OPEN_TAG <?php\n
@BenMorel
BenMorel / getDominantColor.php
Last active December 3, 2020 14:48
Get dominant color from an Imagick image
<?php
/**
* Two methods to get the dominant color from an image.
* Useful to create placeholders while loading an image.
*
* Method 1 yields more bright colors than method 2.
*/
/**
@BenMorel
BenMorel / errorLevelToConstants.php
Last active March 25, 2022 09:45
Returns constants matching an error level
<?php
/**
* Examples:
*
* errorLevelToConstants(E_WARNING) => ['E_WARNING', 'E_ALL']
* errorLevelToConstants(3) => ['E_ERROR', 'E_WARNING', 'E_ALL']
*
* @return string[]
*/
@BenMorel
BenMorel / ca.crt
Created November 27, 2020 09:40
My public dev certificate
-----BEGIN CERTIFICATE-----
MIIDdTCCAl2gAwIBAgIUN09wV0sbUhrzQ9WonV0xOabJqJ8wDQYJKoZIhvcNAQEL
BQAwSjELMAkGA1UEBhMCVVMxGDAWBgNVBAoMD19EZXZlbG9wbWVudCBDQTEhMB8G
A1UEAwwYRGV2ZWxvcG1lbnQgY2VydGlmaWNhdGVzMB4XDTIwMTAxNDE2MTA0NFoX
DTMwMTAxMjE2MTA0NFowSjELMAkGA1UEBhMCVVMxGDAWBgNVBAoMD19EZXZlbG9w
bWVudCBDQTEhMB8GA1UEAwwYRGV2ZWxvcG1lbnQgY2VydGlmaWNhdGVzMIIBIjAN
BgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzpZt/KQzyM+eJW336oeZFb+wqrBQ
gcTPttoRIbaVKvxsGUiXcf1RaYrn9KLfNn3J6HGv6s1gO/sPooNch53dq8pkqPtZ
VQUn66A/0FIMvkBd0Y/zTb5n4eTFwJcrn1xhYiBEODkkr+4c6YAAzDGg2ngY6gay
0womr6C72NDOFQCXVewr+KkXi6DWqIF1cE4hBZ0i97ASMCkWGL/3+cotzmbdBkE1