Loïc Faugeron Technical Blog

Introducing Memio, code generator 08/04/2015

TL;DR: Work in Progress: Memio, a library for PHP code generation.

Code generators write code for you, so you don't have to! There's many kinds out there:

Many frameworks provide code generators, because they can be a real time saver by automating repetitive boring tasks (e.g. bootstraping a CRUD controller). The only issue is that usually we can't customize them.

For example with GeneratorBundle, it's impossible to create REST controllers.

Some of them provide templates but don't template engines, like phpspec for example: this is a step forward but that's not enough.

Now let's have a look at code generator libraries: the main ones don't allow an easy way to customize the coding style of generated code:

Some of them do use a template engine, but you need to write a lot of code in order to use them:

Don't panic! Memio is a code generator library that uses a template engine and provide out of the box templates, generators and even validation!

Started in september 2014 under the name "Medio", it has now matured enough to be soon released in stable version (1.0.0).

Let's have a look at what's going to be achieved with it.

Improving phpspec

The phpspec typehint extension was a good playground for Memio: it allowed to test generation of a method argument by:

Once Memio is stable, it will provide its own phpspec extension that aims at:

Curious about this last bullet point? Then read on.

Automating Dependency Injection

There are many ways to use constructors, and one of them is dependency injection: each argument is stored in a property and the class can then use them.

Note: Remember, Dependency Injection is a fancy word for passing arguments.

When doing so, we need to write a lot of boilerplate code:

Memio will automate this in its phpspec extension.

Possibly more?

We talked about GeneratorBundle: the issue is that we can't define our own templates. Well Memio could solve this issue in a reusable way!

Usually each applications have its own style, its own requirements, etc. In short: we can't use the same code generator between projects. But if we focus on a single project, then we can start using the same code generator to boostrap many classes: endpoint/page controllers, entities, etc. The possibilities are endless!

Conclusion

Memio, once stable, will provide a powerful and reusable way of generating code.

What's left to do before the release of v1.0? Mainly splitting the main package (memio/memio) into small ones (model, template-engine, validator, etc). The main package would become a "standard edition", allowing developers to select the features they want, for example the template engine (as requested by webmozart).

Stay tuned!

Thanks

I'd like to thank the following early contributors:

Keep up the good work!