Thursday, February 25, 2021

It Did Not Take Me Two Months To Update To Slim 4

It only took me a day.

What was I doing during the previous two months? Nothing related to the project, unfortunately.

Anyhow, upgrading to Slim 4 was pretty straightforward. The biggest roadblock was that slim no longer comes bundled with a dependency injection container.

Normally I'd just swap in pimple as that's what I've used since forever. Unfortunately, pimple is apparently not PSR-11 compliant and Slim 4 requires any container used to be PSR-11 compliant. Their documentation referenced (which I interpreted as a reccomendation) something call PHP-DI, so I decided to just use that.

PHP-DI is pretty straightforward to use, though it did take me a while to figure out how best to get the dependencies into the container and how best to get them back out when I needed them.

As with pimple, the PHP-DI container is just an object that stores key/value pairs. The container object doesn't implement array access so you can't treat it like an array when convenient, which is something I was used to with pimple. Theoretically, you can directly set KVP's to the container object, but the documentation discourages that. Rather, you should use a container builder class to set your dependencies because this will eventually allow you to "compile" the container in production and gain performance improvements as a result. Okay.

Unfortunately, this meant that the old way I used to load dependencies into the container wouldn't work. When I was using pimple I 'registered' dependencies to the container by calling a series of 'provider' classes. The only real benefit to these classes were that they allowed me to store some code in a series of organized and well labeled files. So, not really that important and maybe bordering on gold-plating.

With the new container, I decided to go with brutally straightforward. The dependencies are all loaded into the container in the index file that contains all the bootstrapping and the routing for the service. I don't "like" the way the dependencies are handled, but only because the current form offends my artistic sensibilities.

So yeah, now I can really build Mabel.

No comments:

Post a Comment