Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Inspire yourself of Symfony2 to create better code

Inspire yourself of Symfony2 to create better code

Symfony2 is now released and, as the first version of symfony, it's a framework that advocates a lot of best practices. Any developer who works on a Symfony2 application should follow the same rules, in order to keep the project on the right tracks. With the help of Symfony2's code and my personal experience, I will try in this session to bend your mind to some principles like Separation of Concerns and pragmatism, in order to achieve successful projects.

Marc Weistroff

October 21, 2011
Tweet

More Decks by Marc Weistroff

Other Decks in Programming

Transcript

  1. “In computer science, separation of concerns (SoC) is the process

    of separating a computer program into distinct features that overlap in functionality as little as possible.” -- Wikipedia
  2. “A concern is any piece of interest or focus in

    a program. Typically, concerns are synonymous with features or behaviors.” -- Wikipedia
  3. Examples of SoC HTML / CSS –  Separating content from

    styling Internet Protocol Stack – HTTP or SMTP doesn’t care about TCP, nor IP Place where you eat / place where you shit – Don’t mix it up, it’s dirty and can be hazardous
  4. Controller View Model The Controller analyses the user request, calls

    the Model and passes data to the View. The View layer formats data in a dedicated format (html, json…) The Model stores the business logic and classes that manipulate data. Request MVC Pattern
  5. Bridges •  Bridges couple third party libraries with Symfony components.

    •  External to the component and the 3rd party library •  Prevent hard-coupling between component and third-party library.
  6. HttpFoundation •  Implements the HTTP RFC •  Dedicated objects for

    – Request – Response / RedirectResponse – Session – SessionStorage – HeaderBag – ResponseHeaderBag
  7. Pragmatic approach •  Simple & effective •  Download the project

    and start immediatly •  Oriented toward the real world, not purity for the sake of purity.
  8. Pragmatic approach •  No magic = No WTF effect • 

    No magic = Keeping control •  Almost always explicit
  9. Pragmatic approach for your project •  Make a prototype that

    works, and improve it step by step. •  Refactor early, refactor often. •  THINK constantly about what you’re doing and why you’re doing it
  10. Coding standards •  Symfony come with it’s own coding standards

    •  Coming from another framework ? – It can be weird at first time, but you’ll get used to it really quickly
  11. Design by contract 4 137 0   20   40

      60   80   100   120   140   160   symfony  1.4   Symfony2   Interfaces  
  12. Contract •  Rights & Responsibilities of an object •  Interfaces

    are the contracts of OOP •  No surprises •  You can replace an interfaced object with your own implementation, without breaking anything
  13. Tests •  Symfony2 is heavily tested •  PHPUnit •  ~

    5000 tests, 13000 assertions •  Code coverage – Components : 84% – Bundles: 58% – Bridges: 56%
  14. The wheel, not reinvented •  Rock solid HTTP implementation • 

    Edge Side Include (ESI) •  HTTP Cache, no applicative cache – Did not reinvent the wheel !
  15. No singleton and DIC •  Uses DIC •  No singleton

    = better testability thanks to the absence of hard-coupling •  Lazy loading •  Reentrancy
  16. Interfaced exceptions •  Not present in all components – DependencyInjection, Routing,

    Serializer, Yaml •  Keeps semantic of the 13 SPL’s exceptions •  Makes it easier to catch exception of a component/library
  17. // src/Symfony/Component/Yaml/Exception/ExceptionInterface.php! namespace Symfony\Component\Yaml\Exception;! ! interface ExceptionInterface! {! }! !

    ! ! // src/Symfony/Component/Yaml/Exception/DumpException.php! namespace Symfony\Component\Yaml\Exception;! ! class DumpException extends \RuntimeException implements ExceptionInterface! {! }
  18. OpenSource cross polination •  Benefit from the work of hundreds

    of people •  Security Component comes from Spring (Java) •  Monolog was inspired by logbook (python)
  19. OpenSource cross polination •  Assetic is based on webassets (python)

    •  Composer is a port of the SAT solver algorithm of openSUSE’s libzypp (c) •  PHPCR, port of JCR
  20. and… IT WORKS ! •  Already used in hundreds of

    websites worldwide: – symfony.com – lockerz.com – exercise.com – Opensky.com
  21. New project comes in •  User account platform •  SSO

    or webapp can share informations with other accepted webapps •  API for third party websites •  SDK for the API •  Other connected webapps can be done in any php frameworks or languages.
  22. OAuth2 •  RFC is ~40 pages long. •  OAuth2 is

    a complicated subject with a lot of HTTP workflows •  Do you really want to start from scratch?
  23. How to choose? •  Search for other libraries in ruby

    / php / python / whatever •  Is the developper well known? •  Is the company behind renowned? •  Is the library well tested? •  Follow your guts!
  24. Porting •  Porting OAuth2 library from ruby was complicated. – RAKE

    middleware to Symfony2 architecture – I had no knowledge of ruby
  25. How to port? •  Port only what you need – Port

    only the things you need – Make it works – Repeat. •  Port the whole thing in one time
  26. But be pragmatic •  Do not try to immediately release

    the port as open source •  Make it work for your project, and maybe one day, open source it when you’ll be happy of it
  27. OAuth2   Provider   Consumer   (Symfony)   Consumer  

    (Silex)   Consumer   (ZF)   OAuth2 consumers
  28. Think independent •  Do you want to tie your SDK

    to a Framework? •  SDK is framework-independent library •  Only dependency is Buzz (because it’s great) •  Only after that, integrate it into framework – Silex service provider – Symfony2 bundle – symfony 1.x plugin