ZF2 Presentation @PHP Tour 2011 in Lille

  • 2,302 views
Uploaded 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 …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
  • It's Very Good, this is the first hand book about zf2
    Are you sure you want to
    Your message goes here
No Downloads

Views

Total Views
2,302
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
92
Comments
1
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 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. 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. 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. 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. 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. 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. Our goal for ZF2 Better consistency and performance © All rights reserved. Zend Technologies, Inc.
  • 8. Autoloading © All rights reserved. Zend Technologies, Inc.
  • 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. ZF1-Stylerequire_once Zend/Loader/StandardAutoloader.php;$loader = new ZendLoaderStandardAutoloader(array( fallback_autoloader => true,));$loader->register(); © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. 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. Dependency Injection © All rights reserved. Zend Technologies, Inc.
  • 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. 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. Di by setterclass Foo { protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } …} © All rights reserved. Zend Technologies, Inc.
  • 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. Sample definition $definition = array( Foo => array( setBar => array( bar => array( type => Bar, required => true, ), ), ), ); © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. Event Manager © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. MVC© All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. Example● modules/● Foo/● Module.php © All rights reserved. Zend Technologies, Inc.
  • 33. Module.php namespace Foo; class Module { } ● Modules usually also provide: ▶ Autoloading artifacts ▶ Basic configuration © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. 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. 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. Source package © All rights reserved. Zend Technologies, Inc.
  • 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. From ZF1 to ZF2 © All rights reserved. Zend Technologies, Inc.
  • 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. 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. 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. Thank you! ● Vote this talk: ▶ http://joind.in/4359● Comments and feedbacks: – enrico@zend.com © All rights reserved. Zend Technologies, Inc.