zf2 presentation @php tour 2011 in lille

45
© All rights reserved. Zend Technologies, Inc. Zend Framework 2: state of the art by Enrico Zimuel ([email protected]) Senior Software Engineer Zend Framework Core Team Zend Technologies Ltd PHPTour Lille 2011 – 25 November http://afup.org/pages/phptourlille2011/

Upload: zend-technologies

Post on 08-May-2015

2.348 views

Category:

Technology


0 download

DESCRIPTION

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.

TRANSCRIPT

Page 1: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Zend Framework 2:state of the art

by Enrico Zimuel ([email protected])

Senior Software EngineerZend Framework Core TeamZend Technologies Ltd

PHPTour Lille 2011 – 25 Novemberhttp://afup.org/pages/phptourlille2011/

Page 2: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

• 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 Economics

from University of Pescara (Italy)Email: [email protected]: @ezimuel

About me

Page 3: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

● Overview of ZF2● Autoloading● Dependency Injection● Event Manager● The new MVC architecture● Migrate from ZF1 to ZF2

Summary

Page 4: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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 we're done)● No more CLA to contribute to ZF2● We use github (no more SVN)

Page 5: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

A new core

● The ZF1 way:

▶ Singletons, Registries, and Hard-Coded Dependencies

● The ZF2 approach:

▶ Aspect Oriented Design and Dependency Injection

Page 6: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Architectural approach

● Methodologies used in the development

– Decoupling (Zend\Di)

– Event driven (Zend\EventManager)

– Standard interfaces (Zend\Stdlib)● Take advantages of PHP 5.3+

Page 7: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Our goal for ZF2

Better consistency and performance

Page 8: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Autoloading

Page 9: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Autoloading

● No more require_once calls!

● Multiple approaches:

– ZF1-style include_path autoloader

– Per-namespace/prefix autoloading

– Class-map autoloading

Page 10: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

ZF1-Style

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new Zend\Loader\StandardAutoloader(array( 'fallback_autoloader' => true,));

$loader->register();

Page 11: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

ZF2 NS/Prefix

require_once 'Zend/Loader/StandardAutoloader.php';

$loader = new Zend\Loader\StandardAutoloader();

$loader->registerNamespace( 'My', __DIR__ . '/../library/My') ->registerPrefix( 'Foo_', __DIR__ . '/../library/Foo');

$loader->register();

Page 12: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

ZF2 Class-Map

return array( 'My\Foo\Bar' => __DIR__ . '/Foo/Bar.php',);

require_once 'Zend/Loader/ClassMapAutoloader.php';

$loader = new Zend\Loader\ClassMapAutoloader();

$loader->registerAutoloadMap( __DIR__ . '/../library/.classmap.php');

$loader->register();

.classmap.php

Page 13: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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

Page 14: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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!

Page 15: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Dependency Injection

Page 16: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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

Page 17: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Di by construct

class Foo { protected $bar; … public function __construct() { $this->bar= new Bar(); } …}

Without Di

class Foo { protected $bar; … public function __construct(Bar $bar) { $this->bar = $bar; } …}

With Di (construct)

Cons:Difficult to testNo isolationDifficult to reuse code

Pros:Easy to testIsolationFlexible architecture

Page 18: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Di by setter

class Foo { protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } …}

Page 19: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Zend\Di

● 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

Page 20: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Sample definition

$definition = array( 'Foo' => array( 'setBar' => array( 'bar' => array( 'type' => 'Bar', 'required' => true, ), ), ), );

Page 21: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Using the Di container

use Zend\Di\Di, Zend\Di\Configuration; $di = new Di; $config = new Configuration(array( 'definition' => array('class' => $definition) )); $config->configure($di); $foo = $di->get('Foo'); // contains Bar!

Page 22: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Di by annotation

namespace Foo\Bar { use Zend\Di\Definition\Annotation as Di;

class Baz { public $bam; /** * @Di\Inject() */ public function setBam(Bam $bam){ $this->bam = $bam; } }

class Bam { }}

Page 23: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Di by annotation (2)

$compiler = new Zend\Di\Definition\CompilerDefinition();$compiler->addDirectory('File path of Baz and Bar');$compiler->compile();

$definitions = new Zend\Di\DefinitionList($compiler);$di = new Zend\Di\Di($definitions);

$baz = $di->get('Foo\Bar\Baz');

More use cases of Zend\Di:https://github.com/ralphschindler/zf2-di-use-cases

Page 24: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Event Manager

Page 25: ZF2 Presentation @PHP Tour 2011 in Lille

© 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 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?

Page 26: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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.

Page 27: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Example

use Zend\EventManager\EventManager;

$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);

Page 28: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

MVC

Page 29: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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

Page 30: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

“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)

Module architecture

Page 31: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Modules

● The basic unit in a ZF2 MVC application is a Module

● Modules are simply:▶ A namespace▶ containing a single classfile, a Module

Page 32: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Example

● modules/● Foo/● Module.php

Page 33: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Module.php

namespace Foo; class Module { }

● Modules usually also provide:▶ Autoloading artifacts▶ Basic configuration

Page 34: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

index.php

use Zend\Module\Manager, Zend\Mvc\Bootstrap, Zend\Mvc\Application;

$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();

Page 35: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Controller

namespace Foo\Controller; use Zend\Mvc\Controller\ActionController; class HelloController extends ActionController{ public function worldAction() { $query = $this->request->query(); $message = $query->get('message', 'Nobody'); return array('message' => $message); }}

Page 36: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Render a view

use Zend\EventManager\EventCollection as Events, Zend\EventManager\ListenerAggregate;

class ViewListener implements ListenerAggregate{ /* ... */

public function attach(Events $events) { $events->attach('dispatch', array($this, 'renderView', -100); $events->attach('dispatch', array($this, 'renderLayout', -1000); }

/* ... */}

Page 37: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Getting dependencies

namespace Contact\Controller; use Zend\Mail\Transport, Zend\Mvc\Controller\ActionController;

class ContactController extends ActionController{ public function setMailer(Transport $transport) { $this->transport = $transport; }}

Page 38: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Di configuration

return array('di' => array( 'definition' => array('class' => array( 'Zend\Mail\Transport\Smtp' => array( '__construct' => array( 'host' => array('required' => true, 'type' => false), 'user' => array('required' => true, 'type' => false), 'pass' => array('required' => true, 'type' => false), ), ), )), 'instance' => array( 'Zend\Mail\Transport' => array('parameters' => array( 'host' => 'some.host.tld', 'user' => 'user', 'pass' => 'pass' )), ),);

Page 39: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Source package

Page 40: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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>

Page 41: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

From ZF1 to ZF2

Page 42: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Migrate to ZF2

● Our goal: migrate without rewriting much code!● Main steps

– Namespace: Zend_Foo => Zend\Foo

– 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

Page 43: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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 ZF1's

Zend_Application bootstrap works.● The default route utilizes the new ZF2 MVC

routing, but mimics what ZF1 provided.

Page 44: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

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

Page 45: ZF2 Presentation @PHP Tour 2011 in Lille

© All rights reserved. Zend Technologies, Inc.

Thank you!

● Vote this talk:

▶ http://joind.in/4359

● Comments and feedbacks:

[email protected]