Congratulations Aaron Parecki on implementing real-time #indieweb comments in #p3k using #webmention, websockets, redis, node.js and PHP. Very impressive indeed :)
Congratulations Aaron Parecki on implementing real-time #indieweb comments in #p3k using #webmention, websockets, redis, node.js and PHP. Very impressive indeed :)
How to emulate standard #php front-controller behaviour of routing static assets statically, otherwise calling index.php using the PHP 5.4 built-in server:
// file: index.php
// Route static assets from CLI server
if (PHP_SAPI === 'cli-server') {
if (file_exists(__DIR__ . $_SERVER['REQUEST_URI']) and !is_dir(__DIR__ . $_SERVER['REQUEST_URI'])) {
return false;
}
}
// do usual front-controller stuff
Achievement Unlocked: Functional PHP by passing an anonymous function as an argument to itself
I just faked having a task queue for #taproot #indieweb note posting tasks using Symfony HttpKernel::terminate()
and it was the easiest thing ever.
Instances or subclasses of HttpKernel
have a terminate($request, $response)
method which, if called in the front controller after $response->send();
triggers a kernel.terminate
event on the app’s event dispatcher. Listeners attached to this event carry out their work after the content has been sent to the client, making it the perfect place to put time-consuming things like POSSE and webmention sending.
Once you’ve created your new content and it’s ready to be sent to the client, create a new closure which carries out all the the time consuming stuff and attach it as a listener to your event dispatcher, like this:
$dispatcher->addListener('kernel.terminate', function() use ($note) {
$note = sendPosse($note);
sendWebmentions($note);
$note->save();
}
Then, provided you’re calling $kernel->terminate($req, $res);
in index.php, your callback will get executed after the response has been sent to the client.
If you’re not using HttpKernel and HttpFoundation, the exact same behaviour can of course be carried out in pure PHP — just let the client know you’ve finished sending content and execute code after that. Check out these resources to learn more about how to do this:
fastcgi_finish_request()
flush()
HttpFoundation\Request::send()
as a sample implementationFurther ideas: if the time consuming tasks alter the content which will be shown in any way, set a header or something to let the client side know that async stuff is happening. It could then re-fetch the content after a few seconds and update it.
Sure, this isn’t as elegant as a message queue. But as I showed, it’s super easy and portable, requiring the addition of three or four lines of code.
I’m thinking the time might have come to write a wrapper around #php DOMDocument which actually makes it usable. Thoughts:
querySelector
and querySelectorAll
are implemented for both the document and individual elements via Symfony XPath → CSS converter and relative XPath queriesinnerText
, innerHTML
for consistency@packagist feature request: ID attrs on each version element on package pages so we can link to individual versions
Just pushed latest #taproot changes: using htmlpurifier.org to remove any nasties in reply contexts and comments, hopefully with upcoming php-mf2 changes that’ll allow limited HTML comments!
Also using brand new php-mf2-cleaner to parse said reply contexts and comments, find authors, etc. Check it out if you deal with #microformats 2 at all in PHP.
I’m rather impressed with guzzlephp.org’s HTTP Link header abstraction. Parsing Link headers and providing a simple API to check for the existence of/fetch links by rel is welcome attention to detail and cements it’s position as the only PHP HTTP client I will likely ever need.
Need to use require.js to load a bunch of scripts compiled via assetic into a PHP file, annoyed by auto-append of .js, don’t want to set up irritating routing? Add a ? to the URL, require.js will add a .js to the query string, loading the file correctly.
Turns out that performing a GET request on a data URI from PHP works if file_get_contents is used, not if cURL is used. I wonder what support is like for other server side languages — using data URIs in with
could be the basis of some interesting #webactions.
#php-mf2 0.2.0 update brings API improvements and twitter classname -> mf2 classname conversion so you can bung in markup from twitter.com and get (slightly buggy) mf2 json out #microformats #update #php
Try it out here.
pcntl_wait($status); //Protect against Zombie children
Every now and again the otherwise banal PHP docs make me laugh.
Really loving bastianallgeier’s thoughts on PHP as a templating language — very very similar to my own approach #php #dev #bookmark
Finally decided that symfony Security component is way too complicated for my little #taproot, so ditching it — but I’ve learnt a lot from digging through it and my further efforts will try to provide some of the amazing flexibility it gives whilst being more performant and easier to understand #php #dev #meta
I’m trying to grow up but it’s hard past a certain age
Ha, love it Aral Balkan :)
php2python.com is super useful for learning all the little things which always need doing when moving from #php to #python, e.g. splitting a string using another string, or pretty-printing a dict.