UK Symfony Meetup, November 2012
Original presentation: https://docs.google.com/presentation/pub?id=1PLcqTby6yqSbfWlMIDHknH852DU6DO6OAgQJOtSEdsg&start=false&loop=false&delayms=3000
1. UK Symfony Meetup
November, 2012
Symfony
War Stories
Jakub Zalas
SensioLabsUK
http://www.rgbstock.com/bigphoto/mhY2DCY
2. Jakub Zalas a.k.a. Kuba
○ Remembers symfony 1.0
(2007)
○ Open Source contributor
○ BDD advocate
○ Works at Sensio Labs UK
○ ZCE & Sensio Labs ○ Twitter: @jakub_zalas
Certified Symfony ○ Blog: http://zalas.eu
Developer ○ Github: @jakzal
3. Symfony War Stories
● Bundling the code
● Thinking outside of the bundle
● Being lazy
● Learning your stuff
● Keeping your playground clean
● Building the quality in
4. Bundling
the code
http://www.rgbstock.com/bigphoto/mfXkYUq
6. Whenever in doubt...
● Start simple and let the design emerge
● Extract bundles when you need to reuse it
or you see it would improve clarity
● Avoid two-way dependencies between
bundles
7. Thinking
outside of
the bundle
http://www.rgbstock.com/bigphoto/mfBYR2S
9. To keep your design clean...
● Treat bundles as an integration layer
between PHP libraries and the Symfony
framework
● In your libraries, avoid dependencies on
bundles
16. namespace SensioListener;
use DoctrineORMEventLifecycleEventArgs;
use SensioEntityProduct;
class SearchIndexer
{
public function postPersist(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
$entityManager = $args->getEntityManager();
if ($entity instanceof Product) { /* Index me */ }
}
}
17. Lifecycle callbacks (Doctrine)
/**
* @ORMEntity()
* @ORMHasLifecycleCallbacks()
*/
class User
{
/**
* @ORMPrePersist
*/
public function updateCreatedAt() { /* ... */ }
}
21. Data transformers (Forms)
class IssueToNumberTransformer
implements DataTransformerInterface
{
public function transform($issue)
{
// return issue number
}
public function reverseTransform($number)
{
// return Issue instance
}
}
24. class SensioExtension extends Twig_Extension
{
public function getFilters()
{
return array(
'price' => new Twig_Filter_Method(
$this, 'priceFilter')
);
}
public function getName()
{
return 'sensio_extension';
}
public function priceFilter($number, /* ... */ ) {}
}
32. public function publishAction($date)
{
$show = $this->findShow($date);
$fileName = sprintf('show-%s.json', $date->format('Y-m-d'));
while (file_exists($fileName)) {
$fileName = $fileName.preg_replace('/.json$/', '-1.json');
}
$options = array( /* options here */ );
$s3 = new AmazonS3($options);
$objectOptions = array(
'body' => json_encode($show), 'acl' => $options['objectAcl']);
if (!$s3->create_object('my-bucket', $fileName, $objectOptions) {
throw new Exception('Could not save file');
}
// ...
}
33. public function publishAction($date)
{
$show = $this->findShow($date);
$fileNameResolver = new FileNameResolver();
$filesystem = $this->get('gafrette_filesystem');
$publisher = new Publisher(
$fileNameResolver, $filesystem
);
$publisher->publish($show, $date);
}
34. Use the service container!
public function publishAction($date)
{
$show = $this->findShow($date);
$publisher = $this->get('sensio.publisher');
$publisher->publish($show, $date);
}
39. Unit tests
class ReportTest extends PHPUnit_Framework_TestCase
{
public function testThatItReturnsFilePath()
{
$report = new Report();
$file = $report->create('January');
$this->assertSame('/january.csv', $file);
}
}
40. ... or specs
namespace spec;
class Report extends PHPSpec2ObjectBehavior
{
function it_returns_file_path()
{
$this->create('January')
->shouldReturn('/january.csv');
}
}
41. Managing expectations
Feature: Viewing recent news on the homepage
As a Marketing Manager
I want Visitors to see recent news on the homepage
In order to get them interested in the content
Scenario: Viewing recent news
Given there are 10 news items
And there were 5 news items written since yesterday
When I visit the homepage
Then I should see 5 recent news items in the news section
Scenario: There is not enough news
Given there are 10 news items
But there were 3 news items written since yesterday
When I visit the homepage
Then I should see 3 recent news items in the news section