Skip to content

Instantly share code, notes, and snippets.

View fjarrett's full-sized avatar

Frankie Jarrett fjarrett

View GitHub Profile
<?php
/**
* Recursively replace array elements in associative arrays.
*
* The key difference between this function and array_replace_recursive() is that in this function
* only associative arrays are merged recursively and list arrays are replaced entirely (not merged).
* This approach ensures that list arrays are entirely refreshed with new values, while associative
* arrays have their contents intelligently combined, preserving structural integrity and updating
* where necessary.
@fjarrett
fjarrett / ExampleJob.php
Last active July 7, 2023 19:24
How to run Laravel Jobs like Actions with return values, best of both worlds
<?php
namespace App\Jobs;
use Exception;
use Throwable;
class ExampleJob extends BaseJob
{
public int $number;
@fjarrett
fjarrett / composer.json
Last active November 10, 2022 16:44
Alternate package source: composer update foo/project
{
{
"repositories": [
{
"type": "vcs",
"url": "git@github.com:bar/project.git"
}
],
"require": {
"foo/project": "dev-main"
@fjarrett
fjarrett / array_walk_async.php
Created July 29, 2022 17:37
Apply a user supplied function to every member of an array, in parallel
<?php
/**
* Apply a user supplied function to every member of an array, in parallel.
*
* @param array $array
* @param callable $callback First arg is the value, second arg is the key.
* @param mixed $arg Will be passed as the third parameter to the callback.
* @param int $maxProcesses
* @return bool
@fjarrett
fjarrett / laravel-collection-macros.php
Last active June 7, 2023 17:34
Just some useful macros for Laravel Collections
<?php
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
/**
* ```
* collect(['foo' => ['bar' => 'baz']])->recursive();
* collect(['foo' => ['bar', 'baz', 'qux']])->recursive(true);
* ```
@fjarrett
fjarrett / laravel-helpers.php
Last active June 27, 2023 22:45
Just some useful helper functions for Laravel
<?php
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Str;
if (! function_exists('add_query_arg')) {
/**
* Add query arg(s) to a URL.
*
@fjarrett
fjarrett / Retry.php
Last active March 14, 2021 17:18
Trait to support retry annotations on PHPUnit tests, inspired by https://blog.forma-pro.com/retry-an-erratic-test-fc4d928c57fb
<?php
namespace Tests\Traits;
use PHPUnit\Util\Test as TestUtil;
use Throwable;
trait Retry
{
/**
@fjarrett
fjarrett / async-array-walk.php
Last active January 26, 2021 17:38
Apply a user supplied function to every member of an array, in parallel. Similar to PHP's array_walk(), except the input array isn't passed by reference and the callbacks are ran asynchronously. The technique is highly portable and requires only PHP 5.4 and the PCNTL extension.
<?php
/**
* Apply a user supplied function to every member of an array, in parallel
*
* Similar to PHP's array_walk(), except the input array isn't passed by
* reference and the callbacks are ran asynchronously. The technique is highly
* portable and requires only PHP 5.4 and the PCNTL extension.
*
* The most obvious limitation is that we can't pass the input array by
@fjarrett
fjarrett / kernel-upgrade-how-to.md
Last active April 5, 2022 08:33
How to upgrade Linux kernel on CentOS 7

Confirm the current kernel version:

$ uname -snr
Linux 3.10.0-1062.18.1.el7.x86_64 x86_64

Add the ELRepo repository:

yum update -y
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
@fjarrett
fjarrett / atomic_deploy.sh
Created May 9, 2020 01:07
Just an idea for a "Capistrano style" deploy setup
#!/usr/bin/env bash
# BASE (required) - Releases are stored here.
# TARGET (required) - This will be a symlink to the current release.
# REPO (required) - The address to your Git repo.
# KEEP - How many latest releases to keep around (default is 10).
#
# Example:
#
# BASE=/var/www/foo/releases TARGET=/var/www/foo/public_html REPO=git@github.com:foo/bar.git ./atomic_deploy.sh 2b5d4a