Skip to content

Instantly share code, notes, and snippets.

View devhammed's full-sized avatar
💭
Changing the world, one dollar sign in my PHP code at a time!

Hammed Oyedele devhammed

💭
Changing the world, one dollar sign in my PHP code at a time!
View GitHub Profile
@devhammed
devhammed / fetch.php
Last active May 22, 2025 11:18
PHP Fetch
<?php
if (! class_exists('FetchResponse')) {
class FetchResponse
{
private int $status;
private string $body;
private ?string $error;
private array $headers;
@devhammed
devhammed / async.php
Created May 20, 2025 17:01
Async/Await in PHP
<?php
function async(Closure $task): Closure
{
static $resolved = [];
if ( ! extension_loaded('pcntl') || ! extension_loaded('posix')) {
return $task;
}
@devhammed
devhammed / cloudfare-nginx.sh
Last active March 25, 2025 04:55
Script that automatically generates Cloudfare real-ips configuration for NGINX, you need to add `include /etc/nginx/cloudflare;` to the `http` block in `/etc/nginx/nginx.conf` file to activate.
#!/bin/bash
set -eu
CLOUDFLARE_FILE_PATH=/etc/nginx/cloudflare
echo "# Cloudflare" > $CLOUDFLARE_FILE_PATH;
echo "" >> $CLOUDFLARE_FILE_PATH;
@devhammed
devhammed / cloudfare-ufw.sh
Last active March 25, 2025 04:53
Script to reject TCP connections that are not coming from Cloudfare except if it is SSH.
#!/bin/bash
set -eu
# Get the Cloudflare IPs.
curl -s https://www.cloudflare.com/ips-v4 -o /tmp/cloudflare_ips
echo "" >> /tmp/cloudflare_ips
curl -s https://www.cloudflare.com/ips-v6 >> /tmp/cloudflare_ips
# Reset the firewall to clean stuff.
<?php
/**
* Class Duration
* Represents a time span with microsecond precision, similar to Dart's Duration class.
*/
class Duration
{
/**
* @var int Total duration in microseconds.
@devhammed
devhammed / vite-plugin-sql-css.js
Last active March 13, 2025 09:23
Query SQL databases using CSS.
/**
* Query SQL databases using CSS (mysql, sqlite, postgresql).
*
* Syntax: sql("<dsn>", "<query>", "<jsonpath>")
*
* @example #userName::before {
* content: sql("mysql://user:password@host:port/database", "SELECT * FROM users WHERE id = 1", "$.0.name");
* }
*/
module.export = {
@devhammed
devhammed / yiq.php
Last active February 1, 2025 06:47
Calculating Color Contrast using YIQ
function getContrastYIQ(string $hexColor): string
{
$hex = str_replace('#', '', $hexColor);
$r = hexdec(substr($hex, 0, 2));
$g = hexdec(substr($hex, 2, 2));
$b = hexdec(substr($hex, 4, 2));
@devhammed
devhammed / parse_http_request.php
Last active January 31, 2025 17:19
Parse HTTP Request in PHP
<?php
function parse_http_request(string $request): array
{
[$rawHeaders, $rawBody] = mb_split('\r\n\r\n', $request, 2);
$rawHeaders = mb_split('\r\n', $rawHeaders);
$requestLine = array_shift($rawHeaders);
<?php
namespace App\Listeners;
use Exception;
use Throwable;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Process;
@devhammed
devhammed / Connection.php
Last active November 17, 2024 22:06
Multiple OAuth Connections Support using Laravel Socialite (https://laravel.com/docs/socialite)
<?php
namespace App\Models;
use App\Enums\ConnectionProviderType;
use Illuminate\Database\Eloquent\Model;
use Database\Factories\ConnectionFactory;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;