0
Zend Framework 2:state of the artby Enrico Zimuel (enrico@zend.com)Senior Software EngineerZend Framework Core TeamZend Te...
About me                                   • Software Engineer since 1996                                          – Assem...
Summary●   Overview of ZF2●    Autoloading●   Dependency Injection●    Event Manager●    The new MVC architecture●    Migr...
Overview of ZF2●   Open source framework for PHP, evolution of ZF1    (over 10 million downloads)●   Still in development ...
A new core●   The ZF1 way:       ▶   Singletons, Registries, and Hard-Coded              Dependencies●    The ZF2 approach...
Architectural approach●   Methodologies used in the development      – Decoupling (ZendDi)      – Event driven (ZendEventM...
Our goal for ZF2                  Better consistency                   and performance             © All rights reserved. ...
Autoloading  © All rights reserved. Zend Technologies, Inc.
Autoloading●   No more require_once calls!●    Multiple approaches:      – ZF1-style include_path autoloader      – Per-na...
ZF1-Stylerequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader(array(    fallback_aut...
ZF2 NS/Prefixrequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader();$loader->registe...
ZF2 Class-Mapreturn array(    MyFooBar => __DIR__ . /Foo/Bar.php,);                                                       ...
Classmap generator●   How to generate the .classmap.php ?     We provided a command line tool:     bin/classmap_generator....
Performance ●     Class-Maps show a 25% improvement on the ZF1       autoloader when no acceleration is present     ▶   an...
Dependency Injection  © All rights reserved. Zend Technologies, Inc.
Dependency injection●    How to manage dependencies between objects?●    Dependency injection (Di) is a design pattern who...
Di by construct         Without Di                                           With Di (construct)class Foo {               ...
Di by setterclass Foo {   protected $bar;   …   public function setBar(Bar $bar) {      $this->bar = $bar;   }   …}       ...
ZendDi●   Supports the 3 different injection patterns:       – Constructor       – Interface       – Setter●    Implements...
Sample definition $definition = array(     Foo => array(         setBar => array(             bar => array(               ...
Using the Di container use ZendDiDi,     ZendDiConfiguration; $di     = new Di; $config = new Configuration(array(     def...
Di by annotation namespace FooBar {    use ZendDiDefinitionAnnotation as Di;     class Baz {        public $bam;        /*...
Di by annotation (2)$compiler = new ZendDiDefinitionCompilerDefinition();$compiler->addDirectory(File path of Baz and Bar)...
Event Manager   © All rights reserved. Zend Technologies, Inc.
The problem●    How do we introduce logging/debug points in    framework code?●    How do we allow users to introduce cach...
Event Manager●    An Event Manager is an object that aggregates    listeners for one or more named events, and    which tr...
Exampleuse ZendEventManagerEventManager;$events = new EventManager();$events->attach(do, function($e) {    $event = $e->ge...
MVC© All rights reserved. Zend Technologies, Inc.
Event driven architecture●    An Application composes a router, a locator, and an    event manager●    A matching route sh...
Module architecture  “A module is a collection of code and other    files that solves a more specific atomic   problem of ...
Modules●    The basic unit in a ZF2 MVC application is    a Module●    Modules are simply:       ▶   A namespace       ▶  ...
Example●    modules/●     Foo/●       Module.php                 © All rights reserved. Zend Technologies, Inc.
Module.php       namespace Foo;       class Module { } ●   Modules usually also provide:        ▶   Autoloading artifacts ...
index.phpuse ZendModuleManager,    ZendMvcBootstrap,    ZendMvcApplication;$config = include __DIR__. /../configs/app.conf...
Controllernamespace FooController;use ZendMvcControllerActionController;class HelloController extends ActionController{   ...
Render a viewuse ZendEventManagerEventCollection as Events,    ZendEventManagerListenerAggregate;class ViewListener implem...
Getting dependenciesnamespace ContactController;use ZendMailTransport,    ZendMvcControllerActionController;class ContactC...
Di configurationreturn array(di => array(  definition => array(class => array(    ZendMailTransportSmtp => array(      __c...
Source package    © All rights reserved. Zend Technologies, Inc.
Source package ●     http://packages.zendframework.com/ ●     Source packages (download + github) ●   Pyrus packages:     ...
From ZF1 to ZF2    © All rights reserved. Zend Technologies, Inc.
Migrate to ZF2●   Our goal: migrate without rewriting much code!●   Main steps      – Namespace: Zend_Foo => ZendFoo      ...
ZF1 migration prototype●    Source code: http://bit.ly/pvc0X1●    Creates a "Zf1Compat" version of the ZF1    dispatcher a...
Helping out●    http://framework.zend.com/zf2●    http://github.com/zendframework●    Bi-weekly IRC meetings (#zf2-meeting...
Thank you! ●     Vote this talk:        ▶   http://joind.in/4359●     Comments and feedbacks:       – enrico@zend.com     ...
Upcoming SlideShare
Loading in...5
×

ZF2 Presentation @PHP Tour 2011 in Lille

2,378

Published on

uring the first edition of a regional event of the French PHP Community in late November 2012, Enrico Zimuel, ZF Core Team member, presented the “state of the art” of the Zend Framework 2 project. The new architecture, the new features, the performance improvement and the new classes of the 2.0 release were discussed.
Differences between ZF1 and ZF2 and how to migrate a ZF1 project to the new version is also presented.

Published in: Technology
1 Comment
4 Likes
Statistics
Notes
  • It's Very Good, this is the first hand book about zf2
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
2,378
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
98
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide

Transcript of "ZF2 Presentation @PHP Tour 2011 in Lille"

  1. 1. Zend Framework 2:state of the artby Enrico Zimuel (enrico@zend.com)Senior Software EngineerZend Framework Core TeamZend Technologies Ltd PHPTour Lille 2011 – 25 November http://afup.org/pages/phptourlille2011/ © All rights reserved. Zend Technologies, Inc.
  2. 2. About me • Software Engineer since 1996 – Assembly x86, C/C++, Java, Perl, PHP • Enjoying PHP since 1999 • PHP Engineer at Zend since 2008 • ZF Core Team from April 2011 • B.Sc. Computer Science and EconomicsEmail: enrico@zend.comTwitter: @ezimuel from University of Pescara (Italy) © All rights reserved. Zend Technologies, Inc.
  3. 3. Summary● Overview of ZF2● Autoloading● Dependency Injection● Event Manager● The new MVC architecture● Migrate from ZF1 to ZF2 © All rights reserved. Zend Technologies, Inc.
  4. 4. Overview of ZF2● Open source framework for PHP, evolution of ZF1 (over 10 million downloads)● Still in development – ZF2 beta1 released in October 2011● New Betas at least every six weeks (until were done)● No more CLA to contribute to ZF2● We use github (no more SVN) © All rights reserved. Zend Technologies, Inc.
  5. 5. A new core● The ZF1 way: ▶ Singletons, Registries, and Hard-Coded Dependencies● The ZF2 approach: ▶ Aspect Oriented Design and Dependency Injection © All rights reserved. Zend Technologies, Inc.
  6. 6. Architectural approach● Methodologies used in the development – Decoupling (ZendDi) – Event driven (ZendEventManager) – Standard interfaces (ZendStdlib)● Take advantages of PHP 5.3+ © All rights reserved. Zend Technologies, Inc.
  7. 7. Our goal for ZF2 Better consistency and performance © All rights reserved. Zend Technologies, Inc.
  8. 8. Autoloading © All rights reserved. Zend Technologies, Inc.
  9. 9. Autoloading● No more require_once calls!● Multiple approaches: – ZF1-style include_path autoloader – Per-namespace/prefix autoloading – Class-map autoloading © All rights reserved. Zend Technologies, Inc.
  10. 10. ZF1-Stylerequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader(array( fallback_autoloader => true,));$loader->register(); © All rights reserved. Zend Technologies, Inc.
  11. 11. ZF2 NS/Prefixrequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader();$loader->registerNamespace( My, __DIR__ . /../library/My) ->registerPrefix( Foo_, __DIR__ . /../library/Foo);$loader->register(); © All rights reserved. Zend Technologies, Inc.
  12. 12. ZF2 Class-Mapreturn array( MyFooBar => __DIR__ . /Foo/Bar.php,); .classmap.phprequire_once Zend/Loader/ClassMapAutoloader.php;$loader = new ZendLoaderClassMapAutoloader();$loader->registerAutoloadMap( __DIR__ . /../library/.classmap.php);$loader->register(); © All rights reserved. Zend Technologies, Inc.
  13. 13. Classmap generator● How to generate the .classmap.php ? We provided a command line tool: bin/classmap_generator.php● Usage is trivial: $ cd your/library $ php /path/to/classmap_generator.php -w● Class-Map will be created in .classmap.php © All rights reserved. Zend Technologies, Inc.
  14. 14. Performance ● Class-Maps show a 25% improvement on the ZF1 autoloader when no acceleration is present ▶ and 60-85% improvements when an opcode cache is in place! ● Pairing namespaces/prefixes with specific paths shows >10% gains with no acceleration ▶ and 40% improvements when an opcode cache is in place! © All rights reserved. Zend Technologies, Inc.
  15. 15. Dependency Injection © All rights reserved. Zend Technologies, Inc.
  16. 16. Dependency injection● How to manage dependencies between objects?● Dependency injection (Di) is a design pattern whose purpose is to reduce the coupling between software components © All rights reserved. Zend Technologies, Inc.
  17. 17. Di by construct Without Di With Di (construct)class Foo { class Foo { protected $bar; protected $bar; … … public function __construct() { public function $this->bar= new Bar(); __construct(Bar $bar) { } $this->bar = $bar; … }} … } Cons: Pros: Difficult to test Easy to test No isolation Isolation Difficult to reuse code Flexible architecture © All rights reserved. Zend Technologies, Inc.
  18. 18. Di by setterclass Foo { protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } …} © All rights reserved. Zend Technologies, Inc.
  19. 19. ZendDi● Supports the 3 different injection patterns: – Constructor – Interface – Setter● Implements a Di Container: – Manage the dependencies using configuration and annotation – Provide a compiler to autodiscover classes in a path and create the class definitions, for dependencies © All rights reserved. Zend Technologies, Inc.
  20. 20. Sample definition $definition = array( Foo => array( setBar => array( bar => array( type => Bar, required => true, ), ), ), ); © All rights reserved. Zend Technologies, Inc.
  21. 21. Using the Di container use ZendDiDi, ZendDiConfiguration; $di = new Di; $config = new Configuration(array( definition => array(class => $definition) )); $config->configure($di); $foo = $di->get(Foo); // contains Bar! © All rights reserved. Zend Technologies, Inc.
  22. 22. Di by annotation namespace FooBar { use ZendDiDefinitionAnnotation as Di; class Baz { public $bam; /** * @DiInject() */ public function setBam(Bam $bam){ $this->bam = $bam; } } class Bam { } } © All rights reserved. Zend Technologies, Inc.
  23. 23. Di by annotation (2)$compiler = new ZendDiDefinitionCompilerDefinition();$compiler->addDirectory(File path of Baz and Bar);$compiler->compile();$definitions = new ZendDiDefinitionList($compiler);$di = new ZendDiDi($definitions);$baz = $di->get(FooBarBaz);More use cases of ZendDi:https://github.com/ralphschindler/zf2-di-use-cases © All rights reserved. Zend Technologies, Inc.
  24. 24. Event Manager © All rights reserved. Zend Technologies, Inc.
  25. 25. The problem● How do we introduce logging/debug points in framework code?● How do we allow users to introduce caching without needing to extend framework code?● How do we allow users to introduce validation, filtering, ACL checks, etc., without needing to extend framework code? © All rights reserved. Zend Technologies, Inc.
  26. 26. Event Manager● An Event Manager is an object that aggregates listeners for one or more named events, and which triggers events.● A Listener is a callback that can react to an event.● An Event is an action. © All rights reserved. Zend Technologies, Inc.
  27. 27. Exampleuse ZendEventManagerEventManager;$events = new EventManager();$events->attach(do, function($e) { $event = $e->getName(); $params = $e->getParams(); printf( Handled event “%s”, with parameters %s, $event, json_encode($params) );});$params = array(foo => bar, baz => bat);$events->trigger(do, null, $params); © All rights reserved. Zend Technologies, Inc.
  28. 28. MVC© All rights reserved. Zend Technologies, Inc.
  29. 29. Event driven architecture● An Application composes a router, a locator, and an event manager● A matching route should return a controller name● Controllers are pulled from the locator and dispatched● Routing and dispatching are events © All rights reserved. Zend Technologies, Inc.
  30. 30. Module architecture “A module is a collection of code and other files that solves a more specific atomic problem of the larger business problem.” (from the ZF2 RFC) © All rights reserved. Zend Technologies, Inc.
  31. 31. Modules● The basic unit in a ZF2 MVC application is a Module● Modules are simply: ▶ A namespace ▶ containing a single classfile, a Module © All rights reserved. Zend Technologies, Inc.
  32. 32. Example● modules/● Foo/● Module.php © All rights reserved. Zend Technologies, Inc.
  33. 33. Module.php namespace Foo; class Module { } ● Modules usually also provide: ▶ Autoloading artifacts ▶ Basic configuration © All rights reserved. Zend Technologies, Inc.
  34. 34. index.phpuse ZendModuleManager, ZendMvcBootstrap, ZendMvcApplication;$config = include __DIR__. /../configs/app.config.php;$modules = new Manager($config[modules]);$bootstrap = new Bootstrap($modules);$app = new Application();$bootstrap->bootstrap($app);$app->run()->send(); © All rights reserved. Zend Technologies, Inc.
  35. 35. Controllernamespace FooController;use ZendMvcControllerActionController;class HelloController extends ActionController{ public function worldAction() { $query = $this->request->query(); $message = $query->get(message, Nobody); return array(message => $message); }} © All rights reserved. Zend Technologies, Inc.
  36. 36. Render a viewuse ZendEventManagerEventCollection as Events, ZendEventManagerListenerAggregate;class ViewListener implements ListenerAggregate{ /* ... */ public function attach(Events $events) { $events->attach(dispatch, array($this, renderView, -100); $events->attach(dispatch, array($this, renderLayout, -1000); } /* ... */} © All rights reserved. Zend Technologies, Inc.
  37. 37. Getting dependenciesnamespace ContactController;use ZendMailTransport, ZendMvcControllerActionController;class ContactController extends ActionController{ public function setMailer(Transport $transport) { $this->transport = $transport; }} © All rights reserved. Zend Technologies, Inc.
  38. 38. Di configurationreturn array(di => array( definition => array(class => array( ZendMailTransportSmtp => array( __construct => array( host => array(required => true, type => false), user => array(required => true, type => false), pass => array(required => true, type => false), ), ), )), instance => array( ZendMailTransport => array(parameters => array( host => some.host.tld, user => user, pass => pass )), ),); © All rights reserved. Zend Technologies, Inc.
  39. 39. Source package © All rights reserved. Zend Technologies, Inc.
  40. 40. Source package ● http://packages.zendframework.com/ ● Source packages (download + github) ● Pyrus packages: ▶ wget http://packages.zendframework.com/pyrus.phar ▶ pyrus.phar . ▶ pyrus.phar . channel­discover  packages.zendframework.com ▶ pyrus.phar . install zf2/<zf­package> ▶ pyrus.phar . install zf2/Zend_<component> © All rights reserved. Zend Technologies, Inc.
  41. 41. From ZF1 to ZF2 © All rights reserved. Zend Technologies, Inc.
  42. 42. Migrate to ZF2● Our goal: migrate without rewriting much code!● Main steps – Namespace: Zend_Foo => ZendFoo – Exceptions: an Interface for each components, no more Zend_Exception – Autoloading: 3 possible options (one is the same of ZF1) ▶ MVC: module, event based, dispatchable © All rights reserved. Zend Technologies, Inc.
  43. 43. ZF1 migration prototype● Source code: http://bit.ly/pvc0X1● Creates a "Zf1Compat" version of the ZF1 dispatcher as an event listener.● The bootstrap largely mimics how ZF1s Zend_Application bootstrap works.● The default route utilizes the new ZF2 MVC routing, but mimics what ZF1 provided. © All rights reserved. Zend Technologies, Inc.
  44. 44. Helping out● http://framework.zend.com/zf2● http://github.com/zendframework● Bi-weekly IRC meetings (#zf2-meeting on Freenode IRC)● #zftalk.2 on Freenode IRC © All rights reserved. Zend Technologies, Inc.
  45. 45. Thank you! ● Vote this talk: ▶ http://joind.in/4359● Comments and feedbacks: – enrico@zend.com © All rights reserved. Zend Technologies, Inc.
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×