Introducing symfony2

download Introducing symfony2

of 41

  • date post

    08-May-2015
  • Category

    Technology

  • view

    2.175
  • download

    1

Embed Size (px)

description

The slides of the presentation "Introducing Symfony2" given at the Drupal Dev Days 2012 in Barcelona.

Transcript of Introducing symfony2

  • 1.IntroducingSaturday, June 16, 12

2. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Allow me to introduce myselfClaudio Beatrice6+ years of@omissis experience on PHPOrganizing Drupalevents since 2009 PHP, Drupal & Symfony consulting Web Radio Telecommunications 2012 Agavee GmbHSaturday, June 16, 12 3. Drupal Developer Days 2012 Barcelona - Introducing Symfony2 Whats Symfony2A reusable set of standalone, decoupled,and cohesive PHP 5.3 componentsA full-stack web frameworkA Request/Response framework builtaround the HTTP specication A promoter of best practices,standardization and interoperabilityAn awesome community! 2012 Agavee GmbHSaturday, June 16, 12 4. Drupal Developer Days 2012 Barcelona - Introducing Symfony2 Leave The STUPID AloneAs time went by, habits and practices that once seemedacceptable have proven that they were making our codeharder to understand and maintain. In other words, STUPID.But what makes code such a thing? Singleton Tight coupling Untestability Premature optimization Indescriptive naming Duplication 2012 Agavee GmbHSaturday, June 16, 12 5. Drupal Developer Days 2012 Barcelona - Introducing Symfony2SingletonIts a design pattern that restricts the creation of an objectto one instance(think of a DB connection).It does introduce undesirable limitations(what if well needTWO DB connections?), global state and hardcodeddependencies which are all making code more difcult totest and more coupled. 2012 Agavee GmbHSaturday, June 16, 12 6. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Singletonclass DB{private static $instance;private function __construct(){// ... code goes here ...}public static function getInstance(){if (empty(self::$instance)) {self::$instance = new self;}return self::$instance;}// ... more code goes here ...} 2012 Agavee GmbHSaturday, June 16, 12 7. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Tight CouplingIt happens when classes are put in relation by using typehints, static calls or direct instantiation. Introduces hardcoded dependencies between classes, which complicates: code reuse unit testing integration modications 2012 Agavee GmbHSaturday, June 16, 12 8. Drupal Developer Days 2012 Barcelona - Introducing Symfony2 Tight Coupling // An example of tight coupling class House { public function __construct() { $this->door = new Door(); $this->window = new Window(); } } // And a possible solution class House { // Door and Window are interfaces public function __construct(Door $door, Window $window) { $this->door = $door; $this->window = $window; } } 2012 Agavee GmbHSaturday, June 16, 12 9. Drupal Developer Days 2012 Barcelona - Introducing Symfony2UntestabilityIf classes are complex, tightly coupled or trying to do toomuch, then theres a good chance that its also quite hard ifnot impossible to test it in isolation.The lack of proper test coverage will make code harder tomaintain and change, as it becomes very difcult to tell ifany modication is actually breaking something. 2012 Agavee GmbHSaturday, June 16, 12 10. Drupal Developer Days 2012 Barcelona - Introducing Symfony2 Premature Optimization Most of the time major performance issues are caused by small portions of code(80/20 rule). It is easier to optimize correct code than to correct optimized code.Performance are not always a concern, thereforeoptimize when its a proved problem, youll save timeand raise productivity. 2012 Agavee GmbHSaturday, June 16, 12 11. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Indescriptive Naming There are two hard things in computer science: cache invalidation, naming things and off-by-one errors.-- Phil Karlton, variated by the InterwebsEven if hard, naming is a fundamental part of the job andshould be considered part of the documentation, thereforeremember to: communicate intents favor clarity over brevity think that code is read far more often than written, soits more convenient to ease reads over writes 2012 Agavee GmbHSaturday, June 16, 12 12. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Duplication How many times did they tell you to not repeat yourself? 2012 Agavee GmbHSaturday, June 16, 12 13. Drupal Developer Days 2012 Barcelona - Introducing Symfony2BE SOLID!What alternatives to write STUPID code do we have?Another acronym to the rescue: SOLID!It encloses ve class design principles: Single responsibility principle Open/closed principle Liskov substitution principle Interface segregation principle Dependency inversion principle 2012 Agavee GmbHSaturday, June 16, 12 14. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Single ResponsibilityThere should never be more thanone reason for a class to change.Every class should have a single responsibility and fullyencapsulate it.If change becomes localized, complexity and cost ofchange are reduced, moreover theres less risk of rippleeffects. 2012 Agavee GmbHSaturday, June 16, 12 15. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Single Responsibilityinterface Modem{function dial($phoneNumber);function hangup();function send($message);function receive();}The above interface shows two responsibilities: connectionmanagement and data communication, making them goodcandidates for two separate interfaces/implementations. 2012 Agavee GmbHSaturday, June 16, 12 16. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Open/Closed Software entities (classes, functions, etc) should be open for extension, but closed for modication. This principle states that the source code of software entities shouldnt ever be changed: those entities must be derived in order to add the wanted behaviors.Abstract Client Server Client Server Server 2012 Agavee GmbHSaturday, June 16, 12 17. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Liskov SubstitutionObjects in a program should be replaceable withinstances of their subtypes without altering any of thedesirable properties of that program, such as correctnessand performed task.It intends to guarantee semantic interoperability of objecttypes in a hierarchy. 2012 Agavee GmbHSaturday, June 16, 12 18. Drupal Developer Days 2012 Barcelona - Introducing Symfony2 Liskov Substitution class Rectangle { protected $width; protected $height; function setWidth($width) {...}function draw(Rectangle $r) { function getWidth() {...}$r->setWidth(5); function setHeight($height) {...}$r->setHeight(4); function getHeight() {...} }// is it correct to assume thatchanging the width of a Rectangle class Square extends Rectangle { leaves is height unchanged? function setWidth($width) {assertEquals( $this->width = $width;20, $this->height = $width; $r->setWidth() * $r->setHeight() }); function setHeight($height) {} $this->width= $height; $this->height = $height; } } 2012 Agavee GmbHSaturday, June 16, 12 19. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Liskov SubstitutionThe aw in the Rectangle-Square design shows that evenif conceptually a square is a rectangle, a Square object isnot a Rectangle object, since a Square does not behaveas a Rectangle.As a result, the public behavior the clients expect for thebase class must be preserved in order to conform to theLSP. 2012 Agavee GmbHSaturday, June 16, 12 20. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Interface Segregation Many client-specic interfaces are better than one big one. This principle helps decreasing the coupling between objects by minimizing the intersecting surface. interface Printer { function print(...); }interface MultiFunctionPrinter{interface Scannerfunction print(...); {function scan(...);function print(...);function fax(...); }} interface Fax { function print(...); } 2012 Agavee GmbHSaturday, June 16, 12 21. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Dependency Inversion High-level entities should not depend on low-level entitiesand both should depend on abstractions. Abstractions should not depend upon details: detailsshould depend upon abstractions.Component A Component AService Component A PackageComponent B Component B Package 2012 Agavee GmbHSaturday, June 16, 12 22. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Dependency Inversionclass Vehicle {protected $tyres;public function __construct() {$this->tyres = array_fill(0, 4, new Tyre(50));}}class Tyre {private $diameter;public function __construct($diameter) {$this->setDiameter($diameter);}public function setDiameter($diameter) {$this->diameter = $diameter;}public function getDiameter() {return $this->diameter;}} 2012 Agavee GmbHSaturday, June 16, 12 23. Drupal Developer Days 2012 Barcelona - Introducing Symfony2Dependency Inversion namespace Vehicle;namespace Vehicle; class Vehicle { abstract class AbstractTyre { protected $tyres; private $diameter; function addTyre(AbstractTyre $tyre) {function __construct($diameter) {...} $this->tyres[] = $tyre; } function setDiameter($diameter) {...} } function getDiameter() {...} }namespace Tyre;use VehicleAbstractTyre;class RaceTyre extends AbstractTyre {private $compound;function setCompound($compound) {...}function getCompound() {...}} 2012 Agavee GmbHSaturday, June 16, 12 24. Drupal Developer Days 2012 Barcelona - Introducing Symfony2How About Symfony Now?Being aware of the principles of software developmentmentioned earlier allow us to better understand some of thechoices that have been made for the framework as well assome of the tools that have been made available, such as: Class Loader Service Container Event Dispatcher HTTP Foundation HTTP Kernel 2012 Agavee GmbHSaturday, June 16, 12 25. Drupal Developer Days 2012 Barcelona - Introducing Symfony2 Class LoaderIt loads your projects classes automatically if theyrefollowing a standard PHP convention aka PSR-0. DoctrineCommonIsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php SymfonyCoreRequest => /path/to/project/lib/vendor/Sym