Skip to content

Instantly share code, notes, and snippets.

View mortenscheel's full-sized avatar

Morten Scheel mortenscheel

View GitHub Profile
@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
Created October 6, 2023 09:59
A replacement for Laravel's LazilyRefreshDatabase trait that only runs migrate:fresh if necessary.
<?php
namespace Tests;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\RefreshDatabaseState;
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)) {
@mortenscheel
mortenscheel / functions.sh
Last active September 10, 2022 14:31
Laravel specific git hooks for post-checkout and post-merge
#!/usr/bin/env bash
NC='\033[0m' # No Color
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
GREEN='\033[0;32m'
RED='\033[0;31m'
MAGENTA='\033[0;35m'
notify_about_actions_required() {
changed_files="$(git diff-tree -r --name-status --no-commit-id $1 $2)"
@mortenscheel
mortenscheel / DSP seeds.txt
Last active June 2, 2021 16:46
Dyson Sphere Program seeds
18097783
GG fire ice
Lava Ti and Si <1
Ir 2.7
Cu 1.1
Starter base nah
29679915
GG and outer fire ice
Lava ti + Si <1