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.
Shane Becker yeah, where are all these #ruby apps consuming µf2?! My fairly complete #php µf2 parser has had 55 installs so far, probably mainly me updating it :)
aral hideous. That makes me more embarrassed to be a PHP dev than all the bad rep it has as a language. And they're teaching SVN! It gets worse :(
I love that #php now has shiny namespacing and a thriving code sharing community, but I think the heavily hierarchical namespacing practises used by some of the community (e.g. symfony components) are unhealthy.
They are difficult to memorise, relying on (often slow) IDE autocomplete, and encourage a use
statement for each class. That’s pretty much a scoped equivalent of from x import *
in python — not a good practise! It’s still namespace pollution, it just takes longer to write.
I am trying to use a more python–like, package-centred approach with much fewer subnamespaces. The outcome of this should be that you use
the package name:
use BarnabyWalters
osse;
…and then using all the classes/subnamespaces from that root, e.g: $t = Posse\Helpers::convertHtmlToTwitterFormat($s);