MidCOM 3 and context injectors

cover image for MidCOM 3 and context injectors

Context-aware life coaching ad
I know, according to roadmap we all should be now focusing in hammering out bugs in Midgard 8.09, or as we call it, "Ragnaroek" instead of working on MidCOM 3. But Tero had a specific problem he needed to solve, and I wanted to ensure it was done right. And so I ended up adding support for context injectors into MidCOM 3.

What about contexts anyway?

Context in MidCOM is an object that contains all useful information related to the request at hand: arguments, URL, templating information. And the plan is to make it much more.

Context is the main way for controllers to find out about their environment, and for the templating system to decide which views to run and how. As we want to make the new MidCOM lean and efficient, we did not want to stick everything possible into the context. But in order to solve problems developers will need more stuff there. And so the idea of context injectors was born.

Context injectors are a new API components can use to add or modify information in the current context. The injectors will be run in two places:

  • Process injectors will be called before dispatcher loads the component and calls the controller
  • Template injectors will be called after controller has finished and before a template is collected and displayed

There are many things you can do there. Some immediate use cases are:

  • Geopositioning injector could add user's current location to the context so that a controller can fetch nearby data
  • Theming injector could switch the template entry point (in old Midgard term's the ROOT element) according to user's wishes
  • Mobile optimization injector could also switch the template entry point if user is accessing the site with an iPhone or OpenMoko

Using context injectors

The way this works is relatively simple. To create an injector, you create a component and create an injector class to it. Depending which injector you want to provide, you either implement a inject_process or inject_template public method there. The method will have full access to the current context (in $_MIDCOM->context) and can add or change things there. The just declare that you have the class in the component's manifest by adding a key process_injector or template_injector with value being the class name.

MidCOM will take care of loading the component in question in the right time, and of calling the injector method. This will all happen automatically once the component is installed.

If your controller or template uses context information that is not part of the default context data, then make sure to first check that the data is available via an isset(). This way your component will be able to work around missing context information if applicable to your scenario, or throw an error.

Written outside in the Platonik waterpipe cafe in Beşiktaş, Istanbul, while vine leaves above keep the air cool enough for working. The picture is from the Freds House blog.

Read more Midgard posts.