Using Symfony Components for Fun and Profit Sam
Hi, I'm Sam.
  • Tech Lead @ Flint Interactive.
  • Open source developer, Symfony contributor & enthusiast.
  • 8 years PHP & web commercial experience.
  • #melbsf2 group co-organiser.
First, a quick case-study
  • In June, following an office server meltdown, we decided to move from SVN to Git in the cloud.
  • We had ~350 non-archived (active) projects that needed to be moved.
  • Many of our projects were multi-faceted.
  • Many hinged off central libraries where the dependencies were stored.
  • Multiple actions needed to take place to migrate each project.
  • Project migration needed to be coordinated & prioritised to not mess up team of 8+ developers.
  • This was a more complex operation than a shell script or single-file PHP script could take care of.
this is a very common scenario
our needs were too complex for a simple script
using a fully fledged web framework would be overkill
we actually needed many of the things that a framework provides
(e.g. command-line utils, database manipulation, process controls, HTTP/Curl wrappers)
how do you (and did we) overcome this type of challenge without succumbing to the bloat of a full fledged framework?
Symfony components to the rescue!
Symfony provides many of the core features of the framework as stand-alone components, that you can use with little-to-no dependencies.
These components are very flexible, and can be applied to quickly solve many common tasks in programming.
The Console component
Exactly the same as what ships with the framework, allows you to easily expose application functions on the command-line.

add in an implementation of SomeCommand,
and viola. we now have a fully functioning command-line app

The Finder component
A little-known component that is used extensively in the framework's internals, to help you find and traverse files.

Now our application will find all PHP files in the Command directory matching pattern *Command.php.
Note: this is actaully very similar to how the framework finds Command classes in your bundles!

The Process component
Running other applications from your app, and getting and interpreting the result can be hard. Even more challenging if the command runs for a long period of time and you need to display progress to the user.

This will execute svn2git $svnHref and output any output as it comes from the command directly to the screen.
Processes can also execute asynchronously, or execute sandboxed PHP code. Check out the manual for more info:
(symfony.com/doc/.../process.html#running-processes-asynchronously)

The PropertyAccess component
A relatively new stand-alone component designed to help you quickly get results out of an object graph.
Can be used to solve the age-old hydrate-one-object-from-another issue, or to quickly create limited data for access in a view.

This simple script creates a nested array set of only the data you're allowing to be exposed.

This can be very useful for creating "safe" versions of ORM data, or in an advanced usage, as it allows you to traverse through arrays or objects in the structure, you can use it to get a simple set of values from a complex object/array (handy for generating reports).

Symfony internally uses this in translating information from your object through to Form values, and back again.

  • HTTP Foundation: The Symfony request/response, session and cookie management library. Very helpful when dealing with actual web requests.
  • Routing: For any DIY framework that uses a front controller and needs to route the request to a class/function/"controller".
  • Config: Very helpful series of classes to deal with accepting configuration for your application. Bonus usage: Can be used to validate external data or data from an API meets what you're expecting (as it so often doesn't).
  • Event Dispatcher: Implementation of the Observer programming pattern. Very helpful for being able to inform an unknown part of your application when certain things have occurred (aka 'hooks').
  • DOM Crawler & CSS Selector: Helps you parse and extract content from X/HTML. Very useful for functional testing. Internally we're implementing it as a crawler to ensure that projects include standard things like Google analytics, etc.
  • Yaml: Originally a Symfony1 component, designed to parse Yaml configuration/data into an array structure.
  • and so many more.. make sure you check them out: symfony.com/components.
Thanks for listening to my presentation!
I hope you enjoyed it, please feel free to tweet me @sammyjarrett.
Melbourne Symfony2 Developers Group Let's reboot this bizzatch! Stay tuned for the next meetup!
Oh, and..
Presentations welcome!