How to create a Composer plugin

After tinkering around with Symfony4 and Flex I was curious about how Composer plugins work. So I started to check the Composer’s plugin API and the official documentation. The documentation is great and you even have a full example of a plugin but when I wanted to test it… nothing. The plugin wasn’t loaded and nothing happened. Why? Because I was testing it wrong. ūüôā So this is more of a tip on how to start with a Composer plugin than a real step by step tutorial on how to create it.

You can find the demo code here if you want to follow it while reading (ignore the instructions from README for now).

First steps:

  1. Create the composer.json file in a new folder;
  2. Make sure to set the “type” to “composer-plugin”, add “autoload” and “extra” sections;
  3. Create the PHP class defined in the “extra” section.

The wrong way to test the plugin (don’t do it like this):

What I did next was to actually run composer¬†in the same project where the plugin was created¬†as described above. But it doesn’t work like that because this is a plugin and should be added to a project to extend that project’s capabilities.

The right way to test the plugin:

So instead of trying to execute the plugin as a full featured project, add it to one of your projects. Create a repository for the plugin on Github or another VCS system, then update the composer.json file from your project and include it there:

Run composer update and make sure the plugin is installed.

In the demo the event is set to “init” which means that the plugin is executed each time composer runs. If you run¬†composer now you should see the plugin’s output in terminal:


First encounter with React JS

This is a small demo made using React and Boostrap. It’s a list page which contains¬†some of the¬†Avengers, with the possibility to reach a show page with details for each character.

The purpose of the demo was to get an opinion on this JavaScript¬†library for user interfaces, since I’m not a big fan of frontend development.

The conclusion:

  • Suitable for simple interfaces;
  • Easy to learn;
  • Possibility ¬†to port the web application to a native iOS or Android application with minimum of effort, using React Native.

The demo:

The code:


react_demo_avengers_list_page react_demo_avengers_show_page

Add PDF export functionality in SonataAdminBundle

By default SonataAdminBundle doesn’t come with a PDF export functionality. Since this is a nice feature to have in your application here’s a way to add PDF export to the CRUD pages.

The example works with the standard Symfony2 installation and the AcmeDemoBundle. The PDF will be generated using KnpSnappyBundle (the installation process is not covered here).

The Doctrine Entity:


The Color Admin class:

Enable the PDF export link on list page

In order to add the PDF export option the “getExportFormats” method needs to be overridden in the ColorAdmin class:

The “pdf” options should now be displayed on the list page next to the default ones.

Create custom service to handle PDF export

First the default export service (identified by “sonata.admin.exporter”) needs to be overriden. Register the service in src/Acme/DemoBundle/Resources/config/services.xml and make it aware of the knpSnappyPdf. Also the template engine will be necessary.

Next extend the Sonata exporter class as defined in the overridden exporter service:

The last step is to add the Twig template used to render the PDF content, as specified in the custom exporter service.  For example:

Of course, there is room for improvement. You can do it “SonataAdmin” way with a custom PdfWriter class (similar with the built-in XlsWriter, CsvWriter or JsonWriter) to separate the PDF conversion code, instead of having the entire process in the custom Exporter class.

Beautiful strings and the PHP solution

“Beautiful strings” is one of the proposed problems on¬†Facebook Hacker Cup 2013 Qualification Round. The presented PHP solution was already validated.

The problem:

When John was a little kid he didn’t have much to do. There was no internet, no Facebook, and no programs to hack on. So he did the only thing he could… he evaluated the beauty of strings in a quest to discover the most beautiful string in the world.

Given a string s, little Johnny defined the beauty of the string as the sum of the beauty of the letters in it.

The beauty of each letter is an integer between 1 and 26, inclusive, and no two letters have the same beauty. Johnny doesn’t care about whether letters are uppercase or lowercase, so that doesn’t affect the beauty of a letter. (Uppercase ‘F’ is exactly as beautiful as lowercase ‘f’, for example.)

You’re a student writing a report on the youth of this famous hacker. You found the string that Johnny considered most beautiful. What is the maximum possible beauty of this string?

The input file consists of a single integer m followed by m lines.

Your output should consist of, for each test case, a line containing the string “Case #x: y” where x is the case number (with 1 being the first case in the input file, 2 being the second, etc.) and y is the maximum beauty for that test case.

5 ‚ȧ m ‚ȧ 50
2 ‚ȧ length of s ‚ȧ 500

Example input:

Good luck in the Facebook Hacker Cup this year!
Ignore punctuation, please ūüôā
Sometimes test cases are hard to make up.
So I just go consult Professor Dalves

Example output:

Case #1: 152
Case #2: 754
Case #3: 491
Case #4: 729
Case #5: 646

The PHP solution:


More details about the solutions on the official page Qualification Round Solutions