Les tribulations d'un octet

← revenir au blog


Composer or : How I Learned to Stop Worrying and Love the Task Runner in PHP

Attention cet article à plus de 2 ans ! Les informations peuvent ne plus être d'actualité et les liens peuvent être cassé.

I started a project with PHP (which I like !) ! I was full of hope to come back to my first language. With new approach from scala or javascript I though that I could produce better code with PHP. So I started to make a structure with documentation, unit tests, git, everything I though good for a large scale project. Then, I search for a task runner (It helps me a lot with scala and javascript).

I first looked at tasks runners already integrated to PHPStorm and I found... nothing ! nothing ? so there is no big PHP task runner ? As I looked at google results I found out that many peoples comes with the same disillusions.

There is task runners in PHP like robo or bldr but there is no big one. Solutions in PHP complicated and not really mature when you leave Symfony or other big frameworks with integrated consoles. So what to do ?

complicated

At that precise moment, I remember a project where I only use npm scripts as task runner. After all, Javascript, or PHP are scripting languages ! So let's script my tasks in PHP ! Do you know that composer can do that ? I didn't ! So let's see how things works with composer :).

What do I expect from tasks

That's the whole point, in many projects, there is no need for many tasks, so the first thing you need to ask is, what do I want to be automated ? What things did I do every time I don't want to do anymore ?

first things first, I want my documentation to generate itself with a simple line (I don't want to type the source folder each time). Moreover, I want that task to be the same for every one working on the project, so it has to be a task. But is it ? It's only a line of bash code ! We will get back to that later.

I also have unit tests with PHPUnit. This is another line of code with an entry point and a folder... Nothing complicated, but in order to make things easier for me and for my colleagues I have to transform it into a task.

There are assets that I would like to compile like less. One task should be to compile assets. I use to develop with a watcher (from PHPStorm or other), and I would like to find how to get a file watcher with PHP as well.

Also, packages that distribute my clean software would be nice !

What can I launch from composer

The script part of the composer documentation say that "A script, in Composer's terms, can either be a PHP callback (defined as a static method) or any command-line executable command".

perfect

It's exactly what we need, don't we ? So let's go into the code. First We need to declare our tasks in the scripts section of composer.json. Then we need to tell it where to find our scripts in the autoloader section (they support classmap in order to load your php tasks files). Here is an example for you :

As you can see, you can call statics function from a class ! Here you can do whatever you want, as you would in a classic PHP scripts, plus there are Composer Event which provide much information about your project and its structure.

It Works

After discovering how task runners do things (especially the Symfony one) I expected things to be harder than that, but in fact I got a solution that depend only on composer and works just fine everywhere. It's a KISS solution and maybe, if the project goes well, It won't be enough. For now, however, I'm really happy with this. Yes, I miss a watcher but it's okay for this project.

Hopes this could help you start scripting your task with ease. Let me know your own dev environment in PHP :).