Practical ZF1 to ZF2 Migration:
Lessons from the Field
Clark Everetts,
Zend Technologies, Inc.
2
What are we going to talk about?
Getting from Here to Here
Higher-Level
(ROI, Organizational, Planning, Goals, Constrain...
3
•Clark Everetts
– Professional Services Consultant for Zend Technologies
– Onsite and Remote Consulting – Linux, IBM iSe...
4
•Your role: Manager, developer, stakeholder/decision-maker?
•Size: Small to large companies; small to large dev teams
•H...
What (I think) You Want to Know
•Available Tools? How much do they do?
•Low-hanging fruit; most difficulties?
•Cost/Benefi...
6
•Zend's Collective Experience
– Planning, Guiding, Implementing Migrations
Where this talk comes from
7
The Prospect of Migration...
8
•Write new ZF2 application from scratch?
•Run ZF1 and ZF2 side-by-side?
– Use ZF2 components in existing ZF1 application...
More Decisions!
•New URLs for new features of application: can do in ZF2, but will likely rely
upon existing ZF1 models, h...
10
No One Migration Strategy to Rule Them All
There is no single, one-size-fits all, “correct” migration path for all
appl...
Remember the Planned Migration Layer?
•Remember the planned Migration Layer?
– Was to allow ZF1 code to run on the ZF2 “en...
Some Tools Exist to Help
•ZF2 Documentation Page
http://framework.zend.com/manual/2.2/en/migration/overview.html
●
GitHub:...
13
ZF1 Compatibility Module - Overview
•Custom ZF2 Module that you write – simple, straightforward
•Use existing ZF1-based...
14
The Prospect of Migration...
Starting to Feel Better?
15
•What you do depends on what you want
– Delay a complete rewrite
●
Phased approach, reusing much of your ZF1 code
– Ref...
16
// config/application.config.php
'modules' => array(
'Application',
'Zf1', // <-- your ZF1 compatibility module
// you ...
17
// module/Zf1/Module.php
namespace Zf1;
use ZendModuleManagerFeatureAutoloaderProviderInterface;
use ZendMvcMvcEvent;
c...
18
// module/Zf1/Module.php getAutoloaderConfig()
return array(
'ZendLoaderStandardAutoloader' => array (
'namespaces' => ...
19
// module/Zf1/config/module.config.php
'service_manager' => array (
'factories' => array(
'zf1-db' => 'Zf1ServiceFactor...
20
ZF1 Compat. Module – Factories to Create Services
namespace Zf1ServiceFactory;
use ZendServiceManager;
class Database i...
21
ZF1 Compatibility Module – Register the Services
// module/Zf1/Module.php
public function onBootstrap(MvcEvent $e)
{
$s...
22
ZF1 Compatibility Module – Review
•Temporary, stop-gap measure for reusing ZF1 resources
•Avoids major up-front rewrite...
23
The Prospect of Migration...
How are you
feeling?
24
Reusing ZF1 Configuration - Overview
•ZF1 Configuration – .ini, .xml, .json, .yaml
•ZF2 Configuration – arrays
•Reuse e...
25
Reusing ZF1 Configuration - .ini files
// module/SomeModule/Module.php
public function getConfig ()
{
$path = __DIR__ ....
26
Reuse ZF1 Models - Autoloading
// module/SomeModule/Module.php
public function getAutoloaderConfig ()
{
return array(
'...
27
Reuse ZF1 Models – Merge config with ZF2 config
// module/SomeModule/Module.php
public function getConfig ()
{
include ...
Thank You!
Please give constructive feedback at
https://joind.in/9083
Email: clark.e@zend.com
https://github.com/clarkphp
...
Upcoming SlideShare
Loading in...5
×

Zend con practical-zf1-zf2-migration

1,728

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,728
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Zend con practical-zf1-zf2-migration

  1. 1. Practical ZF1 to ZF2 Migration: Lessons from the Field Clark Everetts, Zend Technologies, Inc.
  2. 2. 2 What are we going to talk about? Getting from Here to Here Higher-Level (ROI, Organizational, Planning, Goals, Constraints) Lower-Level (Coding Suggestions, Tools, Resources, Best Practices) Suggestions / Best Practices
  3. 3. 3 •Clark Everetts – Professional Services Consultant for Zend Technologies – Onsite and Remote Consulting – Linux, IBM iSeries, Windows ● Software Architecture Audits and Code Reviews ● Performance Audits ● Continuous Delivery Assessments – Online / Onsite Training in PHP, ZF, Studio, Server, etc. – SmartStarts: Training and Onsite Guided Development •Past Life: Independent Consultant (2002 - 2012) – Web Sites and Web Applications – Contract Instructor for Zend •Past, Past Life: Contractor for US Army and NASA (1990 - 2002) – Interceptor Ground Control Software; Green Screen Db Applications – Space Station: Real-time, Embedded Software; Science Platforms, Life Support Who am I?
  4. 4. 4 •Your role: Manager, developer, stakeholder/decision-maker? •Size: Small to large companies; small to large dev teams •Have you migrated applications from ZF1 to ZF2? •Migrating now? How far along in the process are you? •What challenges did you face / are you facing? •What tools did you use, or wish existed? •What did you do before you started? •Did you expect to accomplish anything besides “the migration?” Who are You?
  5. 5. What (I think) You Want to Know •Available Tools? How much do they do? •Low-hanging fruit; most difficulties? •Cost/Benefit? Expected gains? – Short-term? – Long-term? – When not to migrate? •How to plan a migration; what factors affect the planning? •Developer skills / skill levels required? •Budget, schedule is expected for a given project size? •Other resources?
  6. 6. 6 •Zend's Collective Experience – Planning, Guiding, Implementing Migrations Where this talk comes from
  7. 7. 7 The Prospect of Migration...
  8. 8. 8 •Write new ZF2 application from scratch? •Run ZF1 and ZF2 side-by-side? – Use ZF2 components in existing ZF1 application – Use ZF1 components in new ZF2 application – Some URLs (existing capabilities) ZF1, with other URLs (entirely new features) implemented in ZF2 – Convert models, controllers, utility functions, helpers, etc. •Which ZF2 components are easiest to use in a ZF1 app? Decisions, decisions...
  9. 9. More Decisions! •New URLs for new features of application: can do in ZF2, but will likely rely upon existing ZF1 models, helpers, etc., especially if you took the effort to make those components reusable in the first place •Build and test ZF2 version and then cut over from old site to new one, or iterate existing ZF1 production site into a new ZF2-based site? •Select a minimum PHP version – PHP 5.3.3 is minimum for ZF2; current are 5.5.4 and 5.4.20. PHP 5.3.27+ will receive only security fixes through much of 2014 (Zend supports PHP 5.2 for Enterprise customers on particular product versions; will continue to support PHP 5.3) – Consider most recent PHP version you can; this implies parallel migrations (PHP version as well as ZF2), Look at http://php.net/ChangeLog-5.php for removal of /changes to legacy features and new capabilities – Consider impact to other PHP apps on the server
  10. 10. 10 No One Migration Strategy to Rule Them All There is no single, one-size-fits all, “correct” migration path for all applications and organizations. Many factors, including project size, team size and experience with ZF1 and ZF2, structure and quality of the ZF1 codebase, all affect the path to take. * http://ligh7bulb.deviantart.com/ *
  11. 11. Remember the Planned Migration Layer? •Remember the planned Migration Layer? – Was to allow ZF1 code to run on the ZF2 “engine” – Work started during ZF2 betas; too much change between beta releases to keep such a tool current – Emulating ZF1 behaviour in ZF2 would have taken unacceptable amount of developer time away from ZF2 development •Though many people desired it, a full Migration Layer isn't strictly necessary – PHP 5.3 Namespaces – Migration: tedious, but not as hard as you think ● Depends on the code you're starting with! ● Fat controllers w/ data access, business logic, HTML :-(
  12. 12. Some Tools Exist to Help •ZF2 Documentation Page http://framework.zend.com/manual/2.2/en/migration/overview.html ● GitHub: EvanDotPro/zf-2-for-1 ZF2 Form View Helpers, could be extended to other features • GitHib: prolic/HumusMvcSkeletonApplication integrates ModuleManager and ServiceManager into ZF1 application See http://www.sasaprolic.com/2012/10/when-migration-to-zend- framework-2.html
  13. 13. 13 ZF1 Compatibility Module - Overview •Custom ZF2 Module that you write – simple, straightforward •Use existing ZF1-based application resources with minimal initial code rewrite: – Database – Translation – Session •That is, Any Zend_Application_Resource plugin •Obtain and use these resources as ZF2 Services via ServiceManager •Only for Application-wide, “global” resources
  14. 14. 14 The Prospect of Migration... Starting to Feel Better?
  15. 15. 15 •What you do depends on what you want – Delay a complete rewrite ● Phased approach, reusing much of your ZF1 code – Refactor and clean up “bad” code •Smaller Application, perhaps non mission-critical – Rewrite, use as a learning experience for a larger migration effort Planning
  16. 16. 16 // config/application.config.php 'modules' => array( 'Application', 'Zf1', // <-- your ZF1 compatibility module // you will add other modules to the list as you progress ), // remainder of config array ZF1 Compatibility Module – Add Module to config
  17. 17. 17 // module/Zf1/Module.php namespace Zf1; use ZendModuleManagerFeatureAutoloaderProviderInterface; use ZendMvcMvcEvent; class Module implements AutoloaderProviderInterface { public function getAutoloaderConfig() { $zf1path = getenv('ZF1_PATH'); // SetEnv in Apache vhost/.htaccess if (!$zf1path) { throw new Exception('Define ZF1 library path'); } /// more coming... ZF1 Compatibility Module – Autoload ZF1 Classes
  18. 18. 18 // module/Zf1/Module.php getAutoloaderConfig() return array( 'ZendLoaderStandardAutoloader' => array ( 'namespaces' => array( __NAMESPACE__ => __DIR__ . '/src/' . str_replace('', '/', __NAMESPACE__) ), 'prefixes' => array('Zend' => $zf1path) ) ); ZF1 Compatibility Module – Autoload ZF1 (cont.)
  19. 19. 19 // module/Zf1/config/module.config.php 'service_manager' => array ( 'factories' => array( 'zf1-db' => 'Zf1ServiceFactoryDatabase', 'zf1-translate'=> 'Zf1ServiceFactoryTranslate', // other ZF1-based resources... ), 'aliases' => array ( 'translate' => 'zf1-translate', ) ) ZF1 Compat. Module – Resources become Services
  20. 20. 20 ZF1 Compat. Module – Factories to Create Services namespace Zf1ServiceFactory; use ZendServiceManager; class Database implements ServiceManagerFactoryInterface { public function createService( ServiceManagerServiceLocatorInterface $serviceLocator ) { $config = $serviceLocator->get('config'); $adapter = Zend_Db::factory( $config['zf1-db']['adapter'], $config['zf1-db']['params'] ); return $adapter; } }
  21. 21. 21 ZF1 Compatibility Module – Register the Services // module/Zf1/Module.php public function onBootstrap(MvcEvent $e) { $services = $e->getApplication()->getServiceManager(); $dbAdapter = $services->get('zf1-db'); Zend_Db_Table_Abstract::setDefaultAdapter($dbAdapter); // Perhaps your views need config. For now, in views, access config // directly via registry. Later, use code that injects config into view $config = $services->get('config'); Zend_Registry::set('config', Util::arrayToObject($config)); // In audits, we often find ZF1 models accessing the db via registry // What you see here is not “best practice,” but a baby step toward // injecting this dependency in a later phase of migration $database = $services->get('zf1-db'); Zend_Registry::set('database', $database); $services = $e->getApplication()->getServiceManager(); Zend_Registry::set('session', $services->get('auth')->getStorage()- >read()); }
  22. 22. 22 ZF1 Compatibility Module – Review •Temporary, stop-gap measure for reusing ZF1 resources •Avoids major up-front rewrite •Obtain and use these resources as ZF2 Services via ServiceManager: – Add it to the list of modules used by your ZF2 app – Set up autoloading to find ZF1 library – Identify Resources as Services to the ServiceManager – Write Factories to create the services – Register the Services •Only for Application-wide, “global” resources •Remember: This Module will GO AWAY in a later phase!
  23. 23. 23 The Prospect of Migration... How are you feeling?
  24. 24. 24 Reusing ZF1 Configuration - Overview •ZF1 Configuration – .ini, .xml, .json, .yaml •ZF2 Configuration – arrays •Reuse existing ZF1 configuration files in ZF2 application
  25. 25. 25 Reusing ZF1 Configuration - .ini files // module/SomeModule/Module.php public function getConfig () { $path = __DIR__ . '/config/config.ini'; $config = new Zend_Config_Ini($path, $section); $oldConfig = $config->toArray(); if (defined('ZF1_COMPAT')) { $oldConfig['zf1-db'] = $oldConfig['db']; } // Fix 1: for the database options $dbParams = array ('driver' => $oldConfig['db']['adapter']); $dbParams = array_merge($dbParams, $oldConfig['db']['params']); $oldConfig['db'] = $dbParams; $phpConfig = include __DIR__ . '/config/module.config.php'; return array_merge($oldConfig, $phpConfig); }
  26. 26. 26 Reuse ZF1 Models - Autoloading // module/SomeModule/Module.php public function getAutoloaderConfig () { return array( 'ZendLoaderClassMapAutoloader' => array( __DIR__ . '/autoload_classmap.php' ), 'ZendLoaderStandardAutoloader' => array ( 'namespaces' => array( // if we're in a namespace deeper than one level we need to // fix the in the path __NAMESPACE__ => __DIR__ . '/src/' . str_replace('', '/', __NAMESPACE__) ), 'prefixes' => array(// This goes away in later phase 'Model' => __DIR__ .'/src/' . str_replace('', '/', __NAMESPACE__) . '/Model', ) ) ); }
  27. 27. 27 Reuse ZF1 Models – Merge config with ZF2 config // module/SomeModule/Module.php public function getConfig () { include __DIR__ . '/config/config.php'; $path = __DIR__ . '/config/config.ini'; $config = new Zend_Config_Ini($path, $section); $oldConfig = $config->toArray(); if (defined('ZF1_COMPAT')) { $oldConfig['zf1-db'] = $oldConfig['db']; } // database options from db section of config.ini $dbParams = array('driver' => $oldConfig['db']['adapter']); $dbParams = array_merge($dbParams, $oldConfig['db']['params']); $oldConfig['db'] = $dbParams; $phpConfig = include __DIR__ . '/config/module.config.php'; return array_merge($oldConfig, $phpConfig); } Opportunity for refactoring
  28. 28. Thank You! Please give constructive feedback at https://joind.in/9083 Email: clark.e@zend.com https://github.com/clarkphp Twitter: @clarkphp
  1. A particular slide catching your eye?

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

×