Skip to content

Instantly share code, notes, and snippets.

@tonysm
tonysm / index.php
Last active December 18, 2022 02:57
Simple Blockchain in PHP
<?php
// This is the blockchain example from The Imposter's Handbook Season 2 book.
class Block
{
public static function mine(array $transactions, float $timestamp, int $difficulty, ?Block $previous = null)
{
if ($difficulty < 2) throw new \Exception("This isn't much of a challenge");
if ($difficulty > 18) throw new \Exception("Do polar icecaps matter to you?");
@tonysm
tonysm / action_text.rb
Last active July 13, 2022 19:20
Migrate Stored Hashes Using Rich Text Laravel
# Pseudo-code based on the PHP version...
namespace :action_text do
task refresh_embeds: :environment do
ActionText::RichText.where.not(body: nil).find_each do |trix|
next unless trix.embeds.size.positive?
trix.update_column :body, trix.body.fragment.replace("action-text-attachment[sgid]") do |node|
return ActionText::Attachment.from_attachable(
GlobalID::Locator.locate_signed(
@tonysm
tonysm / binary_search.php
Last active November 3, 2023 18:18
Algorithms in PHP
<?php
function binary_search(array $items, $lookingFor) {
$min = 0;
$max = count($items);
while ($min <= $max) {
$middle = $min + intval(floor($max - $min) / 2);
if ($items[$middle] === $lookingFor) {
@tonysm
tonysm / create.blade.php
Last active April 8, 2022 02:18
Combining Turbo Frames and Streams
<x-app-layout title="New Invitations">
<h1>New Invitations!</h1>
<form action="{{ route('invitations.store') }}" method="POST">
<div id="invitations"></div>
<button type="submit">Invite</button>
</form>
<x-turbo-frame id="create_trigger_invitations">
@tonysm
tonysm / index.php
Last active October 17, 2021 01:55
8 Queens Puzzle
<?php
class Queen
{
private int $row;
public function __construct(private int $column, private ?Queen $neighbour)
{
$this->row = 1;
}
@tonysm
tonysm / hub-example.sh
Created August 16, 2020 18:06
Commiting changes and sending a Pull Request to the infrastructure repository
# Configuring Git
git config --global credential.helper store
echo "https://${GITHUB_TOKEN}:x-oauth-basic@github.com" > "${HOME}/.git-credentials"
hub config --global hub.protocol https
hub config --global user.name "${GITHUB_NAME}"
hub config --global user.email "${GIT_EMAIL}"
# Cloning the infrastructure repository
hub clone ${GIT_REPO} infrastructure-repo
cd infrastructure-repo
@tonysm
tonysm / patch-example.sh
Created August 16, 2020 17:58
Patching images with Kustomimze
kustomize edit set image laravel-app-php=tonysm/docker4laravelapp-php:1.0.0
@tonysm
tonysm / docker-images.yml
Last active August 16, 2020 17:39
GitHub action for building docker images and patching infrastructure repository - part 01
name: Build Docker Images
on:
push:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
@tonysm
tonysm / 02-dependencies.diff
Last active May 26, 2020 14:48
12-factors-with-docker-code-samples
const app = express()
const port = 3000
-app.get('/', (req, res) => res.send('Hello World!'))
+app.get('/', (req, res) => res.send('Hello World 2.0.0!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
@tonysm
tonysm / tests_Unit_PropertyBasedTest.php
Created March 24, 2020 14:39
Property based testing in PHP with DataProviders, Generators, and Faker
<?php
namespace Tests\Unit;
use Faker\Factory;
use PHPUnit\Framework\TestCase;
class PropertyBasedTest extends TestCase
{
public function propertyBaseData()