Skip to content

Instantly share code, notes, and snippets.

View aklump's full-sized avatar

Aaron Klump aklump

View GitHub Profile
@aklump
aklump / Upfind.php
Last active November 8, 2025 17:21
Search for a path moving up the file tree, e.g. "locate .git from child path"
<?php
/**
* The Upfind class provides functionality to locate a file or directory
* by walking up the directory tree from a starting directory.
*/
class Upfind {
public function __invoke(string $basename): string {
$result = $this->upfind($basename);
@aklump
aklump / is_windows.php
Created August 22, 2025 19:47
Detect if Windows
(defined('PHP_OS_FAMILY') && PHP_OS_FAMILY === 'Windows')
|| strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
@aklump
aklump / drupal_user_timezone.php
Created July 2, 2025 15:05
Get timezone name per current user.
<?php
if (!$account instanceof \Drupal\Core\Session\AccountInterface || $account->isAnonymous()) {
$timezone_name = \Drupal::config('system.date')->get('timezone.default');
}
else {
$timezone_name = $account->getTimeZone();
}
@aklump
aklump / GetUrlTemplateByRouteName.php
Created May 30, 2025 00:00
Generate a URL template from a Drupal route name (with optional/partial parameter replacement).
<?php
namespace Drupal\commons_core\Helper;
use http\Exception\InvalidArgumentException;
class GetUrlTemplateByRouteName {
/**
* Generate a URL template from a route.
@aklump
aklump / vendor_autoload.php
Last active November 8, 2025 17:07
snippet to load composer autoload
// https://getcomposer.org/doc/articles/vendor-binaries.md#finding-the-composer-autoloader-from-a-binary
if (isset($GLOBALS['_composer_autoload_path'])) {
// As of Composer 2.2...
$_composer_autoload_path = $GLOBALS['_composer_autoload_path'];
}
else {
// < Composer 2.2
$ds = DIRECTORY_SEPARATOR;
foreach ([
__DIR__ . $ds . '..' . $ds . '..' . $ds . 'autoload.php',
@aklump
aklump / todo.md
Created March 5, 2025 20:08
Task list scaffolding.

Critical

Normal

Complete

@aklump
aklump / xdebug_controller.sh
Last active February 18, 2025 00:07
Xdebug Lando Controller
#!/usr/bin/env bash
#
# @file
# Xdebug Lando Controller (Supports Xdebug versions 2 and 3)
#
# Save this file to: ./install/lando/xdebug_controller.sh.
#
# This script is meant to be used for Lando tooling to control Xdebug. When
# using with version 2, use the version 3 syntax and it will be properly
@aklump
aklump / GetShortPath.php
Last active November 8, 2025 18:16
Create a pretty/short path, removing CWD or basepath.
<?php
/**
* @code
* // Print a shortened, nice-to-read path when possible:
* echo (new GetShortPath('/some/base/path')($long_path)
*
* // Or relative to the CWD:
* echo (new GetShortPath()($long_path)
* @endcode
@aklump
aklump / RotateImageDataURI.php
Last active September 16, 2024 21:36
Invokable class to rotate an image data URI
<?php
use InvalidArgumentException;
/**
* Rotate an image DataURI string.
*
* Because I'm working with a string, not a file, the Drupal image API
* (image.factory) didn't seem to be an appropriate solution, therefor I'm using
* the native PHP GD library.
*/
@aklump
aklump / alter_exception_message.php
Created May 17, 2024 00:37
How to alter an Exception message.
<?php
$augmented_message = $exception->getMessage() . "\nMy augementation";
$reflected_exception = new \ReflectionObject($exception);
$message_property = $reflected_exception->getProperty('message');
$message_property->setAccessible(TRUE);
$message_property->setValue($exception, $augmented_message);
throw $exception;