come sfruttare tutte le potenzialità di symfony in drupal 8
DESCRIPTION
Drupal 8 mette gia a disposizione molte API di Symfony, ma non tutte. Vediamo come sfruttare le altre.TRANSCRIPT
![Page 1: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/1.jpg)
COME SFRUTTARE TUTTE LE POTENZIALITÀ DI SYMFONY IN DRUPAL 8
![Page 2: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/2.jpg)
LUCA LUSSO
• @lussoluca • www.linkedin.com/in/lussoluca • drupal.org/user/138068 • [email protected]
Senior Web Developer @Wellnet
![Page 3: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/3.jpg)
#DrupalDaysITBENVENUTI
Sviluppatori Drupal7 ?
Sviluppatori Symfony ?
Sviluppatori Drupal8 ?
![Page 4: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/4.jpg)
#DrupalDaysITNUOVE API IN DRUPAL8
• Routing • Configuration management • Entity • Plugin • Views • Migrate • Typed Data • Validation • …
![Page 5: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/5.jpg)
#DrupalDaysITCOMPONENTI DI SYMFONY IN DRUPAL8
• Classloader • Dependency injection • Event dispatcher • Http foundation • Http kernel • Routing • Serializer • Validator • Yaml • Twig
![Page 6: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/6.jpg)
#DrupalDaysITMODULI CUSTOM
In un modulo custom posso usare:
le nuove API di Drupal8
+
tutte le API messe a disposizione dai componenti di Symfony
+
![Page 7: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/7.jpg)
#DrupalDaysITTALK
In questo talk vedremo un sacco di codice, non preoccupatevi…
…però partiamo da un caso reale, giusto per non parlare a vuoto :-)
+
![Page 8: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/8.jpg)
#DrupalDaysITPROBLEMAIdentificare il motivo per cui una data pagina è lenta
![Page 9: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/9.jpg)
#DrupalDaysITDRUPAL DEVEL
Il modulo Devel ha un logger per query, time e memoria
![Page 10: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/10.jpg)
#DrupalDaysITSYMFONY PROFILER
![Page 11: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/11.jpg)
#DrupalDaysITSYMFONY PROFILER
E’ presente solo nella Standard Edition di Symfony.
Quindi nel core di Drupal8 non ci sarà…
![Page 12: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/12.jpg)
#DrupalDaysITSYMFONY PROFILER
Per poter misurare tutti questi valori il profiler di Symfony è molto integrato dentro il framework e usa API e concetti
piuttosto specifici
Posso fare una cosa simile per Drupal?
![Page 13: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/13.jpg)
#DrupalDaysITWEBPROFILER
Modulo Webprofiler
drupal.org/project/webprofiler
![Page 14: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/14.jpg)
#DrupalDaysITWEBPROFILER
Profiler di Symfony per Drupal8!
![Page 15: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/15.jpg)
#DrupalDaysITWEBPROFILER
![Page 16: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/16.jpg)
#DrupalDaysITWEBPROFILER
![Page 17: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/17.jpg)
#DrupalDaysITATTENZIONE 1
Drupal8 è ancora in versione alpha, gli esempi seguenti potrebbero non funzionare più!
![Page 18: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/18.jpg)
#DrupalDaysITATTENZIONE 2
Esistono due versioni del modulo webprofiler
!• 8.x-1.x-alpha11 -> compatibile con la alpha11 del core • 8.x-dev -> compatibile con l’HEAD corrente del core
![Page 19: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/19.jpg)
#DrupalDaysITESEMPIO
Vedremo come sfruttare questi concetti Symfony in un modulo Drupal: !
• Service provider • Compiler • Event listener • Profiler
![Page 20: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/20.jpg)
#DrupalDaysITSERVICE PROVIDER
• Symfony utilizza un’architettura a servizi • I servizi dovrebbero contenere la business logic dell’applicazione,
non i controller! • Tutti i servizi del core sono definiti nel file core/core.services.yml • I moduli possono aggiungere nuovi servizi e modificare quelli esistenti
![Page 21: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/21.jpg)
#DrupalDaysITSERVIZI DEL CORE
• current_user • string_translation • database • settings • state • config.factory • cache_factory • form_builder • http_client • ~ altri 200 (^(\s){2}([a-z_.]*):$ su core.services.yml)
![Page 22: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/22.jpg)
#DrupalDaysITSERVIZI
• Implementano una certa funzionalità • Aumentano la possibilità di riuso del codice • Rendono un applicazione più testabile • Possono essere facilmente sostituti • Vengono caricati solo se servono per gestire una data richiesta
![Page 23: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/23.jpg)
#DrupalDaysITSERVIZI
Sono gestiti da un ServiceContainer che in automatico soddisfa le eventuali dipendenze attraverso un meccanismo chiamato Dependency Injection, ossia non è il servizio che carica le proprie dipendenze da altri servizi ma è il Container che istanzia le classi necessarie all’atto della creazione del servizio stesso.
![Page 24: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/24.jpg)
#DrupalDaysITSERVIZI
Servizio B (es. Database)Servizio A (es. Mailer)
Servizio C che dipende da A e B (es. Newsletter)
Se chiedo al Container un’istanza del servizio C, prima verranno create le istanze dei servizi A e B. Il container userà poi queste istanze (tipicamente) come parametri del costruttore di C all’atto della sua creazione.
![Page 25: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/25.jpg)
#DrupalDaysITSERVICE PROVIDER
• Durante il bootstrap Drupal cerca una classe chiamata nomemoduloServiceProvider e un file nomemodulo.services.yml all’interno di ciascun modulo attivo
• Il file .yml permette di aggiungere nuovi servizi • La classe permette di modificare i servizi esistenti o di
aggiungerne di nuovi • Alla fine del processo di scoperta e modifica dei servizi il
ServiceContainer viene scritto su un file (tipicamente) in sites/default/files/php (verrà eliminato solo da uno svuotamento eventuale della cache)
![Page 26: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/26.jpg)
#DrupalDaysITWEBPROFILERSERVICEPROVIDER.PHP
![Page 27: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/27.jpg)
#DrupalDaysITWEBPROFILER.SERVICES.YML
![Page 28: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/28.jpg)
#DrupalDaysITESEMPIO
• Service provider • Compiler • Event listener • Profiler
![Page 29: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/29.jpg)
#DrupalDaysITCOMPILER
• Dopo aver aggiunto o alterato i servizi il ServiceContainer viene “compilato” per ottimizzarlo e per aggiungere ulteriori funzionalità (parametri e tag)
• Il ServiceContainer passa attraverso una sequenza ordinata di passi di compilazione. Molti di questi passi sono definiti da Symfony stesso, alcuni sono nel core di Drupal, i moduli possono definirne ulteriori
• I passi di compilazione servono per gestire i riferimenti circolari, rimuovere servizi inutilizzati, trovare servizi con specifici tag, …
![Page 30: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/30.jpg)
#DrupalDaysITCOMPILER
![Page 31: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/31.jpg)
#DrupalDaysITCOMPILER PASS
Un passo di compilazione potrebbe ad esempio cercare tutti i servizi che hanno un certo tag:
ContainerBuilder::findTaggedServiceIds()
![Page 32: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/32.jpg)
#DrupalDaysITCOMPILER PASS
![Page 33: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/33.jpg)
#DrupalDaysITCOMPILER PASS
Oppure modificare un servizio esistente, ad esempio cambiando una delle dipendenze.
Possiamo addirittura modificare completamente l’implementazione di un servizio (l’importante è che il nuovo servizio rispetti la stessa interfaccia di quello sostituito) -> servizi mock per il testing
![Page 34: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/34.jpg)
#DrupalDaysITCOMPILER PASS
Cambio una dipendenza
Sostituisco un servizio con un altro
![Page 35: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/35.jpg)
#DrupalDaysITESEMPIO
• Service provider • Compiler • Event listener • Profiler
![Page 36: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/36.jpg)
#DrupalDaysITEVENT LISTENER
Il componente EventDispatcher di Symfony permette di definire, sollevare e registrarsi ad eventi:
EventDispatcher::dispatch($eventName, Event $e = null)
EventSubscriberInterface::getSubscribedEvents()
![Page 37: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/37.jpg)
#DrupalDaysITEVENT LISTENERDefinisco un servizio all’interno di nomemodulo.services.yml e gli aggiungo il tag “event_subscriber”. La classe relativa deve implementare l’interfaccia EventSubscriberInterface e definire il metodo getSubscribedEvents(); in questo metodo registro i listener agli eventi che mi interessano. Uno dei passi di compilazione si occuperà di trovare il mio servizio e di registrarlo all’interno del gestore degli eventi. Se qualcuno solleva uno degli eventi a cui mi sono registrato vengo notificato.
![Page 38: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/38.jpg)
#DrupalDaysITEVENT LISTENER
![Page 39: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/39.jpg)
#DrupalDaysITEVENT LISTENER
![Page 40: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/40.jpg)
#DrupalDaysITEVENT LISTENER
• Posso definire i miei eventi e sollevarli in punti precisi dell’esecuzione di un mio codice
• Sostanzialmente gli eventi potrebbero sostituire il meccanismo degli hook di Drupal e in effetti posso già usarli all’interno di moduli custom (probabilmente gli hook spariranno del tutto in Drupal9, sostituiti dagli eventi)
![Page 41: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/41.jpg)
#DrupalDaysITESEMPIO
• Service provider • Compiler • Event listener • Profiler
![Page 42: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/42.jpg)
#DrupalDaysITPROFILER
• Il componente Http kernel contiene un tool per il profiling delle richieste
• In Symfony Standard Edition il Profiler è definito nel WebProfilerBundle e usato dal FrameworkBundle (i bundle sono i moduli di Symfony)
• Usa diversi DataCollectors per memorizzare informazioni su una specifica richiesta (tempo di esecuzione, memoria, routing, query al database, …)
• Nell’implementazione di Symfony salva i profili sul filesystem, usando un token come nome del file
![Page 43: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/43.jpg)
#DrupalDaysITPROFILER
L’infrastruttura per la generazione e il salvataggio dei profili è già tutta nel core di Drupal8, mancherebbero i pezzi aggiunti dal FrameworkBundle e dal WebprofilerBundler, però le funzionalità che questi due bundle usano sono già anch’esse nel core di Drupal8 (eventi, servizi, tag, …)
![Page 44: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/44.jpg)
#DrupalDaysITPROFILER
Ogni singolo collettore di dati è registrato come servizio e marcato con il tag “data_collector”. La classe relativa deve estendere la classe DataCollector e implementare il metodo collect()
![Page 45: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/45.jpg)
#DrupalDaysITPROFILER
![Page 46: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/46.jpg)
#DrupalDaysITESEMPIO
• Service provider • Compiler • Event listener • Profiler
![Page 47: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/47.jpg)
#DrupalDaysITTOOLS
• PHP 5.4 • GIT • Drush 7 • PhpStorm EAP (ha il supporto per Drupal8!)
![Page 48: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/48.jpg)
#DrupalDaysITAPPROFONDIMENTI
La versione 8.x del modulo examples (drupal.org/project/examples) conterrà a breve molti esempi a riguardo di questi concetti, potete scaricarne una preview da qua:
github.com/lussoluca/examples
![Page 49: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/49.jpg)
#DrupalDaysITRISORSE
• drupal.org/node/2116747 • drupal.org/node/2133171 • symfony.com/doc/current/components/dependency_injection/index.html • symfony.com/doc/current/components/event_dispatcher/index.html • symfony.com/doc/current/cookbook/profiler/index.html
![Page 50: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/50.jpg)
#DrupalDaysITCONCLUSIONI
• Drupal8 mette a disposizione innumerevoli nuove API, ma i componenti di Symfony inclusi nel core ne aggiungono a loro volta molte altre!
• Molte delle API di Symfony sono esposte da Drupal8 in modo che i moduli contrib e custom ne possano beneficiare
• I nostri moduli custom possono sfruttare tutte queste potenzialità per integrarsi sempre di più all’interno del framework
![Page 51: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/51.jpg)
#DrupalDaysITCONCLUSIONISe vogliamo arrivare pronti all’uscita di Drupal8 potremmo già iniziare a ragionare a “servizi” nei nostri moduli custom per Drupal7, quello che ci serve sono un classloader in grado di capire i namespace di PHP 5.3 e il PSR-0/4 (quello standard di Drupal7 non lo è) e un implementazione di ServiceContainer: !• drupal.org/project/xautoload -> PSR-0, PSR-4 • drupal.org/project/pimple_container
Se progettiamo così la business logic della nostra applicazione, la migrazione di un modulo per Drupal7 a Drupal8 potrebbe essere meno faticosa
![Page 52: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/52.jpg)
#DrupalDaysIT
Vuoi contribuire a webprofiler? Contattaci! Abbiamo bisogno di codice, grafica, documentazione, idee
drupal.org/project/webprofiler
![Page 53: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/53.jpg)
#DrupalDaysIT
DOMANDE?
![Page 54: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/54.jpg)
#DrupalDaysIT
GRAZIE ;-)
![Page 55: Come sfruttare tutte le potenzialità di Symfony in Drupal 8](https://reader034.vdocumenti.com/reader034/viewer/2022052621/5581ae1cd8b42afd4c8b5306/html5/thumbnails/55.jpg)
#DrupalDaysITTITOLO DIAPOSITIVA
Corpo della diapositiva