Skip to content

Instantly share code, notes, and snippets.

View joelharkes's full-sized avatar

Joël Harkes joelharkes

  • NS mobiliteitsdiensten
  • Den Haag, Netherlands
View GitHub Profile
@joelharkes
joelharkes / ServiceProvider.php
Created April 13, 2023 09:16
Log Http server-to-server requests in Laravel Clockwork
<?php
declare(strict_types=1);
namespace App\Providers;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Support\Facades\Event;
@joelharkes
joelharkes / LaravelUsage.php
Last active November 25, 2021 08:01
Making streams in PHP/Laravel
<?php
public function writeQueryAsCsvFileToStorage() {
$disk = Storage::disk('yourDiskNameHer');
// optional: ensure file doesn't exist:
// $disk->delete($filePath);
$cursor = $this->getQuery()
->toBase() // +-10x speed improvement, skip making laravel models
->cursor() // avoid loading whole result set into memory.
->map(fn ($item) => $this->map((array) $item));
@joelharkes
joelharkes / sftp.php
Created June 4, 2020 08:19
Upload file in vanilla php
protected function putfile(string $path, string $content)
{
$host = config('services.sftp.host');
$port = config('services.sftp.port');
$con = ssh2_connect($host, $port, [], ['disconnect' => fn () => Log::debug('sftp disconeccted')]);
if (false === $con) {
throw new HttpException(503, 'sftp connection failed', ['sftpHost' => $host, 'sftpPort' => $port]);
}
if (false === ssh2_auth_password($con, config('services.sftp.username'), config('services.sftp.password'))) {\
ssh2_disconnect($con);
@joelharkes
joelharkes / pgp-keys.ts
Last active September 23, 2019 08:29
Generate Pgp keys with node
import * as openpgp from "openpgp";
import { writeFileSync } from "fs";
import { join } from "path";
var phrase = "test12";
generateRsaKeys('test', phrase);
async function generateRsaKeys(name: string, passphrase: string) {
var options: openpgp.KeyOptions = {
userIds: [{ name: "Test user", email: "test@user.email" }], // multiple user IDs
@joelharkes
joelharkes / CrockfordBase32.php
Created August 5, 2019 13:07
Crockford base 32 implementation (lower case version)
/**
* Crockford base32 encoding implementation (useable with random_bytes())
* see: https://www.crockford.com/base32.html.
*/
class CrockfordBase32
{
const BITS_5_RIGHT = 31;
const CHARS = '0123456789abcdefghjkmnpqrstvwxyz'; // lower-case
@joelharkes
joelharkes / EventServiceProvider.php
Last active April 30, 2021 19:35
Sqreen implementation for laravel, SQREEN_ID configurable in environment variable
<?php
namespace App\Providers;
use Illuminate\Support\Facades\Event;
class EventServiceProvider extends ServiceProvider
{
/**
@joelharkes
joelharkes / MapMap.tsx
Last active December 8, 2017 15:54
Map function for the Map Class
// generic map function for the Map class for typescript
function MapMap<TKey, TValue, TReturn>(map: Map<TKey, TValue>, mapper: (key: TKey, value: TValue) => TReturn): TReturn[] {
var t = [] as TReturn[];
map.forEach((val, key) => t.push(mapper(key, val)));
return t;
}
// USAGE EXAMPLE
interface SimpleSelectProps extends BaseFieldProps<string> {
@joelharkes
joelharkes / launch.json
Last active September 18, 2022 08:19
Debug ts-mocha in vs code (visual studio code)
{
"version": "0.2.0",
"configurations": [
{
"name": "Run ts-mocha File",
"type": "node",
"request": "launch",
"runtimeExecutable": "${workspaceRoot}/node_modules/.bin/ts-mocha",
"runtimeArgs": [
"${file}"
@joelharkes
joelharkes / test-with-ts-mocha.js
Created November 21, 2017 13:44
Run ts-mocha programmatically (in a script)
require('ts-mocha');
const Mocha = require('mocha');
const fs = require('fs');
let testSubject = null;
if (process.argv.length === 3) {
testSubject = process.argv[2];
}
const mocha = new Mocha({ timeout: 50000 });
@joelharkes
joelharkes / launch.json
Created November 21, 2017 13:39
Debug IronPython in visual studio code (vscode)
{
"version": "0.2.0",
"configurations": [
{
"name": "IronPython",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"pythonPath": "C:\\Program Files (x86)\\IronPython 2.7\\ipy.exe",
"program": "${file}",