Skip to content

Instantly share code, notes, and snippets.

View danilopinotti's full-sized avatar
Possibly drinking coffee

Danilo Pinotti danilopinotti

Possibly drinking coffee
View GitHub Profile
@danilopinotti
danilopinotti / ExecutionId.php
Created September 20, 2023 12:56
Laravel setup Logger as JSON
<?php
namespace App\Support;
use Illuminate\Support\Facades\App;
use Symfony\Component\Uid\Factory\UlidFactory;
class ExecutionId implements \Stringable
{
private string $id;
@danilopinotti
danilopinotti / Searchable.php
Last active November 1, 2022 02:37
Laravel searchabe trait
<?php
namespace App\Support\Models;
use Illuminate\Database\PostgresConnection;
use Illuminate\Support\Arr;
trait Searchable
{
public function scopeSearch(
@danilopinotti
danilopinotti / ClientRequestWatcher.php
Last active October 6, 2022 16:49
Show the Transfer Time in Client Request URI in Telescope
<?php
namespace App\Services\Telescope\Watchers;
use Illuminate\Http\Client\Events\ResponseReceived;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\Watchers\ClientRequestWatcher as BaseWatcher;
use Laravel\Telescope\Telescope;
class ClientRequestWatcher extends BaseWatcher
@danilopinotti
danilopinotti / HighOrderCacheProxy.php
Created September 28, 2022 15:27
Implements a layer above objects that caches all method calls
<?php
namespace App\Support;
class HighOrderCacheProxy
{
protected string $ttl = '15 minutes';
protected array $tags = [];
public function __construct(
@danilopinotti
danilopinotti / CacheManager.php
Created September 28, 2022 11:57
Cache Manager to easily converts TTL and enable/disable custom cache
<?php
namespace App\Support;
use Illuminate\Contracts\Cache\Repository;
class CacheManager
{
private ?string $driver = null;
@danilopinotti
danilopinotti / pi.py
Last active September 13, 2022 15:02
Simulate an approximation of PI value using Python
import numpy as np
import multiprocessing as mp
import random
import math
threads = mp.cpu_count()
points = 100_000_000
def simulate_pi(points):
radius = 1
@danilopinotti
danilopinotti / OrderByPriorityTrendCriterion.php
Last active May 25, 2022 13:04
Criterion to order a given query by a priority trend. This class may be applied in Laravel Query Builder.
<?php
namespace App\QueryBuilderCriteria;
use App\Contracts\QueryBuilderCriteria;
use Illuminate\Database\PostgresConnection;
class OrderByPriorityTrendCriterion extends QueryBuilderCriteria
{
public function apply($queryBuilder)
@danilopinotti
danilopinotti / memoization.php
Last active March 10, 2022 17:22
PHP Memoization inspired by @CViniciusSDias
<?php
use Ds\Map;
function memo(callable $function): callable
{
$cache = new Map();
return function (mixed ...$args) use (&$cache, $function): mixed {
if (isset($cache[$args])) {
return $cache[$args];
@danilopinotti
danilopinotti / SentMailsTest.php
Created August 18, 2021 18:30
Test sent emails in Laravel 8. Applied to test emails sent by Mail or Notification facades.
<?php
namespace Tests;
use App\User;
use Tests\TestCase;
use Illuminate\Notifications\Notification;
class MyNotification extends Notification
{
@danilopinotti
danilopinotti / tailrecursion.php
Created June 19, 2021 23:31 — forked from pyldin601/tailrecursion.php
PHP Tail Recursion
<?php
function tail($fn)
{
$underCall = false;
$pool = [];
return function (...$args) use (&$fn, &$underCall, &$pool) {
$result = null;
$pool[] = $args;