PHP Cloud Functions on Google Cloud Platform with “Functions Framework for PHP”

Google Cloud Platform has launched official support for PHP Cloud Functions using Functions Framework for PHP.

With it, an HTTP Cloud Function becomes as simple as this:

use Psr\Http\Message\ServerRequestInterface;

function helloHttp(ServerRequestInterface $request): string
{
    $queryString = $request->getQueryParams();
    $name = $queryString['name'] ?? $name;

    return sprintf('Hello, %s!', $name);
}

Functions that respond to Cloud Events can work with a \Google\CloudFunctions\CloudEvent instance:

use Google\CloudFunctions\CloudEvent;

function helloworldPubsub(CloudEvent $event): void
{
    $name = 'World';
    $cloudEventData = $event->getData();
    if (!empty($cloudEventData['data'])) {
        $name = base64_decode($cloudEventData['data']);
    }

    // …
}

Installation per Composer

composer require google/cloud-functions-framework

Functions Framework for PHP →
Introducing PHP on Cloud Functions →

Unconventional Autoloaders in PHP

Recording from the October 2020 edition of Laravel Worldwide Meetup where Liam Hammet (starting at 36:58​) showed off some nice use cases for custom autoloaders in PHP:

We use autoloaders in PHP all the time, but if you change your mindset a little, you’ll find they can do a lot more than you might’ve thought. We’ll be taking a look at using some unconventional techniques, we can use autoloaders to take PHP a step further, introducing interesting and exciting new functionality PHP natively doesn’t support.

Really enlightening stuff! Slides also available.

Unconventional Autoloaders (Talk) →

PHP Curl Security Hardening

Good post — with accompanying code — on PHP.Watch on how to tighten the almighty curl:

  1. Limit Curl Protocols
  2. Do not enable automatic redirects unless absolutely necessary
  3. If redirects are enabled enabled, limit allowed protocols (if different from #1 above)
  4. If redirects are enabled, set a strict limit
  5. Set a strict time-out
  6. Do not disable certification validation, or enforce it
  7. Disable insecure SSL and TLS versions

PHP Curl Security Hardening →

Ray – Dump Debugging Evolved

The folks over at Spatie have released a new tool called Ray that helps you with debugging.

Ray is a beautiful, lightweight desktop app that helps you debug your app. After installing one of the libraries to send information to Ray, you can use the ray() function to quickly dump stuff. Any variable(s) that you pass to Ray will be displayed.

Since Spatie mainly develops with Laravel it plays very nice with it. It supports logging all performed queries or properly displaying the contents of any Eloquent Model / Mailable for example.

ray('Hello world');

ray(['a' => 1, 'b' => 2])->color('red');

ray('multiple', 'arguments', 'are', 'welcome');

ray()->showQueries();

User::firstWhere('email', 'john@example.com');

At its core Ray is “simply” an app that listens for incoming messages on a specific port (e.g. 23517). That means you can use it with any other programming language, as long as you send it the proper (JSON) payload. Libraries to use Ray with WordPress or JavaScript are already available too.

Ray →
Ray Docs →
Ray Introductory Blogpost →

💁‍♂️ Sidenote: To debug JavaScript I’d recommend the DevTools you already have though, as they’re already built for it. To debug from a remote source you could use something like JSConsole or get knee-deep in ADB and remote debugging protocols. But from a technological point of view it’s pretty cool that you can use Ray for it if you wanted to.

It’s not legacy code — it’s PHP

In 2004 Vimeo wrote their first backend code in PHP. 16 years later and they (mainly) still run PHP.

Many developers wrote a bit of PHP a decade ago and have since moved on to better-respected languages. They’re often eager to tell others how relieved they are to not be writing PHP anymore, and how it can’t be redeemed.

I’m here to tell you that it can, and Vimeo’s continued success with PHP is proof that it’s a great tool for fast-moving companies in 2020.

One can’t deny the PHP 7 release was a big leap forward. The new PHP 8 release also added quite a few nice additions to the language. Really looking forward at what’s more to come for PHP …

It’s not legacy code — it’s PHP →

Related:

(ref)

Run Composer Scripts in Parallel with veewee/composer-run-parallel

Nice addition to Composer by Toon Verwerft which allows you to run your custom commands in parallel

This composer plugin allows you to run the tasks inside your composer file in parallel. No more waiting on one specific task!

Say you have these two custom commands:

{
  "scripts": {
    "php1": "@php -r 'sleep(3); echo \"1\";'",
    "php2": "@php -r 'echo \"2\";'"
  }
}

Then you can invoke them to run in parallel as follows:

$ composer run parallel php1 php2

Using @parallel it’s also possible to trigger the functionality from inside another custom command:

{
  "scripts": {
    …
    "php1And2": "@parallel php1 php2"
  }
}

Installation per Composer:

$ composer require --dev veewee/composer-run-parallel

You might want to install this one globally 😉

veewee/composer-run-parallel

ibis — A PHP tool that helps you write eBooks in markdown and convert to PDF

Mohamed Said recently wrote the book “Laravel Queues in Action”. To create the book he wrote it Markdown and then used his self-created tool ibis to generated the PDF and ePub versions.

Installation per Composer:

composer global require themsaid/ibis

After which you start a new project with ibis init

Check out the preview of “Laravel Queues in Action” to get an idea of what is being generated.

themsaid/ibis
Laravel Queues in Action Preview (Light) →
Laravel Queues in Action Preview (Dark) →

Front Line PHP – Building modern web applications with PHP 8

The folks over at Spatie are at it again, this time with Front Line PHP.

An ebook on cutting edge tactics in PHP 8, accompanied by videos and practical examples.

The videos covering the new additions to PHP 8 are free to watch, and should give you an idea of what you can expect.

At €79 (now €56 due to #BlackFriday) this is a steal.

Front Line PHP →
Front Line PHP Videos (paid + free)

Convert Guzzle requests to curl commands with namshi/cuzzle

The other day the namshi/cuzzle PHP pacakge came in really handy.

This library let’s you dump a Guzzle request to a cURL command for debug and log purposes

This way I could test some things on the CLI, and easily share these tests with all my colleagues, including those without PHP installed.

use Namshi\Cuzzle\Formatter\CurlFormatter;
use GuzzleHttp\Message\Request;

$request = new Request('GET', 'example.local');
$options = [];

echo (new CurlFormatter())->format($request, $options);
// ~> curl example.local -X GET -A 'GuzzleHttp/6.4.1 curl/7.71.1 PHP/7.4.9'

Also comes with a Monolog formatter to easily log the resulting curl commands in your log files. Do keep in mind that you might be leaking sensitive information (passwords/tokens) that way …

Installation per Composer:

composer require namshi/cuzzle

Cuzzle, cURL command from Guzzle requests →

Parse the output of git log using PHP

This gist might come in handy one day: