Skip to content

Instantly share code, notes, and snippets.

View IMSoP's full-sized avatar

Rowan Tommins IMSoP

View GitHub Profile
@IMSoP
IMSoP / 01-anon-classes.php
Last active March 14, 2023 21:12
PHP Brainstorming: inline syntax for lexical capture
<?php
// Existing functionality
function wrapLogger(LoggerInterface $existingLogger, string $myExtraContextValue) {
// Values can only be passed in via the constructor
$delegatingLogger = new class($existingLogger, $myExtraContextValue) extends AbstractLogger {
public function __construct(
// Constructor property promotion simplifies this, but we still need to declare private properties
private LoggerInterface $delegateTo,
private string $extraContextValue
@IMSoP
IMSoP / cipher-suite-checks.php
Created April 19, 2022 15:57
Check cipher suite policies against the SSLLabs User Agent support data
<?php declare(strict_types=1);
$sslLabsData = json_decode(file_get_contents('https://api.ssllabs.com/api/v3/getClients'), true);
$comparisonPolicies = [];
$comparisonPolicies['EC2 FS-1-2-Res-2020-10'] = [
'TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256',
'TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384',
'TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256',
Here are three things I'd like to see in any new auto-capturing closures RFC:
1. JUSTIFICATION
Don't assume that everyone already thinks it's necessary and desirable.
Try to come up with some real-life examples where you would want to use it.
Acknowledge the possible down-sides, and explain why you think they're
justified.
@IMSoP
IMSoP / 00-transaction-context.php
Created March 24, 2021 22:26
Python's with statement, but for PHP
<?php
class Transaction implements ContextManager
{
public function __construct(private Database $db) {}
public function contextEnter() {
$this->db->beginTransaction();
// Could return some other representation of the transaction if appropriate
return $this;
@IMSoP
IMSoP / Apis.php
Last active May 14, 2020 21:37 — forked from mikeschinkel/Apis.php
Examples reworked to use possible try-catch extensions
<?php
class Apis {
static function JsonGET( string $api_url, array $args = array() ) {
try {
$wp_error = null;
$args = wp_parse_args( $args, array(
'response_type' => ARRAY_A,
) );
<?php
$xml = <<<XML
<foo>
<bar baz="bob">1<bing>bang</bing></bar>
<bar>2</bar>
</foo>
XML;
$sx = simplexml_load_string($xml);
@IMSoP
IMSoP / bug-73297.phpt
Created October 11, 2016 21:03
/ext/standard/tests/http/bug-73297.phpt
--TEST--
Ignore 100 Continue returned by HTTP/1.1 servers
--INI--
allow_url_fopen=1
--SKIPIF--
<?php require 'server.inc'; http_server_skipif('tcp://127.0.0.1:12342'); ?>
--FILE--
<?php
require 'server.inc';
<?php
$options = [
'http' => [
'protocol_version' => '1.1',
'header' => 'Connection: Close'
],
];
$ctx = stream_context_create($options);
echo file_get_contents('https://http2.golang.org/reqinfo', false, $ctx);
Create Table test ( testjson jsonb );
Insert Into test ( testjson ) Values ( '[{"sid":"12345284239407942"}]'::jsonb );
Select testjson->>'sid' From test;
@IMSoP
IMSoP / Lazy_PDO.php
Last active January 22, 2016 19:11 — forked from anonymous/Lazy_PDO.php
<?php
class Lazy_PDO
{
private $pdo;
private $dsn, $username, $password, $options;
public function __construct($dsn, $username, $password, $options) {
$this->dsn = $dsn;
$this->username = $username;