PHP

Robo – The Awesome Task Runner For PHP


Web Development is Hard. There are so many things you have to take into consideration during the course of building web applications, especially when you want to follow best practices and deliver the best product.

One of those several actions is automating tasks.

We have task runners like Grunt and Gulp, these two do an amazing job of automating several tasks. In short, virtually every web developer has adopted its use in building quality apps.

Grunt and Gulp are built in Javascript. I am a core PHP and JS developer, but I have discovered over time that some( maybe a few ) PHP devs don’t really know a lot about what’s dumped in the grunt and gulp files, instead they have the know-how to tweak things a little bit to get by,..which is good!

What if you had a replica of that?..What if it’s written in the language you shield as a sword daily?. What if you understand every single thing about automating those tasks?

Yes, there is a PHP TASK RUNNER called ROBO

Robo allows you to write tasks in common OOP PHP Style. You can use it to automate your tasks, start workers, run parallel tasks, execute commands, run tests and also watch file changes.

Let’s get to work

1. Installation

You can easily install it via Composer

Most times I like to do a global installation like so:

Then I can just call robo from any directory.

2 .Create a folder, our directory structure would be like this:

——— webapp

————–app

—————–cache

—————–logs

—————–site.php

————-css

—————–app.css

—————–boostrap.css

—————–style.css

————-dist

——————uploads

————–vendor

——————bower.json

3. Now just run robo from the terminal. You should see a prompt like this:

Agree by pressing Y.

Now you have a RoboFile.php created. The RoboFile will reside in the root of your directory.

Go ahead and dump all sorts of different css content into app.css, bootstrap.css and style.css.

Create several .txt files inside the cache and logs folder.

Let your RoboFile.php look like so:

RoboFile.php

Every public method serves as a command and it obeys camelCase format in writing the methods.

So I can run the first method from the terminal like so:

That will start the inbuilt PHP server and load site.php

Second method , clean()

_cleanDir deletes all files in a folder. In this instance, it deletes all the files in the cache and logs folder.

_deleteDir deletes the folder directory. In this case, it deletes the uploads folder.

So, run robo clean like so:

Third method, build()

As you can easily decipher from the code, it concatenates all the three css files into just one and helps to create a minified version too.

It can also install the bower dependencies present in our bower.json file.

Run it like so: robo build

Fourth method, test()

This invokes a globally or locally installed PHPUnit.

Run it like so: robo test

I have an error on my terminal because I don’t have any version of PHPUnit installed.

Conclusion

There are so many things you can accomplish with Robo.  Git, ssh, Docker, Vagrant and so many other tasks that need automation. Check out one example used in Codeception here. You can now imagine the vast possibilities after going through that file.

Head over to their documentation, dig deeper and do exploits with Robo

Please if you have any observations or comments, feel free to drop it in the comments section below.

 

PROSPER OTEMUYIWA

About PROSPER OTEMUYIWA

Food Ninja, Code Slinger, Technical Trainer, Accidental Writer, Open Source Advocate and Developer Evangelist.