Skip to content

Instantly share code, notes, and snippets.

@kmuenkel
kmuenkel / helpers.php
Created March 15, 2023 12:59
Wildcard support for get path
<?php
if (!function_exists('get_path')) {
function get_path(array $data, $path)
{
$nodes = is_string($path) ? explode('.', $path) : $path;
$node = array_shift($nodes);
$getNext = function ($item) use ($nodes) {
return $nodes ? (is_array($item) ? get_path($item, $nodes) : null) : $item;
@kmuenkel
kmuenkel / helpers.php
Created February 6, 2023 17:24
Compile arguments in a Laravel fashion for closure bindings
<?php
if (!function_exists('array_is_assoc')) {
/**
* Determine if the given array is associative or numerically indexed
*
* @param array $array
* @return bool
*/
function array_is_assoc(array $array)
@kmuenkel
kmuenkel / DuskTestCase.php
Created February 2, 2023 17:04
Control which browser plugin will be used by Dusk by .env values
<?php
namespace Tests;
use InvalidArgumentException;
use Illuminate\Support\Collection;
use Laravel\Dusk\TestCase as BaseTestCase;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\RemoteWebDriver;
<?php
use Carbon\Carbon;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\Query\Expression;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
@kmuenkel
kmuenkel / xdebug.ini
Created November 14, 2022 19:48
Docker XDebug configs for browser callbacks version 2 and 3
;zend_extension=/usr/local/lib/php/extensions/no-debug-non-zts-20131226/xdebug.so
;Version 2
xdebug.remote_autostart=0
xdebug.remote_connect_back=0
xdebug.remote_enable=On
xdebug.remote_handler=dbgp
xdebug.remote_host=host.docker.internal
xdebug.remote_mode=req
xdebug.remote_port=9003
@kmuenkel
kmuenkel / TestCase.php
Last active April 28, 2022 07:50
Allow PhpUnit Environment Variables to override Cached Configs
<?php
class TestCase
{
/**
* @inheritDoc
*/
protected function setUpTraits()
{
$this->artisan('optimize:clear');
@kmuenkel
kmuenkel / AppServiceProvider.php
Last active June 23, 2022 07:04
Dot notation with query support and without full-path recursive data retrieval
<?php
namespace App\Providers;
use Illuminate\Http\Request;
use Illuminate\Support\{Collection, ServiceProvider};
class AppServiceProvider extends ServiceProvider
{
/**
@kmuenkel
kmuenkel / get_route.php
Created January 16, 2022 08:08
CI4 get route by name
<?php
/**
* @param string $name
* @param array $params
* @return string
*/
function routeByName(string $name, array $params = []): string
{
if (!static::$routesByName) {
@kmuenkel
kmuenkel / TestCase.php
Created December 29, 2021 23:40
Create OAuth Bearer token for testing
<?php
$this->client = app(ClientRepository::class)->create(null, $clientName, '');
$response = $this->post(route('passport.token'), [
'grant_type' => 'client_credentials',
'client_id' => $this->client->getKey(),
'client_secret' => $this->client->plainSecret,
'scope' => implode(' ', $scopes)
]);
$this->token = $response->json('token_type').' '.$response->json('access_token');
@kmuenkel
kmuenkel / helpers.php
Last active December 23, 2021 21:28
Dot notation config traversing
<?php
if (!function_exists('conf')) {
/**
* Look for a dot-delimited name/key reference
* @param string $name
* @return mixed|null
*/
function conf(string $name)
{