Introducing the Symfony Service Exploder

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}
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';
$e = sfExplosionFactory::getInstance();

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
  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."

Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.


Where can I get this code from? I've been looking for this kind of thing for ages! I assume I can hook this in with sfFluxCapacitor providing I can find 1.21 jigawatts?
How can I integrate these exploded forms with a captcha? Is there a way to finally have an easy way to validate agains captchas? I am scratching my head for 2 years already, the Service Exploder looks like a solution to my headaches!
Wow - how much coffee are you guys drinking these day? Keep it up - I can't wait to read more.
@pookey +1
Pure genius :)
"Manipulating your YAML with XML." haha
Some peoples really have a lot of imagination for April 1st :)
pure nerdish april fool jokes, you can't mess like that with the average john doe
Ah ah ah nice easter egg tutorial :)
@James guilty as charged
haha @kris, but you got me for about 5 minutes there, i was telling myself "well, i usually don't read the blog because it's too complicated for me but this time i'll give it a try... but then, i tought, what's the point of all that?"
Sweet. I've been looking for a way to replace all of my PHP with YAML. Then, I can transform the YAML to XML, then use an XSD to turn the XML to JSON. Then I'll be able to program my PHP in Javascript! This is real progress man!
You are really crazy.
I love you!!

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.