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

A Tour Through the Symfony Ecosystem

A Tour Through the Symfony Ecosystem

Presented June 25, 2014 at NYC eZ Publish meetup: https://joind.in/talk/view/10377

Reveal.js presentation published at: http://jmikola.github.io/slides/symfony_ecosystem/

F23700b51dc0c196c1dc02f84aeeecdf?s=128

Jeremy Mikola

June 25, 2014
Tweet

Transcript

  1. A Tour Through the Symfony Ecosystem Jeremy Mikola jmikola

  2. Composer

  3. Composer PHP’s package manager Pull external libraries into any project

    Handles downloading and autoloading Works well with Package repositories , , semantic versioning Packagist Satis Toran Proxy
  4. [ h o n e y d e w :

    n e w - p r o j e c t ] $ c o m p o s e r C o m p o s e r v e r s i o n 2 8 c 6 5 b 5 4 2 5 f 7 f 8 7 2 2 c 2 6 0 e 3 7 8 7 b 7 4 c 1 e a 6 b 3 7 e 3 b 2 0 1 4 - 0 6 - 2 2 1 8 : 2 4 : 2 8 U s a g e : [ o p t i o n s ] c o m m a n d [ a r g u m e n t s ] O p t i o n s : - - h e l p - h D i s p l a y t h i s h e l p m e s s a g e . - - q u i e t - q D o n o t o u t p u t a n y m e s s a g e . - - v e r b o s e - v | v v | v v v I n c r e a s e t h e v e r b o s i t y o f m e s s a g e s - - v e r s i o n - V D i s p l a y t h i s a p p l i c a t i o n v e r s i o n . - - a n s i F o r c e A N S I o u t p u t . - - n o - a n s i D i s a b l e A N S I o u t p u t . - - n o - i n t e r a c t i o n - n D o n o t a s k a n y i n t e r a c t i v e q u e s t i o n . - - p r o f i l e D i s p l a y t i m i n g a n d m e m o r y u s a g e i n f o r m a t i o n - - w o r k i n g - d i r - d I f s p e c i f i e d , u s e t h e g i v e n d i r e c t o r y a s w o r k i n g d i r e c t o r y . A v a i l a b l e c o m m a n d s : a b o u t S h o r t i n f o r m a t i o n a b o u t C o m p o s e r a r c h i v e C r e a t e a n a r c h i v e o f t h i s c o m p o s e r p a c k a g e c o n f i g S e t c o n f i g o p t i o n s c r e a t e - p r o j e c t C r e a t e n e w p r o j e c t f r o m a p a c k a g e i n t o g i v e n d i r e c t o r y . d e p e n d s S h o w s w h i c h p a c k a g e s d e p e n d o n t h e g i v e n p a c k a g e d i a g n o s e D i a g n o s e s t h e s y s t e m t o i d e n t i f y c o m m o n e r r o r s . d u m p - a u t o l o a d D u m p s t h e a u t o l o a d e r g l o b a l A l l o w s r u n n i n g c o m m a n d s i n t h e g l o b a l c o m p o s e r d i r ( $ C O M P O S E R _ H O M E ) . h e l p D i s p l a y s h e l p f o r a c o m m a n d i n i t C r e a t e s a b a s i c c o m p o s e r . j s o n f i l e i n c u r r e n t d i r e c t o r y . i n s t a l l I n s t a l l s t h e p r o j e c t d e p e n d e n c i e s l i c e n s e s S h o w i n f o r m a t i o n a b o u t l i c e n s e s o f d e p e n d e n c i e s l i s t L i s t s c o m m a n d s r e q u i r e A d d s r e q u i r e d p a c k a g e s t o y o u r c o m p o s e r . j s o n a n d i n s t a l l s t h e m r u n - s c r i p t R u n t h e s c r i p t s d e f i n e d i n c o m p o s e r . j s o n . s e a r c h S e a r c h f o r p a c k a g e s s e l f - u p d a t e U p d a t e s c o m p o s e r . p h a r t o t h e l a t e s t v e r s i o n . s h o w S h o w i n f o r m a t i o n a b o u t p a c k a g e s s t a t u s S h o w a l i s t o f l o c a l l y m o d i f i e d p a c k a g e s u p d a t e U p d a t e s y o u r d e p e n d e n c i e s t o t h e l a t e s t v e r s i o n v a l i d a t e V a l i d a t e s a c o m p o s e r . j s o n
  5. Installing a Library [ h o n e y d

    e w : n e w - p r o j e c t ] $ c o m p o s e r r e q u i r e d o c t r i n e / m o n g o d b = ~ 1 . 1 . / c o m p o s e r . j s o n h a s b e e n u p d a t e d L o a d i n g c o m p o s e r r e p o s i t o r i e s w i t h p a c k a g e i n f o r m a t i o n U p d a t i n g d e p e n d e n c i e s ( i n c l u d i n g r e q u i r e - d e v ) - I n s t a l l i n g d o c t r i n e / l e x e r ( v 1 . 0 ) D o w n l o a d i n g : 1 0 0 % - I n s t a l l i n g d o c t r i n e / a n n o t a t i o n s ( v 1 . 1 . 2 ) D o w n l o a d i n g : 1 0 0 % - I n s t a l l i n g d o c t r i n e / c o l l e c t i o n s ( v 1 . 2 ) D o w n l o a d i n g : 1 0 0 % - I n s t a l l i n g d o c t r i n e / c a c h e ( v 1 . 3 . 0 ) D o w n l o a d i n g : 1 0 0 % - I n s t a l l i n g d o c t r i n e / i n f l e c t o r ( v 1 . 0 ) D o w n l o a d i n g : 1 0 0 % - I n s t a l l i n g d o c t r i n e / c o m m o n ( v 2 . 4 . 2 ) D o w n l o a d i n g : 1 0 0 % - I n s t a l l i n g d o c t r i n e / m o n g o d b ( 1 . 1 . 6 ) D o w n l o a d i n g : 1 0 0 % d o c t r i n e / m o n g o d b s u g g e s t s i n s t a l l i n g j m i k o l a / g e o j s o n ( S u p p o r t G e o J S O N i n 2 d s p h e r e q u e r i e s ) W r i t i n g l o c k f i l e G e n e r a t i n g a u t o l o a d f i l e s
  6. Directory Structure [ h o n e y d e

    w : n e w - p r o j e c t ] $ t r e e - L 3 . ├ ─ ─ c o m p o s e r . j s o n ├ ─ ─ c o m p o s e r . l o c k └ ─ ─ v e n d o r ├ ─ ─ a u t o l o a d . p h p ├ ─ ─ c o m p o s e r │ ├ ─ ─ a u t o l o a d _ c l a s s m a p . p h p │ ├ ─ ─ a u t o l o a d _ n a m e s p a c e s . p h p │ ├ ─ ─ a u t o l o a d _ p s r 4 . p h p │ ├ ─ ─ a u t o l o a d _ r e a l . p h p │ ├ ─ ─ C l a s s L o a d e r . p h p │ └ ─ ─ i n s t a l l e d . j s o n └ ─ ─ d o c t r i n e ├ ─ ─ a n n o t a t i o n s ├ ─ ─ c a c h e ├ ─ ─ c o l l e c t i o n s ├ ─ ─ c o m m o n ├ ─ ─ i n f l e c t o r ├ ─ ─ l e x e r └ ─ ─ m o n g o d b [ h o n e y d e w : n e w - p r o j e c t ] $ c a t c o m p o s e r . j s o n { " r e q u i r e " : { " d o c t r i n e / m o n g o d b " : " ~ 1 . 1 " } }
  7. Doctrine MongoDB { " n a m e " :

    " d o c t r i n e / m o n g o d b " , " t y p e " : " l i b r a r y " , " r e q u i r e " : { " p h p " : " > = 5 . 3 . 2 " , " e x t - m o n g o " : " > = 1 . 2 . 1 2 , < 1 . 6 - d e v " , " d o c t r i n e / c o m m o n " : " > = 2 . 1 . 0 , < 2 . 5 - d e v " } , " s u g g e s t " : { " j m i k o l a / g e o j s o n " : " S u p p o r t G e o J S O N i n 2 d s p h e r e q u e r i e s " } , " a u t o l o a d " : { " p s r - 0 " : { " D o c t r i n e \ \ M o n g o D B " : " l i b / " } } , " e x t r a " : { " b r a n c h - a l i a s " : { " d e v - m a s t e r " : " 1 . 2 . x - d e v " } } }
  8. Components First, Framework Second

  9. Components DependencyInjection EventDispatcher HttpFoundation, HttpKernel Routing, Security, Templating Console, Form,

    Validator There are many more!
  10. Components Are… Single-purpose Decoupled libraries Well-tested Full of interfaces Framework-agnostic

  11. Component Finder $ f i n d e r =

    n e w S y m f o n y \ C o m p o n e n t \ F i n d e r \ F i n d e r ( ) ; $ f i n d e r - > f i l e s ( ) - > n a m e ( ' * . p h p ' ) - > d e p t h ( 0 ) - > s i z e ( ' > = 1 K ' ) - > i n ( _ _ D I R _ _ ) ; f o r e a c h ( $ f i n d e r a s $ f i l e ) { p r i n t f ( " % s \ n " , $ f i l e - > g e t R e a l p a t h ( ) ) ; }
  12. Component Finder $ s 3 = n e w Z

    e n d _ S e r v i c e _ A m a z o n _ S 3 ( $ k e y , $ s e c r e t ) ; $ s 3 - > r e g i s t e r S t r e a m W r a p p e r ( ' s 3 ' ) ; $ f i n d e r = n e w S y m f o n y \ C o m p o n e n t \ F i n d e r \ F i n d e r ( ) ; $ f i n d e r - > f i l e s ( ) - > n a m e ( ' p h o t o s * ' ) - > s i z e ( ' < 1 0 0 K ' ) - > d a t e ( ' s i n c e 1 h o u r a g o ' ) - > i n ( ' s 3 : / / b u c k e t - n a m e ' ) ; f o r e a c h ( $ f i n d e r a s $ f i l e ) { p r i n t f ( " % s \ n " , $ f i l e - > g e t F i l e n a m e ( ) ) ; }
  13. Serving an HTTP Request

  14. HTTP Request Flow Component Teamwork

  15. HttpKernel Request → Kernel → Response Security Routing Controller Templating

    {
  16. Routing Configuration URL patterns with path params Optional param requirements

    Controller logical name or service First match wins b l o g _ s h o w : p a t h : / b l o g / { s l u g } d e f a u l t s : { _ c o n t r o l l e r : D e m o B u n d l e : B l o g : s h o w } m e t h o d s : [ G E T ] a r t i c l e _ s h o w : p a t h : / a r t i c l e s / { c u l t u r e } / { y e a r } / { t i t l e } . { _ f o r m a t } d e f a u l t s : { _ c o n t r o l l e r : D e m o B u n d l e : A r t i c l e : s h o w , _ f o r m a t : h t m l } r e q u i r e m e n t s : _ f o r m a t : h t m l | r s s c u l t u r e : e n | f r y e a r : \ d +
  17. Controllers Receive params or a Request Process a form or

    page logic Use services for heavy lifting Optionally render a template Return a Response (or content)
  18. Components to Bundles to Full Stack

  19. What’s in a Bundle? Configuration Service definitions Controllers Templates and

    resources Console commands
  20. Core Bundles FrameworkBundle SecurityBundle TwigBundle WebProfilerBundle

  21. FrameworkBundle { " n a m e " : "

    s y m f o n y / f r a m e w o r k - b u n d l e " , " t y p e " : " s y m f o n y - b u n d l e " , " r e q u i r e " : { " p h p " : " > = 5 . 3 . 3 " , " s y m f o n y / d e p e n d e n c y - i n j e c t i o n " : " ~ 2 . 2 " , " s y m f o n y / c o n f i g " : " ~ 2 . 4 " , " s y m f o n y / e v e n t - d i s p a t c h e r " : " ~ 2 . 5 " , " s y m f o n y / h t t p - f o u n d a t i o n " : " ~ 2 . 4 " , " s y m f o n y / h t t p - k e r n e l " : " ~ 2 . 6 " , " s y m f o n y / f i l e s y s t e m " : " ~ 2 . 3 " , " s y m f o n y / r o u t i n g " : " ~ 2 . 2 " , " s y m f o n y / s e c u r i t y - c o r e " : " ~ 2 . 4 " , " s y m f o n y / s e c u r i t y - c s r f " : " ~ 2 . 4 " , " s y m f o n y / s t o p w a t c h " : " ~ 2 . 3 " , " s y m f o n y / t e m p l a t i n g " : " ~ 2 . 1 " , " s y m f o n y / t r a n s l a t i o n " : " ~ 2 . 3 " , " d o c t r i n e / a n n o t a t i o n s " : " ~ 1 . 0 " } , " s u g g e s t " : { " s y m f o n y / c o n s o l e " : " F o r u s i n g t h e c o n s o l e c o m m a n d s " , " s y m f o n y / f i n d e r " : " F o r u s i n g t h e t r a n s l a t i o n l o a d e r a n d c a c h e w a r m e r " , " s y m f o n y / f o r m " : " F o r u s i n g f o r m s " , " s y m f o n y / v a l i d a t o r " : " F o r u s i n g v a l i d a t i o n " , " d o c t r i n e / c a c h e " : " F o r u s i n g a l t e r n a t i v e c a c h e d r i v e r s " } }
  22. Bundles + Components = Applications Don’t reinvent the wheel: KnpBundles.com

  23. Twig You’ll love it. Your frontend devs will, too!

  24. Twig Templates Flexible property access Intuitive control structures Expressions, filters,

    macros Totally extensible < ! D O C T Y P E h t m l > < h t m l > < h e a d > < t i t l e > M y W e b p a g e < / t i t l e > < / h e a d > < b o d y > < u l i d = " n a v i g a t i o n " > { % f o r i t e m i n n a v i g a t i o n % } < l i > < a h r e f = " { { i t e m . h r e f } } " > { { i t e m . c a p t i o n } } < / a > < / l i > { % e l s e % } < l i > N o t h i n g t o s e e h e r e ! < / l i > { % e n d f o r % } < / u l > < p > T h e c u r r e n t t i m e i s { { " n o w " | d a t e ( " d / m / Y H : i " ) } } . < / p > < / b o d y > < / h t m l >
  25. Silex A micro-framework from components.

  26. Silex in a Nutshell Service container Just a few components

    Extensions up the wazoo HttpFoundation, HttpKernel EventDispatcher, Routing Cache, Doctrine, Session, Security Form, Logging, Serializer, Twig r e q u i r e _ o n c e _ _ D I R _ _ . ' / . . / v e n d o r / a u t o l o a d . p h p ' ; $ a p p = n e w S i l e x \ A p p l i c a t i o n ( ) ; $ a p p - > g e t ( ' / h e l l o / { n a m e } ' , f u n c t i o n ( $ n a m e ) u s e ( $ a p p ) { r e t u r n ' H e l l o , ' . $ a p p - > e s c a p e ( $ n a m e ) ; } ) ; $ a p p - > r u n ( ) ;
  27. You’re in Good Company

  28. Thanks! Any Questions? assembled most of this content The is

    chock full of good stuff has PHP and Symfony tutorials Ryan Weaver Symfony Book KnpUniversity
  29. Image Credits http://www.cfa.arizona.edu/symphony/images/ArizonaSymphony2011aerialSerrano_000.jpg