Upload
zend-technologies
View
2.348
Download
0
Embed Size (px)
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.
Citation preview
© 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/
© 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
© All rights reserved. Zend Technologies, Inc.
● Overview of ZF2● Autoloading● Dependency Injection● Event Manager● The new MVC architecture● Migrate from ZF1 to ZF2
Summary
© 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)
© 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
© 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+
© All rights reserved. Zend Technologies, Inc.
Our goal for ZF2
Better consistency and performance
© All rights reserved. Zend Technologies, Inc.
Autoloading
© 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
© 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();
© 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();
© 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
© 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
© 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!
© All rights reserved. Zend Technologies, Inc.
Dependency Injection
© 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
© 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
© All rights reserved. Zend Technologies, Inc.
Di by setter
class Foo { protected $bar; … public function setBar(Bar $bar) { $this->bar = $bar; } …}
© 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
© All rights reserved. Zend Technologies, Inc.
Sample definition
$definition = array( 'Foo' => array( 'setBar' => array( 'bar' => array( 'type' => 'Bar', 'required' => true, ), ), ), );
© 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!
© 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 { }}
© 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
© All rights reserved. Zend Technologies, Inc.
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 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.
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.
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);
© All rights reserved. Zend Technologies, Inc.
MVC
© 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
© 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
© 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
© All rights reserved. Zend Technologies, Inc.
Example
● modules/● Foo/● Module.php
© All rights reserved. Zend Technologies, Inc.
Module.php
namespace Foo; class Module { }
● Modules usually also provide:▶ Autoloading artifacts▶ Basic configuration
© 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();
© 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); }}
© 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); }
/* ... */}
© 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; }}
© 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' )), ),);
© All rights reserved. Zend Technologies, Inc.
Source package
© 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 . channeldiscover packages.zendframework.com
▶ pyrus.phar . install zf2/<zfpackage>▶ pyrus.phar . install zf2/Zend_<component>
© All rights reserved. Zend Technologies, Inc.
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 => 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
© 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.
© 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
© All rights reserved. Zend Technologies, Inc.
Thank you!
● Vote this talk:
▶ http://joind.in/4359
● Comments and feedbacks: