Archives


Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

gravatar
Introducing the Symfony Service Exploder
by Kris Wallsmith – April 01, 2009 – 12 comments

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

Comments RSS

  • gravatar
    #1 Ian P. Christian (pookey) said on the 2009/04/01 at 19:43
    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?
  • gravatar
    #2 cprior said on the 2009/04/01 at 19:53
    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!
  • gravatar
    #3 Ryan Weaver said on the 2009/04/01 at 20:03
    Wow - how much coffee are you guys drinking these day? Keep it up - I can't wait to read more.
  • gravatar
    #4 jjkaye said on the 2009/04/01 at 20:03
    @pookey +1
  • gravatar
    #5 Chris Sedlmayr (catchamonkey) said on the 2009/04/01 at 21:38
    Pure genius :)
    "Manipulating your YAML with XML." haha
  • gravatar
    #6 ghis said on the 2009/04/01 at 21:56
    Some peoples really have a lot of imagination for April 1st :)
  • gravatar
    #7 James said on the 2009/04/01 at 21:59
    pure nerdish april fool jokes, you can't mess like that with the average john doe
  • gravatar
    #8 Hugo HAMON said on the 2009/04/01 at 22:22
    Ah ah ah nice easter egg tutorial :)
  • gravatar
    #9 Kris said on the 2009/04/01 at 22:22
    @James guilty as charged
  • gravatar
    #10 James said on the 2009/04/01 at 23:04
    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?"
  • gravatar
    #11 Ian Taylor said on the 2009/04/01 at 23:45
    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!
  • gravatar
    #12 Carlos Jacobs said on the 2009/04/06 at 14:10
    You are really crazy.
    I love you!!