Skip to content

Instantly share code, notes, and snippets.

View mortenscheel's full-sized avatar

Morten Scheel mortenscheel

View GitHub Profile
@mortenscheel
mortenscheel / CustomMacroServiceProvider.php
Created October 25, 2024 08:13
Laravel Http macro that records the raw HTTP request and response to a file.
<?php
declare(strict_types=1);
namespace App\Providers;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\PromiseInterface;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
@mortenscheel
mortenscheel / callable2code.php
Last active June 14, 2024 07:31
PHP callable to source code
<?php
public function callableToCode(mixed $callable): ?string
{
if (is_string($callable) && str_contains($callable, '@')) {
$callable = explode('@', $callable);
}
if (is_array($callable) && count($callable) === 2) {
[$class, $method] = $callable;
$instance = app($class);
@mortenscheel
mortenscheel / AppServiceProvider.php
Created April 24, 2024 13:00
Bouncer ACL with fallback to relation's abilities
<?php
namespace App\Providers;
use App\CustomClipboard;
use Illuminate\Cache\ArrayStore;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
@mortenscheel
mortenscheel / process_response_stream.php
Created November 7, 2023 14:50
Example of how to process a streamed response from Laravel's Http client line by line.
<?php
use GuzzleHttp\Psr7\Utils;
use Illuminate\Support\Facades\Http;
$stream = Http::withOptions(['stream' => true])
->get('https://example.com')
->toPsrResponse()
->getBody();
while ($line = Utils::readLine($stream)) {
@mortenscheel
mortenscheel / LazilyRefreshDatabaseIfNecessary.php
Last active September 13, 2024 13:03
A replacement for Laravel's LazilyRefreshDatabase trait that only runs migrate:fresh if necessary.
<?php
namespace Tests;
use Artisan;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
use Illuminate\Support\Str;
trait LazilyRefreshDatabaseIfNecessary
@mortenscheel
mortenscheel / deploy-laravel-docker-compose.php
Created May 10, 2023 12:39
Deployer laravel replacement for docker compose projects
<?php
namespace Deployer;
require_once __DIR__.'/vendor/deployer/deployer/recipe/common.php';
add('recipes', ['laravel']);
//set('shared_dirs', ['storage']);
set('shared_files', ['.env']);
@mortenscheel
mortenscheel / AppServiceProvider.php
Created February 24, 2023 14:42
Write raw request and response to file with Laravel's Http client
<?php
namespace App\Providers;
use GuzzleHttp\Middleware;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
@mortenscheel
mortenscheel / AnsiText.php
Last active October 27, 2022 09:12
A simple ansi text builder for PHP
<?php
class AnsiText implements \Stringable
{
public const BLACK = 30;
public const RED = 31;
public const GREEN = 32;
public const YELLOW = 33;
public const BLUE = 34;
public const MAGENTA = 35;
@mortenscheel
mortenscheel / php-run-as-sail.sh
Created September 30, 2022 10:52
PHP wrapper script for Laravel Sail and PhpStorm
#!/usr/bin/env sh
runuser -u sail -- php "$@"
# 1. Copy or mount this script into the laravel.test container.
# 2. Setup your remote PHP interpreter in PhpStorm to use the script as "PHP Executable"
@mortenscheel
mortenscheel / detect_horizontal_overflow.js
Last active August 17, 2022 08:13
Find and highlight DOM elements overflowing the viewport width, causing the horizontal scrollbar to appear.
const detectHorizontalOverflow = (root) => {
root = root || document;
const { offsetWidth } = document.documentElement;
let previousOverflowing = null;
let overflowingCount = 0;
root.querySelectorAll('*').forEach((el) => {
const { right } = el.getBoundingClientRect();
if (right > offsetWidth) {
// Ignore the children of overflowing elements. The issue is probably their parents.
if (previousOverflowing === null || !previousOverflowing.contains(el)) {