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

Drupal VM, Meet Symfony Console

Drupal VM, Meet Symfony Console

Oliver Davies

July 24, 2016
Tweet

More Decks by Oliver Davies

Other Decks in Technology

Transcript

  1. Oliver Davies (opdavies) Senior Drupal Developer for Appnovation Symfony hobbyist

    Drupal VM user, Drupal VM Generator maintainer Drupal Bristol organiser, PHPSW co- organiser, DrupalCamp committee member
  2. About Drupal VM Virtual machine for Drupal development Developed and

    maintained by Jeff Geerling Vagrant, Ansible Con gured via YAML les Customisable
  3. Using Drupal VM (< 3.0) Download the project Copy example.con

    g.yml to con g.yml Edit values Start the VM
  4. Using Drupal VM (>= 3.0) Download the project default.con g.yml

    contains default values Make con g.yml if needed and override values Start the VM
  5. About the Drupal VM Generator Symfony application Twig Generates minimal,

    use-case speci c con guration les http://bit.ly/announcing-drupal-vm-generator
  6. Using Drupal VM (>= 3.0) Download the project default.con g.yml

    contains default values Make con g.yml if needed and override values Start the VM
  7. Drupal VM Generator Example 2 d r u p a

    l v m c o n f i g : g e n e r a t e \ - - m a c h i n e - n a m e = " d r u p a l b r i s t o l " \ - - h o s t n a m e = " d r u p a l b r i s t o l . l " \ - - i p - a d d r e s s = " 1 9 2 . 1 6 8 . 8 8 . 8 8 " \ - - c p u s = " 1 " - - m e m o r y = " 5 1 2 " \ - - w e b s e r v e r = " n g i n x " - - d r u p a l - v e r s i o n = " 8 " \ - - d a t a b a s e - n a m e = " d r u p a l " - - d a t a b a s e - u s e r = " d r u p a l " \ - - d a t a b a s e - p a s s w o r d = " d r u p a l " - - b u i l d - m a k e f i l e = f a l s e \ . . .
  8. The Console component eases the creation of beautiful and testable

    command line interfaces. The Console component allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs. http://symfony.com/doc/current/components/console/introduction.html
  9. Installation $ c o m p o s e r

    r e q u i r e s y m f o n y / c o n s o l e $ c o m p o s e r i n s t a l l
  10. Installation (cont) # c o m p o s e

    r . j s o n " r e q u i r e " : { " s y m f o n y / c o n s o l e " : " ^ 3 . 1 " }
  11. Installation (cont) # a p p . p h p

    r e q u i r e _ _ D I R _ _ . ' / v e n d o r / a u t o l o a d . p h p ' ; / / D o s t u f f .
  12. Steps Download the Console component Add an "entry point" Con

    gure and run an A p p l i c a t i o n class Add new Commands
  13. bin/drupalcamp # ! / u s r / b i

    n / e n v p h p r e q u i r e _ _ D I R _ _ . ' / d r u p a l c a m p . p h p ' ;
  14. bin/drupalcamp.php r e q u i r e _ _

    D I R _ _ . ' / . . / v e n d o r / a u t o l o a d . p h p ' ; u s e S y m f o n y \ C o m p o n e n t \ C o n s o l e \ A p p l i c a t i o n ; $ a p p l i c a t i o n = n e w A p p l i c a t i o n ( ) ; $ a p p l i c a t i o n - > r u n ( ) ;
  15. bin/drupalcamp.php (cont) r e q u i r e _

    _ D I R _ _ . ' / . . / v e n d o r / a u t o l o a d . p h p ' ; u s e S y m f o n y \ C o m p o n e n t \ C o n s o l e \ A p p l i c a t i o n ; $ a p p l i c a t i o n = n e w A p p l i c a t i o n ( ) ; $ a p p l i c a t i o n - > r u n ( ' D r u p a l C a m p s ' , ' 1 . 0 ' ) ;
  16. Adding Commands Add command classes in s r c /

    Autoload via Composer Add to A p p l i c a t i o n
  17. Autoloading via Composer # c o m p o s

    e r . j s o n " a u t o l o a d " : { " p s r - 4 " : { " " : " s r c / " } }
  18. Autoloading via Composer # c o m p o s

    e r . j s o n " a u t o l o a d " : { " p s r - 4 " : { " D r u p a l C a m p s \ \ " : " s r c / " } }
  19. src/GoCommand.php u s e S y m f o n

    y \ C o m p o n e n t \ C o n s o l e \ C o m m a n d \ C o m m a n d ; c l a s s G o C o m m a n d e x t e n d s C o m m a n d { p u b l i c f u n c t i o n c o n f i g u r e ( ) { $ t h i s - > s e t N a m e ( ' g o ' ) - > s e t D e s c r i p t i o n ( ' G o t o a D r u p a l C a m p . ' ) ; } }
  20. src/GoCommand.php (cont) u s e S y m f o

    n y \ C o m p o n e n t \ C o n s o l e \ I n p u t \ I n p u t I n t e r f a c e ; u s e S y m f o n y \ C o m p o n e n t \ C o n s o l e \ O u t p u t \ O u t p u t I n t e r f a c e ; . . . p u b l i c f u n c t i o n e x e c u t e ( I n p u t I n t e r f a c e $ i n p u t , O u t p u t I n t e r f a c e $ o u t p u t ) { / / E x e c u t e t h e c o m m a n d . }
  21. bin/drupalcamp.php $ a p p l i c a t

    i o n = n e w A p p l i c a t i o n ( ) ; $ a p p l i c a t i o n - > a d d ( n e w G o C o m m a n d ( ) ) ; $ a p p l i c a t i o n - > r u n ( ) ;
  22. bin/drupalcamp.php $ a p p l i c a t

    i o n = n e w A p p l i c a t i o n ( ) ; $ a p p l i c a t i o n - > a d d C o m m a n d s ( [ n e w G o C o m m a n d ( ) , ] ) ; $ a p p l i c a t i o n - > r u n ( ) ;
  23. src/GoCommand.php u s e S y m f o n

    y \ C o m p o n e n t \ C o n s o l e \ I n p u t \ I n p u t A r g u m e n t ; . . . p u b l i c f u n c t i o n c o n f i g u r e ( ) { $ t h i s - > s e t N a m e ( ' g o ' ) - > s e t D e s c r i p t i o n ( ' G o t o a D r u p a l C a m p ' ) - > a d d A r g u m e n t ( ' n a m e ' , I n p u t A r g u m e n t : : O P T I O N A L , ' W h i c h D r u p a l C a m p ? ' ) - > a d d O p t i o n ( ' p a s t ' , n u l l , I n p u t O p t i o n : : V A L U E _ N O N E ) ; }
  24. src/GoCommand.php (cont) p u b l i c f u

    n c t i o n e x e c u t e ( I n p u t I n t e r f a c e $ i n p u t , O u t p u t I n t e r f a c e $ o u t p u t ) { $ t e x t = $ i n p u t - > g e t A r g u m e n t ( ' n a m e ' ) ; $ p a s t = $ i n p u t - > g e t O p t i o n ( ' p a s t ' ) ; . . . }
  25. InputInterface $ i n p u t - > g

    e t A r g u m e n t ( ' f o o ' ) ; $ i n p u t - > g e t O p t i o n ( ' b a r ' ) ;
  26. OutputInterface $ o u t p u t - >

    w r i t e ( $ t e x t ) ; $ o u t p u t - > w r i t e l n ( $ t e x t ) ;
  27. OutputInterface $ o u t p u t - >

    w r i t e ( " < i n f o > $ t e x t < / i n f o > " ) ; $ o u t p u t - > w r i t e ( " < e r r o r > $ t e x t < / e r r o r > " ) ; $ o u t p u t - > w r i t e ( " < d e b u g > $ t e x t < / d e b u g > " ) ;
  28. SymfonyStyle p u b l i c f u n

    c t i o n e x e c u t e ( I n p u t I n t e r f a c e $ i n p u t , O u t p u t I n t e r f a c e $ o u t p u t ) { $ i o = n e w S y m f o n y S t y l e ( $ i n p u t , $ o u t p u t ) ; $ i o - > t a b l e ( [ ' T i t l e ' , ' S p e a k e r ' ] , [ [ ' D r u p a l V M , M e e t S y m f o n y C o n s o l e ' , ' O l i v e r D a v i e s ' ] , [ ' D r u p a l 8 a n d t h e S y m f o n y E v e n t D i s p a t c h e r ' , ' E r i c S m i t h ' ] , [ ' B u i l d i n g w i t h A P I s ' , ' N i g e l D u n n ' ] , ] ) ; }
  29. src/GoCommand.php p u b l i c f u n

    c t i o n i n t e r a c t ( I n p u t I n t e r f a c e $ i n p u t , O u t p u t I n t e r f a c e $ o u t p u t ) { $ i o = n e w S y m f o n y S t y l e ( $ i n p u t , $ o u t p u t ) ; i f ( ! $ i n p u t - > g e t A r g u m e n t ( ' n a m e ' ) ) { $ i n p u t - > s e t A r g u m e n t ( ' n a m e ' , $ i o - > a s k ( ' W h i c h D r u p a l C a m p ? ' ) ) ; } }
  30. src/GoCommand.php p u b l i c f u n

    c t i o n e x e c u t e ( I n p u t I n t e r f a c e $ i n p u t , O u t p u t I n t e r f a c e $ o u t p u t ) { $ i o = n e w S y m f o n y S t y l e ( $ i n p u t , $ o u t p u t ) ; $ i o - > s u c c e s s ( $ i n p u t - > g e t A r g u m e n t ( ' n a m e ' ) ) ; }
  31. Dependency Injection Instantiate dependencies in single place or DI container

    Add as arguments when adding commands Add as arguments to the constructor and assign to properties Make sure to call the __construct() method within the parent class.
  32. src/Console/Application.php u s e G u z z l e

    H t t p \ C l i e n t ; . . . $ c l i e n t = n e w C l i e n t ( ) ; $ a p p l i c a t i o n - > a d d ( n e w N e w C o m m a n d ( $ c l i e n t ) ) ;
  33. src/Command/NewCommand.php u s e G u z z l e

    H t t p \ C l i e n t I n t e r f a c e ; u s e S y m f o n y \ C o m p o n e n t \ C o n s o l e \ C o m m a n d ; f i n a l c l a s s N e w C o m m a n d e x t e n d s C o m m a n d { p r i v a t e $ c l i e n t ; p u b l i c f u n c t i o n _ _ c o n s t r u c t ( C l i e n t I n t e r f a c e $ c l i e n t ) { p a r e n t : : _ _ c o n s t r u c t ( ) ; $ t h i s - > c l i e n t = $ c l i e n t ; } }
  34. Generating phar les # c o m p o s

    e r . j s o n " s c r i p t s " : { " b u i l d " : [ " b o x b u i l d " , " s h a s u m d r u p a l v m . p h a r | a w k ' { p r i n t $ 1 } ' > d r u p a l v m . p h a r . v e r s i o n " ] } $ c o m p o s e r r u n - s c r i p t b u i l d
  35. Roadmap Keep up to date with Drupal VM stable releases

    New commands - updating existing les, adding vhosts Improve user defaults and settings