Skip to content

Instantly share code, notes, and snippets.

@thekid
thekid / bundle.script.php
Last active March 22, 2021 16:22
Bundle JS and CSS
<?php namespace bundle;
use io\streams\{InputStream, Streams, StreamTransfer};
use io\{File, Folder};
use lang\{Environment, IllegalArgumentException};
use peer\http\{HttpConnection, HttpRequest};
use text\json\{Json, FileInput, StreamInput};
use util\URI;
use util\cmd\Console;
@thekid
thekid / Async.class.php
Created March 29, 2021 13:32
Async server
<?php
use io\File;
use peer\ServerSocket;
use peer\server\{AsyncServer, ServerProtocol};
class HttpProtocol implements ServerProtocol {
public function __construct(private array $handlers) { }
@thekid
thekid / dump-graph.script.php
Last active April 18, 2021 15:22
Dump and load Neo4J / ONgDB graphs using Cypher
<?php
use com\neo4j\Graph from 'xp-forge/neo4j';
use text\json\{StreamOutput, Types, Format} from 'xp-forge/json';
use util\cmd\Console;
$origin= new Graph($argv[1]);
$stream= new StreamOutput(Console::$out->stream(), Format::wrapped(indent: ' ', options: ~Format::ESCAPE_SLASHES));
@thekid
thekid / fetch.script.php
Created December 13, 2021 12:56
Fetch simultaneously using fibers
<?php
use peer\{Socket, Sockets, ConnectException};
use util\profiling\Timer;
function now() {
return (new DateTime())->format('H:i:s.v');
}
function fetch($url) {
@thekid
thekid / await.script.php
Last active December 16, 2021 13:34
Await with Fibers
<?php
use io\OperationTimedOutException;
use peer\{Socket, Sockets, ConnectException};
use util\Objects;
use util\profiling\Timer;
// Library code
function logger(... $args) {
printf("[%s] %s\n", (new DateTime())->format('H:i:s.v'), implode('', array_map(
@thekid
thekid / watch.script.php
Created December 17, 2021 15:59
INotify on Windows using ReadDirectoryChangesW() from Kernel32 and PHP FFI
<?php
// Map necessary types, structs and functions from Kernel32
$ffi= FFI::cdef('
typedef void VOID, *PVOID, *LPVOID, *HANDLE;
typedef char CHAR;
typedef int BOOL;
typedef const char *LPCSTR;
typedef unsigned long DWORD, *LPDWORD;
typedef unsigned long *ULONG_PTR, **PULONG_PTR;
@thekid
thekid / search-meta.script.php
Created March 27, 2022 09:03
MongoDB atlas facetted search
<?php namespace mongodb;
use com\mongodb\MongoConnection;
use lang\IllegalArgumentException;
use util\cmd\Console;
use util\profiling\Timer;
$dsn= $argv[1] ?? 'mongodb://localhost';
$namespace= $argv[2] ?? (function() { throw new IllegalArgumentException('Missing namespace'); })();
$term= $argv[3] ?? (function() { throw new IllegalArgumentException('Missing search term'); })();
@thekid
thekid / trigger.js
Created March 27, 2022 10:28
MongoDB search index creation trigger
exports = async function(changeEvent) {
const collection = context.services.get('Cluster0').db('dialog').collection('searchable');
console.log(changeEvent.operationType, ': ', JSON.stringify(changeEvent));
const document = {kind: 'ENTRY', 'title': changeEvent.fullDocument.title};
let result;
switch (changeEvent.operationType) {
case 'insert':
document._id = changeEvent.documentKey._id;
result = await collection.insertOne(document);
@thekid
thekid / sealed.diff
Created March 31, 2022 21:14
XP Compiler: Sealed classes
diff --git a/src/main/php/lang/ast/emit/PHP.class.php b/src/main/php/lang/ast/emit/PHP.class.php
index d7d7728..9657f2b 100755
--- a/src/main/php/lang/ast/emit/PHP.class.php
+++ b/src/main/php/lang/ast/emit/PHP.class.php
@@ -390,6 +390,7 @@ abstract class PHP extends Emitter {
$result->at($class->declared)->out->write(implode(' ', $class->modifiers).' class '.$this->declaration($class->name));
$class->parent && $result->out->write(' extends '.$class->parent);
$class->implements && $result->out->write(' implements '.implode(', ', $class->implements));
+ $class->permits && $result->out->write(' permits '.implode(', ', $class->permits));
$result->out->write('{');
@thekid
thekid / o365-cert-auth.script.php
Created June 8, 2022 07:18
Authenticate an app using certificates
<?php
use io\streams\Streams;
use lang\IllegalStateException;
use util\cmd\Console;
use util\UUID;
use peer\http\HttpConnection from 'xp-framework/http';
/**