The Symfony team is pleased to announce its latest contribution to the open source community, the Symfony Service Exploder. When run, this script "explodes" your application by creating a unique service class for every line. Each of these classes can then be manipulated using a YAML configuration layer, freeing you from having to write anymore PHP!

How can I explode my application?

Let's start with a simple example. Consider this PHP script that processes a contact form submission.

// contact/process_submit.php
include_once 'config.php';
 
$message = <<<EOF
Contact form submission
 
From: {email}
Sent: {now}
 
{body}
EOF;
 
mail(CONTACT_FORM_TO, CONTACT_FORM_SUBJECT, strtr($message, array(
  '{email}' => $_POST['email'],
  '{now}'   => date('Y-m-d H:i:s'),
  '{body}'  => $_POST['body'],
)));
 
header('Location: thanks.php');

For what it is, this will work fine. But what happens if you want to change the message format or change where the user is redirected upon submission? Without the service exploder in place, your only recourse would be to re-open the same PHP script and directly edit your PHP.

The Symfony Service Exploder presents another solution to this very common problem of having to edit PHP files. To expose the exploder API, simply pass your application through the exploder script.

$ php explode.php /path/to/project/root /path/to/exploded/project

This script will parse every *.php in the supplied directory and generate a new, exploded application. The exploded version of the above script would look something like this:

// contact/process_submit.php
include_once 'lib/explode/sfExplodoloader.php';
sfExplodoloader::register();
 
$e = sfExplosionFactory::getInstance();
 
$e['contact/process_submit.php'][1]->execute();
$e['contact/process_submit.php'][2]->execute();
$e['contact/process_submit.php'][3]->execute();
$e['contact/process_submit.php'][4]->execute();

You can already see the difference in how clean your exploded application's code becomes!

Configuring your explosion

The sfExplosionFactory class includes a YAML configuration layer, which is where the real power of exploding your application lies. With this configuration layer in place, you can manipulate all aspects of your application without editing a single token of PHP.

For example, changing where the above script redirects after sending an email can be configured with the following YAML:

# .explosion/contact/process_submit/4.yml
strings:
  0: 'Location: contact.php?flash=Thanks!'

Or if you would rather require_once than include_once your configuration:

# .explosion/contact/process_submit/1.yml
function: require_once

What's next?

We're very excited about the new explosion classes and the impact we anticipate this library having in the PHP and larger open source communities. Stay tuned for a beta release in the next fews days and a forthcoming series of tutorials: "Atmospheric Explosions: Scaling your explosion to the cloud," "Third time's a charm: When to re-explode your already exploded application," and "Manipulating your YAML with XML."