SlideShare ist ein Scribd-Unternehmen logo
1 von 32
ZF2 Modules and Services (And DI)
Maurice Kherlakian
Zend Technologies




                        © All rights reserved. Zend Technologies, Inc.
Who am I?
    • Zend Professional Services consultant
    • Worked with PHP for about 10 years
    • Based in Montreal




     Linkedin: http://ca.linkedin.com/in/mkherlakian
     Twitter: http://twitter.com/mkherlakian
     Email: maurice.k@zend.com




2   Zend Professional Services     © All rights reserved. Zend Technologies, Inc.
ZF2 Modules – What are they?

    • Discrete blocks of code
    • Allow for code re-use (not only within ZF2 but with other
      frameworks – Sf2 anyone?)
    • Physically a namespaced directory
    • If written well, distributable and re-usable
    • Examples:
        User module (authentication – login/logout…)

        ACL module

        Blog module



3   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Why are they better than ZF1’s modules

    • ZF1 modules had serious problems
    • Tightly coupled with MVC
    • Could not be easily reused
    • Bootstrapping expensive
    • Not self-contained
    • No dependency management


       ZF2 addresses all of these problems




4   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Requirements for a module – Module class
    • A namespaced class called Module

          <?php

          namespace Mymod;
          class Module {}


     • That’s it!




5   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Requirements for a module – Directory
    structure
    • There is, of course, a recommended directory structure




6   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Requirements for a module - Registration
    • In order for the application (Zend/Application) to run your module, it has
      to be registered
         <?php                                               //application.config.php

         return array(
            'modules' => array(                      //Which modules are we loading?
               'Application',
               'Mymod',
            ),
            'module_listener_options' => array(
               'config_glob_paths' => array(
                  'config/autoload/{,*.}{global,local}.php',
               ),
               'config_cache_enabled' => false,
               'cache_dir'        => 'data/cache',
               'module_paths' => array(                 //What paths are we loading modules from?
                  './module',                    //We will look for a Module.php file in subdirs
                  './vendor',                   //one level down from this dir *by default*
               ),
            ),
            'service_manager' => array(                   //More on that later
               'use_defaults' => true,
               'factories' => array(
               ),
            ),
         );




7   Zend Professional Services                          © All rights reserved. Zend Technologies, Inc.
Optional for a module – getConfig()
    • Called automatically to get the module’s configuration and aggregate it
      with the other modules
    • Best is to include a config file that returns a php array (best practice in
      ZF2 in general)
       <?php
       namespace Mymod;

       use ZendModuleManagerModuleManager,
         ZendMvcMvcEvent;

       class Module {
          public function getConfig() {
            return include(__DIR__.'/config/module.config.php');
          }
       }




8   Zend Professional Services                        © All rights reserved. Zend Technologies, Inc.
Optional for a module – init()
    • A module can have an init() method that fires upon initialization
    • The ModuleManager is passed to the init function as an argument
         <?php
         namespace Mymod;

         use ZendModuleManagerModuleManager,
           ZendMvcMvcEvent;

         class Module {
           public function init(ModuleManager $e) {
             $e->events()->attach(
                       'loadModules.post',
                       function($e) {
                          echo 'postLoad from MyMod';
                       },
                       100);
             $e->events()->getSharedManager()->attach(
                       __NAMESPACE__,
                       'dispatch',
                       function($e) {
                          echo "Only if dispatched to Mymod";
                       },
                       100);
           }
         }




9   Zend Professional Services                         © All rights reserved. Zend Technologies, Inc.
Optional for a module – onBootstrap()
     • And if init() is too early, ModuleManager automatically registers the
       onBootstrap method if found in the module
     • An MvcEvent is passed as the argument
        <?php
        namespace Mymod;

        use ZendModuleManagerModuleManager,
          ZendMvcMvcEvent;

        class Module {
           public function onBootstrap(MvcEvent $e) {
             $request = $e->getRequest();
           }
        }




10   Zend Professional Services                         © All rights reserved. Zend Technologies, Inc.
Optional for a module – Dep solving
     • Thru getProvides() and getDependencies()
     • Off by default, can be turned on
         <?php

         namespace Blog;
         class Module {
            public function getProvides()
            {
              return array(
                    'name' => 'Blog',
                    'version' => '0.1.0',
              );
            }

             public function getDependencies()
             {
               return array(
                     'php' => array(
                           'required' => true,
                           'version' => '>=5.3.1',
                     ),
                     'ext/mongo' => array(
                           'required' => true,
                           'version' => '>=1.2.0',
                     ),
               );
             }
         }




11   Zend Professional Services                      © All rights reserved. Zend Technologies, Inc.
ModuleManager

     • It’s automatically instantiated in your ZendApplication
     • It fulfills 3 functions:
         Aggregates enabled modules (allows you to iterate over them)

         Aggregates the configuration from each module

         Triggers the module initialization

     • There are many events for which the ModuleManager is
       passed as an argument
     • This allows you to provide listeners at practically any point
       in the code to get access to the manager



12   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
An MVC module
     • In most cases that’s what you will want
          <?php                                                               //module.config.php
          return array(
              'router' => array(
                 'routes' => array(
                 'Mymod' => array(
                    'type' => 'ZendMvcRouterHttpLiteral',
                 'options' => array(
                    'route' => '/mymod',
                    'defaults' => array(
                       'controller' => 'mymod',
                       'action' => 'index',
                       ),
                    ),
                 ),
              ),
            ),
            'controller' => array(
              'classes' => array(
                'mymod' => 'MymodControllerIndexController',
              ),
            ),
              'view_manager' => array(
                 'template_path_stack' => array(
                    __DIR__ . '/../view',
                 ),
              ),
          );



13   Zend Professional Services                         © All rights reserved. Zend Technologies, Inc.
The missing link - bootstrapping – index.php
          <?php                                                // /public/index.php

          require_once 'Loader/StandardAutoloader.php';
          use ZendLoaderStandardAutoloader,
            ZendServiceManagerServiceManager,
            ZendMvcServiceServiceManagerConfiguration;

          $loader = new StandardAutoloader();
          $loader->registerNamespace('Application', 'module/Application/src/Application');
          $loader->registerNamespace('Mymod', 'module/Mymod/src/Application');

          spl_autoload_register(array($loader, 'autoload'));

          // Get application stack configuration
          chdir(dirname(__DIR__));
          $configuration = include 'config/application.config.php';

          // Setup service manager
          $serviceManager = new ServiceManager(new ServiceManagerConfiguration($configuration['service_manager']));
          $serviceManager->setService('ApplicationConfiguration', $configuration);
          $serviceManager->get('ModuleManager')->loadModules();

          // Run application
          $serviceManager->get('Application')->bootstrap()->run()->send();




14   Zend Professional Services                           © All rights reserved. Zend Technologies, Inc.
The missing link - bootstrapping – index.php
     • Autoloading – it is the responsibility of index.php to set up autoloading
     • This is because modules have to be portable between different
       frameworks (reusability)
     • Any PSR-0 autoloader should work, it does not have to be ZF2’s
       autoloader
         For example Rob Allen’s Skeleton module uses Composer’s autoloader

     • The next lines are the ServiceManager’s setup (more on it later)




15   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Modules distribution
     • Any source you can think of
         Pyrus

         Git

         Http

         Tar & send

         Composer

     • There is already a ZF2 modules site
                                  http://modules.zendframework.com/
     (We need help! Want to contribute?)




16   Zend Professional Services           © All rights reserved. Zend Technologies, Inc.
Modules packaging

     • Tar/Zip
     • Phar (being careful that writes occur in location external to
       the archive)




17   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Location, location, location
     DI - short summary
     • Since the presentation was originally about ZendDi it’s only fair that we
       talk about it!
     • DI is the practice of reducing coupling between classes by injecting
       dependent classes in the subject instead of instantiating them

         <?php //No DI
         class DbAdapter {
            public function __construct($host, $user, $pass, $db) {
              //...
            }
         }

         class Author {
            protected $_db;

             public function __construct($host, $user, $pass, $db) {
               $this->_db = new DbAdapter($host, $user, $pass, $db);
             }
         }




18   Zend Professional Services                          © All rights reserved. Zend Technologies, Inc.
Location, location, location
     DI - short summary
         <?php //DI
         class DbAdapter {
            public function __construct($host, $user, $pass, $db) {
              //...
            }
         }

         class Author {
            protected $_db;

             public function __construct($db) {
               $this->_db = $db;
             }
         }

         $db = new DbAdapter();
         $author = new Author($db);




     • That’s all it is – Really! Makes it easy to substitute the DB class for mocking, or to change it all
       together for instance, and promotes decoupling and reusability. The D in SOLID.




19   Zend Professional Services                         © All rights reserved. Zend Technologies, Inc.
Location, location, location
     DI - short summary
     • Consider the case that an Author needs a DbGateway, which needs a DbAdapter, and a logging
       and caching class


         <?php

         $db = new DbAdapter();
         $gateway = new DbGateway($db);
         $cache = new Cache();
         $log = new Log();

         $author = new Author($db, $gateway, $cache, $log);
         $authors = $author->fetchAll();



       • 5 lines of code for initialization, but only one line for what we really need to do. Lots of
         boilerplate code
       • Enter Dependency Injection Containers, or DiC – ZendDi is one of them, so is the Symfony
         DiC, and a number of others




20    Zend Professional Services                      © All rights reserved. Zend Technologies, Inc.
Location, location, location
     DiC – ZendDiDi()
          <?php

          $config = array(
               'instance' => array(
                  'Author' => array(
                     'parameters' => array(
                         'gateway' => 'DbGateway',
                         'cache' => 'Cache',
                         'log' => 'Log'
                      )
                  ),
                  'DbGateway' => array(
                     'parameters' => array(
                        'db' => 'DbAdapter',
                     )
                  ),
                  'DbAdapter' => array(
                     'parameters' => array(
                        'host' => 'somehost',
                        'user' => 'someuser',
                        'pass' => 'somepass',
                        'db' => ‘somedb'
                     )
                  ),
                  'Cache' => array(
                  ),
                  'Log' => array(
                  )
               )
            );

          $di = new ZendDiDi();
          $di->configure(new ZendDiConfiguration($config));

          $author = $di->get('Author');




21   Zend Professional Services                                 © All rights reserved. Zend Technologies, Inc.
Location, location, location
     DiC – ZendDiDi()
     • The last line on the previous slide:
                    $author = $di->get('Author');
     Returns a fully configured Author object, following the definition we provided
     • DiC does instantiation and auto-wiring
     • ZendDi, although difficult to show here, is a complex solution for complex
       problems – it can easily be abused
     • For simpler problems, and more in line with the PHP philosophy, ServiceManager
       replaced it




22   Zend Professional Services          © All rights reserved. Zend Technologies, Inc.
Location, location, location
     Location v/s Injection
     • Service manager is a ServiceLocator !== DiC
     • BUT DiC === Service Locator
     • One main difference is that the subject requiring the dependencies is:
         Not aware of the DiC when using DI (the dependency is injected)

         Is aware of the locator in the case of SL, and asks the locator to obtain the dependency

     • ServiceManager is a service locator
     Think back to index.php:
                    $serviceManager->get('ModuleManager')->loadModules();


     • Martin Fowler is the subject matter expert on this:
       http://martinfowler.com/articles/injection.html




23   Zend Professional Services         © All rights reserved. Zend Technologies, Inc.
Location, location, location
     ZendServiceManagerServiceManager()
     • Service manager uses factories to generate its services
     • Factory can be a class implementing ZendServiceManagerFactoryInterface or a
       php closure




24   Zend Professional Services    © All rights reserved. Zend Technologies, Inc.
Location, location, location
     ZendServiceManagerServiceManager()
          <?php
          class ServiceConfiguration extends Configuration
          {
             public function configureServiceManager(ServiceManager $serviceManager)
             {
               $serviceManager->setFactory('author', function(ServiceManager $sm) {
                   $dbGateway = $sm->get('db_gateway');
                   $cache = $sm->get('cache');
                   $log = $sm->get('log');
                   return new Author($dbGateway, $cache, $log);
               } );
               $serviceManager->setShared('author', false);
               $serviceManager->setFactory('db_gateway', function(ServiceManager $sm){
                   $dbAdapter = $sm->get('db_adapter');
                   return new DbGateway($dbAdapter);
               } );
               $serviceManager->setFactory('db_adapter', function(ServiceManager $sm) {
                   //Using hard-coded values for the example, but normally you would either create a factory
                   //and inject the values from a config file
                   //or get the configuration from the ServiceManager and read the valuees in
                   return new DbAdapter('somehost', 'someuser', 'somepass', 'somedb');
               } );
               $serviceManager->setFactory('cache', function(ServiceManager $sm) { return new Cache(); } );
               $serviceManager->setFactory('log', function(ServiceManager $sm) { return new Log(); } );
             }
          }

          $config = new ServiceConfiguration();
          $sm = new ServiceManager();
          $config->configureServiceManager($sm);

          $author = $sm->get('author');




25   Zend Professional Services                             © All rights reserved. Zend Technologies, Inc.
Location, location, location
     ZendServiceManagerServiceManager()
     • The example above purposely uses closures to illustrate that a full-fledged
       factory is not needed
     • But in a lot of cases, implementing a factory makes sense - rewriting DbAdapter
       from the above example as a factory:

         <?php
         use ZendServiceManagerFactoryInterface;

         //The factory class
         class AuthorFactory implements FactoryInterface {
            public function createService(ZendServiceManagerServiceLocatorInterface $sl) {
              $dbGateway = $sl->get('db_gateway');
              $cache = $sl->get('cache');
              $log = $sl->get('log');
              return new Author($dbGateway, $cache, $log);
            }
         }

         //And the configuration class
         class ServiceConfiguration extends Configuration
         {
            public function configureServiceManager(ServiceManager $serviceManager)
            {
               //...
               $serviceManager->setFactory('author', 'AuthorFactory');
               //...
             }
         }



26   Zend Professional Services                             © All rights reserved. Zend Technologies, Inc.
Location, location, location
     ZendServiceManagerServiceManager()
     • The example above purposely uses closures to illustrate that a full-fledged
       factory is not needed
     • But in a lot of cases, implementing a factory makes sense - rewriting DbAdapter
       form the above example as a factory:

         <?php
         use ZendServiceManagerFactoryInterface;

         //The factory class
         class AuthorFactory implements FactoryInterface {
            public function createService(ZendServiceManagerServiceLocatorInterface $sl) {
              $dbGateway = $sl->get('db_gateway');
              $cache = $sl->get('cache');
              $log = $sl->get('log');
              return new Author($dbGateway, $cache, $log);
            }
         }

         //And the configuration class
         class ServiceConfiguration extends Configuration
         {
            public function configureServiceManager(ServiceManager $serviceManager)
            {
               //...
               $serviceManager->setFactory('author', 'AuthorFactory');
               //...
             }
         }



27   Zend Professional Services                             © All rights reserved. Zend Technologies, Inc.
Location, location, location
     Setting and retrieving the locator
     • In most MVC components (e.g.Controllers) the ServiceManager
       component is composed automatically by the MVC stack
     • The interface ZendServiceManagerServiceLocatorAwareInterface can
       be implemented to ensure that a service locator is composed in the
       subject class
     • Two methods, SetServiceLocator(ServiceLocatorInterface $locator) and
       getServiceLocator() must be implemented
     • Notice that the Component is ServiceManager(), but the interface is
       ServiceLocatorInterface. This is to allow you to provide an alternative
       implementation of service locator.




28   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Location, location, location
     Setting and retrieving the locator
     • Controllers implement ServiceLocatorAwareInterface therefore

         <?php

         namespace MymodController;

         use ZendMvcControllerActionController,
           ZendViewModelViewModel;

         class IndexController extends ActionController
         {
            public function indexAction()
            {
              $sl = $this->getServiceLocator();
              $sl->get('author');
              return new ViewModel();
            }
         }




29   Zend Professional Services                      © All rights reserved. Zend Technologies, Inc.
Locating the injector or injecting the
     locator?
     • Although ServiceManager is now the goto dependency management
       component in ZF2, Di actually still exists as a fallback (by default)
     • One can specify a collection AbstractFactories to ServiceManager on
       which it will fall back if it does not find the target class
     • Therefore, ZendServiceManagerDiDiAbstractFactory is an abstract
       factory to which the name of the object is passed if it is not found by
       ServiceManager (the order can be changed)
     • You can also, of course, provide your own service factory (Proxy class to
       other frameworks maybe?)




30   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
Some references
     • ZF2 module site: http://modules.zendframework.com/
     • Rob Allen’s Skeleton:
       https://github.com/zendframework/ZendSkeletonApplication
     • Matthew’s discussion on Modules: http://mwop.net/blog/267-Getting-
       started-writing-ZF2-modules.html
     • Ralph on the decisions that drove to ServiceManager: http://zend-
       framework-community.634137.n4.nabble.com/Services-Instances-
       Dependencies-in-ZF2-td4584632.html
     • SOLID OO design: http://en.wikipedia.org/wiki/SOLID_(object-
       oriented_design)




31   Zend Professional Services   © All rights reserved. Zend Technologies, Inc.
• Download ZF2 and happy coding!
     http://zendframework.com/zf2




                                                Thank You!
                                  http://twitter.com/mkherlakian




32   Zend Professional Services         © All rights reserved. Zend Technologies, Inc.

Weitere ähnliche Inhalte

Was ist angesagt?

Re-Design with Elixir/OTP
Re-Design with Elixir/OTPRe-Design with Elixir/OTP
Re-Design with Elixir/OTPMustafa TURAN
 
Quick start on Zend Framework 2
Quick start on Zend Framework 2Quick start on Zend Framework 2
Quick start on Zend Framework 2Enrico Zimuel
 
Deprecated: Foundations of Zend Framework 2
Deprecated: Foundations of Zend Framework 2Deprecated: Foundations of Zend Framework 2
Deprecated: Foundations of Zend Framework 2Adam Culp
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Stefano Valle
 
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)ZFConf Conference
 
Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Ryan Mauger
 
Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Ryan Mauger
 
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"Fwdays
 
How to build customizable multitenant web applications - PHPBNL11
How to build customizable multitenant web applications - PHPBNL11How to build customizable multitenant web applications - PHPBNL11
How to build customizable multitenant web applications - PHPBNL11Stephan Hochdörfer
 
Projet d'accès aux résultats des étudiant via client mobile
Projet d'accès aux résultats des étudiant via client mobile Projet d'accès aux résultats des étudiant via client mobile
Projet d'accès aux résultats des étudiant via client mobile Patrick Bashizi
 
Apigility reloaded
Apigility reloadedApigility reloaded
Apigility reloadedRalf Eggert
 
Zend Framework Introduction
Zend Framework IntroductionZend Framework Introduction
Zend Framework IntroductionRafael Monteiro
 
Java Code Generation for Productivity
Java Code Generation for ProductivityJava Code Generation for Productivity
Java Code Generation for ProductivityDavid Noble
 
CUDA lab's slides of "parallel programming" course
CUDA lab's slides of "parallel programming" courseCUDA lab's slides of "parallel programming" course
CUDA lab's slides of "parallel programming" courseShuai Yuan
 

Was ist angesagt? (20)

Re-Design with Elixir/OTP
Re-Design with Elixir/OTPRe-Design with Elixir/OTP
Re-Design with Elixir/OTP
 
Quick start on Zend Framework 2
Quick start on Zend Framework 2Quick start on Zend Framework 2
Quick start on Zend Framework 2
 
Deprecated: Foundations of Zend Framework 2
Deprecated: Foundations of Zend Framework 2Deprecated: Foundations of Zend Framework 2
Deprecated: Foundations of Zend Framework 2
 
Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2Instant ACLs with Zend Framework 2
Instant ACLs with Zend Framework 2
 
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
ZFConf 2012: Zend Framework 2, a quick start (Enrico Zimuel)
 
Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)Webinar: Zend framework Getting to grips (ZF1)
Webinar: Zend framework Getting to grips (ZF1)
 
Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)Zend framework: Getting to grips (ZF1)
Zend framework: Getting to grips (ZF1)
 
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
Ростислав Михайлив "Zend Framework 3 - evolution or revolution"
 
How to build customizable multitenant web applications - PHPBNL11
How to build customizable multitenant web applications - PHPBNL11How to build customizable multitenant web applications - PHPBNL11
How to build customizable multitenant web applications - PHPBNL11
 
Zend Framework 2 Patterns
Zend Framework 2 PatternsZend Framework 2 Patterns
Zend Framework 2 Patterns
 
Download It
Download ItDownload It
Download It
 
Projet d'accès aux résultats des étudiant via client mobile
Projet d'accès aux résultats des étudiant via client mobile Projet d'accès aux résultats des étudiant via client mobile
Projet d'accès aux résultats des étudiant via client mobile
 
How to fake_properly
How to fake_properlyHow to fake_properly
How to fake_properly
 
Apigility reloaded
Apigility reloadedApigility reloaded
Apigility reloaded
 
Zend Framework Introduction
Zend Framework IntroductionZend Framework Introduction
Zend Framework Introduction
 
Java Code Generation for Productivity
Java Code Generation for ProductivityJava Code Generation for Productivity
Java Code Generation for Productivity
 
JavaFXScript
JavaFXScriptJavaFXScript
JavaFXScript
 
ZF2 Presentation @PHP Tour 2011 in Lille
ZF2 Presentation @PHP Tour 2011 in LilleZF2 Presentation @PHP Tour 2011 in Lille
ZF2 Presentation @PHP Tour 2011 in Lille
 
J2SE 5
J2SE 5J2SE 5
J2SE 5
 
CUDA lab's slides of "parallel programming" course
CUDA lab's slides of "parallel programming" courseCUDA lab's slides of "parallel programming" course
CUDA lab's slides of "parallel programming" course
 

Andere mochten auch

Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slidesmkherlakian
 
High performance PHP: Scaling and getting the most out of your infrastructure
High performance PHP: Scaling and getting the most out of your infrastructureHigh performance PHP: Scaling and getting the most out of your infrastructure
High performance PHP: Scaling and getting the most out of your infrastructuremkherlakian
 
Zend con eu keynote (publish)
Zend con eu keynote (publish)Zend con eu keynote (publish)
Zend con eu keynote (publish)mkherlakian
 
Big server-is-watching-you
Big server-is-watching-youBig server-is-watching-you
Big server-is-watching-youmkherlakian
 
Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...
Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...
Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...Alexey Petrov
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsLinkedIn
 

Andere mochten auch (8)

Zend Con 2008 Slides
Zend Con 2008 SlidesZend Con 2008 Slides
Zend Con 2008 Slides
 
High performance PHP: Scaling and getting the most out of your infrastructure
High performance PHP: Scaling and getting the most out of your infrastructureHigh performance PHP: Scaling and getting the most out of your infrastructure
High performance PHP: Scaling and getting the most out of your infrastructure
 
Zend con eu keynote (publish)
Zend con eu keynote (publish)Zend con eu keynote (publish)
Zend con eu keynote (publish)
 
Big server-is-watching-you
Big server-is-watching-youBig server-is-watching-you
Big server-is-watching-you
 
Php optimization
Php optimizationPhp optimization
Php optimization
 
Nginx pres
Nginx presNginx pres
Nginx pres
 
Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...
Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...
Dockerize Me: Distributed PHP applications with Symfony, Docker, Consul and A...
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 

Ähnlich wie Zf2 phpquebec

Foundations of Zend Framework
Foundations of Zend FrameworkFoundations of Zend Framework
Foundations of Zend FrameworkAdam Culp
 
uRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orguRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orgAgelos Pikoulas
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your projectMichelangelo van Dam
 
Getting up & running with zend framework
Getting up & running with zend frameworkGetting up & running with zend framework
Getting up & running with zend frameworkSaidur Rahman
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the TrenchesJonathan Wage
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenchesLukas Smith
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHPPaul Jones
 
Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS偉格 高
 
JavaScript Modules in Practice
JavaScript Modules in PracticeJavaScript Modules in Practice
JavaScript Modules in PracticeMaghdebura
 
Z-Ray: A customizable development tool belt (Zendcon 2016)
Z-Ray: A customizable development tool belt (Zendcon 2016)Z-Ray: A customizable development tool belt (Zendcon 2016)
Z-Ray: A customizable development tool belt (Zendcon 2016)Mathew Beane
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.jsChris Cowan
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshopjulien pauli
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend frameworkAlan Seiden
 
Tools and Tips for Moodle Developers - #mootus16
 Tools and Tips for Moodle Developers - #mootus16 Tools and Tips for Moodle Developers - #mootus16
Tools and Tips for Moodle Developers - #mootus16Dan Poltawski
 
Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)xMartin12
 
2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps2010 07-28-testing-zf-apps
2010 07-28-testing-zf-appsVenkata Ramana
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolGordon Forsythe
 

Ähnlich wie Zf2 phpquebec (20)

Foundations of Zend Framework
Foundations of Zend FrameworkFoundations of Zend Framework
Foundations of Zend Framework
 
uRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.orguRequire@greecejs: An introduction to http://uRequire.org
uRequire@greecejs: An introduction to http://uRequire.org
 
Zf2 how arrays will save your project
Zf2   how arrays will save your projectZf2   how arrays will save your project
Zf2 how arrays will save your project
 
Getting up & running with zend framework
Getting up & running with zend frameworkGetting up & running with zend framework
Getting up & running with zend framework
 
Getting up and running with Zend Framework
Getting up and running with Zend FrameworkGetting up and running with Zend Framework
Getting up and running with Zend Framework
 
Symfony2 from the Trenches
Symfony2 from the TrenchesSymfony2 from the Trenches
Symfony2 from the Trenches
 
Symfony2 - from the trenches
Symfony2 - from the trenchesSymfony2 - from the trenches
Symfony2 - from the trenches
 
Decoupled Libraries for PHP
Decoupled Libraries for PHPDecoupled Libraries for PHP
Decoupled Libraries for PHP
 
Zend
ZendZend
Zend
 
Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS
 
JavaScript Modules in Practice
JavaScript Modules in PracticeJavaScript Modules in Practice
JavaScript Modules in Practice
 
Z-Ray: A customizable development tool belt (Zendcon 2016)
Z-Ray: A customizable development tool belt (Zendcon 2016)Z-Ray: A customizable development tool belt (Zendcon 2016)
Z-Ray: A customizable development tool belt (Zendcon 2016)
 
Intro To Node.js
Intro To Node.jsIntro To Node.js
Intro To Node.js
 
JavaCro'14 - Using WildFly core to build high performance web server – Tomaž ...
JavaCro'14 - Using WildFly core to build high performance web server – Tomaž ...JavaCro'14 - Using WildFly core to build high performance web server – Tomaž ...
JavaCro'14 - Using WildFly core to build high performance web server – Tomaž ...
 
Php7 extensions workshop
Php7 extensions workshopPhp7 extensions workshop
Php7 extensions workshop
 
Performance tuning with zend framework
Performance tuning with zend frameworkPerformance tuning with zend framework
Performance tuning with zend framework
 
Tools and Tips for Moodle Developers - #mootus16
 Tools and Tips for Moodle Developers - #mootus16 Tools and Tips for Moodle Developers - #mootus16
Tools and Tips for Moodle Developers - #mootus16
 
Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)Asynchronous Module Definition (AMD)
Asynchronous Module Definition (AMD)
 
2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps2010 07-28-testing-zf-apps
2010 07-28-testing-zf-apps
 
Zend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_ToolZend Framework 1.9 Setup & Using Zend_Tool
Zend Framework 1.9 Setup & Using Zend_Tool
 

Kürzlich hochgeladen

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 

Kürzlich hochgeladen (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 

Zf2 phpquebec

  • 1. ZF2 Modules and Services (And DI) Maurice Kherlakian Zend Technologies © All rights reserved. Zend Technologies, Inc.
  • 2. Who am I? • Zend Professional Services consultant • Worked with PHP for about 10 years • Based in Montreal Linkedin: http://ca.linkedin.com/in/mkherlakian Twitter: http://twitter.com/mkherlakian Email: maurice.k@zend.com 2 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 3. ZF2 Modules – What are they? • Discrete blocks of code • Allow for code re-use (not only within ZF2 but with other frameworks – Sf2 anyone?) • Physically a namespaced directory • If written well, distributable and re-usable • Examples:  User module (authentication – login/logout…)  ACL module  Blog module 3 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 4. Why are they better than ZF1’s modules • ZF1 modules had serious problems • Tightly coupled with MVC • Could not be easily reused • Bootstrapping expensive • Not self-contained • No dependency management ZF2 addresses all of these problems 4 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 5. Requirements for a module – Module class • A namespaced class called Module <?php namespace Mymod; class Module {} • That’s it! 5 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 6. Requirements for a module – Directory structure • There is, of course, a recommended directory structure 6 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 7. Requirements for a module - Registration • In order for the application (Zend/Application) to run your module, it has to be registered <?php //application.config.php return array( 'modules' => array( //Which modules are we loading? 'Application', 'Mymod', ), 'module_listener_options' => array( 'config_glob_paths' => array( 'config/autoload/{,*.}{global,local}.php', ), 'config_cache_enabled' => false, 'cache_dir' => 'data/cache', 'module_paths' => array( //What paths are we loading modules from? './module', //We will look for a Module.php file in subdirs './vendor', //one level down from this dir *by default* ), ), 'service_manager' => array( //More on that later 'use_defaults' => true, 'factories' => array( ), ), ); 7 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 8. Optional for a module – getConfig() • Called automatically to get the module’s configuration and aggregate it with the other modules • Best is to include a config file that returns a php array (best practice in ZF2 in general) <?php namespace Mymod; use ZendModuleManagerModuleManager, ZendMvcMvcEvent; class Module { public function getConfig() { return include(__DIR__.'/config/module.config.php'); } } 8 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 9. Optional for a module – init() • A module can have an init() method that fires upon initialization • The ModuleManager is passed to the init function as an argument <?php namespace Mymod; use ZendModuleManagerModuleManager, ZendMvcMvcEvent; class Module { public function init(ModuleManager $e) { $e->events()->attach( 'loadModules.post', function($e) { echo 'postLoad from MyMod'; }, 100); $e->events()->getSharedManager()->attach( __NAMESPACE__, 'dispatch', function($e) { echo "Only if dispatched to Mymod"; }, 100); } } 9 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 10. Optional for a module – onBootstrap() • And if init() is too early, ModuleManager automatically registers the onBootstrap method if found in the module • An MvcEvent is passed as the argument <?php namespace Mymod; use ZendModuleManagerModuleManager, ZendMvcMvcEvent; class Module { public function onBootstrap(MvcEvent $e) { $request = $e->getRequest(); } } 10 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 11. Optional for a module – Dep solving • Thru getProvides() and getDependencies() • Off by default, can be turned on <?php namespace Blog; class Module { public function getProvides() { return array( 'name' => 'Blog', 'version' => '0.1.0', ); } public function getDependencies() { return array( 'php' => array( 'required' => true, 'version' => '>=5.3.1', ), 'ext/mongo' => array( 'required' => true, 'version' => '>=1.2.0', ), ); } } 11 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 12. ModuleManager • It’s automatically instantiated in your ZendApplication • It fulfills 3 functions:  Aggregates enabled modules (allows you to iterate over them)  Aggregates the configuration from each module  Triggers the module initialization • There are many events for which the ModuleManager is passed as an argument • This allows you to provide listeners at practically any point in the code to get access to the manager 12 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 13. An MVC module • In most cases that’s what you will want <?php //module.config.php return array( 'router' => array( 'routes' => array( 'Mymod' => array( 'type' => 'ZendMvcRouterHttpLiteral', 'options' => array( 'route' => '/mymod', 'defaults' => array( 'controller' => 'mymod', 'action' => 'index', ), ), ), ), ), 'controller' => array( 'classes' => array( 'mymod' => 'MymodControllerIndexController', ), ), 'view_manager' => array( 'template_path_stack' => array( __DIR__ . '/../view', ), ), ); 13 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 14. The missing link - bootstrapping – index.php <?php // /public/index.php require_once 'Loader/StandardAutoloader.php'; use ZendLoaderStandardAutoloader, ZendServiceManagerServiceManager, ZendMvcServiceServiceManagerConfiguration; $loader = new StandardAutoloader(); $loader->registerNamespace('Application', 'module/Application/src/Application'); $loader->registerNamespace('Mymod', 'module/Mymod/src/Application'); spl_autoload_register(array($loader, 'autoload')); // Get application stack configuration chdir(dirname(__DIR__)); $configuration = include 'config/application.config.php'; // Setup service manager $serviceManager = new ServiceManager(new ServiceManagerConfiguration($configuration['service_manager'])); $serviceManager->setService('ApplicationConfiguration', $configuration); $serviceManager->get('ModuleManager')->loadModules(); // Run application $serviceManager->get('Application')->bootstrap()->run()->send(); 14 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 15. The missing link - bootstrapping – index.php • Autoloading – it is the responsibility of index.php to set up autoloading • This is because modules have to be portable between different frameworks (reusability) • Any PSR-0 autoloader should work, it does not have to be ZF2’s autoloader  For example Rob Allen’s Skeleton module uses Composer’s autoloader • The next lines are the ServiceManager’s setup (more on it later) 15 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 16. Modules distribution • Any source you can think of  Pyrus  Git  Http  Tar & send  Composer • There is already a ZF2 modules site http://modules.zendframework.com/ (We need help! Want to contribute?) 16 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 17. Modules packaging • Tar/Zip • Phar (being careful that writes occur in location external to the archive) 17 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 18. Location, location, location DI - short summary • Since the presentation was originally about ZendDi it’s only fair that we talk about it! • DI is the practice of reducing coupling between classes by injecting dependent classes in the subject instead of instantiating them <?php //No DI class DbAdapter { public function __construct($host, $user, $pass, $db) { //... } } class Author { protected $_db; public function __construct($host, $user, $pass, $db) { $this->_db = new DbAdapter($host, $user, $pass, $db); } } 18 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 19. Location, location, location DI - short summary <?php //DI class DbAdapter { public function __construct($host, $user, $pass, $db) { //... } } class Author { protected $_db; public function __construct($db) { $this->_db = $db; } } $db = new DbAdapter(); $author = new Author($db); • That’s all it is – Really! Makes it easy to substitute the DB class for mocking, or to change it all together for instance, and promotes decoupling and reusability. The D in SOLID. 19 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 20. Location, location, location DI - short summary • Consider the case that an Author needs a DbGateway, which needs a DbAdapter, and a logging and caching class <?php $db = new DbAdapter(); $gateway = new DbGateway($db); $cache = new Cache(); $log = new Log(); $author = new Author($db, $gateway, $cache, $log); $authors = $author->fetchAll(); • 5 lines of code for initialization, but only one line for what we really need to do. Lots of boilerplate code • Enter Dependency Injection Containers, or DiC – ZendDi is one of them, so is the Symfony DiC, and a number of others 20 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 21. Location, location, location DiC – ZendDiDi() <?php $config = array( 'instance' => array( 'Author' => array( 'parameters' => array( 'gateway' => 'DbGateway', 'cache' => 'Cache', 'log' => 'Log' ) ), 'DbGateway' => array( 'parameters' => array( 'db' => 'DbAdapter', ) ), 'DbAdapter' => array( 'parameters' => array( 'host' => 'somehost', 'user' => 'someuser', 'pass' => 'somepass', 'db' => ‘somedb' ) ), 'Cache' => array( ), 'Log' => array( ) ) ); $di = new ZendDiDi(); $di->configure(new ZendDiConfiguration($config)); $author = $di->get('Author'); 21 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 22. Location, location, location DiC – ZendDiDi() • The last line on the previous slide: $author = $di->get('Author'); Returns a fully configured Author object, following the definition we provided • DiC does instantiation and auto-wiring • ZendDi, although difficult to show here, is a complex solution for complex problems – it can easily be abused • For simpler problems, and more in line with the PHP philosophy, ServiceManager replaced it 22 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 23. Location, location, location Location v/s Injection • Service manager is a ServiceLocator !== DiC • BUT DiC === Service Locator • One main difference is that the subject requiring the dependencies is:  Not aware of the DiC when using DI (the dependency is injected)  Is aware of the locator in the case of SL, and asks the locator to obtain the dependency • ServiceManager is a service locator Think back to index.php: $serviceManager->get('ModuleManager')->loadModules(); • Martin Fowler is the subject matter expert on this: http://martinfowler.com/articles/injection.html 23 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 24. Location, location, location ZendServiceManagerServiceManager() • Service manager uses factories to generate its services • Factory can be a class implementing ZendServiceManagerFactoryInterface or a php closure 24 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 25. Location, location, location ZendServiceManagerServiceManager() <?php class ServiceConfiguration extends Configuration { public function configureServiceManager(ServiceManager $serviceManager) { $serviceManager->setFactory('author', function(ServiceManager $sm) { $dbGateway = $sm->get('db_gateway'); $cache = $sm->get('cache'); $log = $sm->get('log'); return new Author($dbGateway, $cache, $log); } ); $serviceManager->setShared('author', false); $serviceManager->setFactory('db_gateway', function(ServiceManager $sm){ $dbAdapter = $sm->get('db_adapter'); return new DbGateway($dbAdapter); } ); $serviceManager->setFactory('db_adapter', function(ServiceManager $sm) { //Using hard-coded values for the example, but normally you would either create a factory //and inject the values from a config file //or get the configuration from the ServiceManager and read the valuees in return new DbAdapter('somehost', 'someuser', 'somepass', 'somedb'); } ); $serviceManager->setFactory('cache', function(ServiceManager $sm) { return new Cache(); } ); $serviceManager->setFactory('log', function(ServiceManager $sm) { return new Log(); } ); } } $config = new ServiceConfiguration(); $sm = new ServiceManager(); $config->configureServiceManager($sm); $author = $sm->get('author'); 25 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 26. Location, location, location ZendServiceManagerServiceManager() • The example above purposely uses closures to illustrate that a full-fledged factory is not needed • But in a lot of cases, implementing a factory makes sense - rewriting DbAdapter from the above example as a factory: <?php use ZendServiceManagerFactoryInterface; //The factory class class AuthorFactory implements FactoryInterface { public function createService(ZendServiceManagerServiceLocatorInterface $sl) { $dbGateway = $sl->get('db_gateway'); $cache = $sl->get('cache'); $log = $sl->get('log'); return new Author($dbGateway, $cache, $log); } } //And the configuration class class ServiceConfiguration extends Configuration { public function configureServiceManager(ServiceManager $serviceManager) { //... $serviceManager->setFactory('author', 'AuthorFactory'); //... } } 26 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 27. Location, location, location ZendServiceManagerServiceManager() • The example above purposely uses closures to illustrate that a full-fledged factory is not needed • But in a lot of cases, implementing a factory makes sense - rewriting DbAdapter form the above example as a factory: <?php use ZendServiceManagerFactoryInterface; //The factory class class AuthorFactory implements FactoryInterface { public function createService(ZendServiceManagerServiceLocatorInterface $sl) { $dbGateway = $sl->get('db_gateway'); $cache = $sl->get('cache'); $log = $sl->get('log'); return new Author($dbGateway, $cache, $log); } } //And the configuration class class ServiceConfiguration extends Configuration { public function configureServiceManager(ServiceManager $serviceManager) { //... $serviceManager->setFactory('author', 'AuthorFactory'); //... } } 27 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 28. Location, location, location Setting and retrieving the locator • In most MVC components (e.g.Controllers) the ServiceManager component is composed automatically by the MVC stack • The interface ZendServiceManagerServiceLocatorAwareInterface can be implemented to ensure that a service locator is composed in the subject class • Two methods, SetServiceLocator(ServiceLocatorInterface $locator) and getServiceLocator() must be implemented • Notice that the Component is ServiceManager(), but the interface is ServiceLocatorInterface. This is to allow you to provide an alternative implementation of service locator. 28 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 29. Location, location, location Setting and retrieving the locator • Controllers implement ServiceLocatorAwareInterface therefore <?php namespace MymodController; use ZendMvcControllerActionController, ZendViewModelViewModel; class IndexController extends ActionController { public function indexAction() { $sl = $this->getServiceLocator(); $sl->get('author'); return new ViewModel(); } } 29 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 30. Locating the injector or injecting the locator? • Although ServiceManager is now the goto dependency management component in ZF2, Di actually still exists as a fallback (by default) • One can specify a collection AbstractFactories to ServiceManager on which it will fall back if it does not find the target class • Therefore, ZendServiceManagerDiDiAbstractFactory is an abstract factory to which the name of the object is passed if it is not found by ServiceManager (the order can be changed) • You can also, of course, provide your own service factory (Proxy class to other frameworks maybe?) 30 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 31. Some references • ZF2 module site: http://modules.zendframework.com/ • Rob Allen’s Skeleton: https://github.com/zendframework/ZendSkeletonApplication • Matthew’s discussion on Modules: http://mwop.net/blog/267-Getting- started-writing-ZF2-modules.html • Ralph on the decisions that drove to ServiceManager: http://zend- framework-community.634137.n4.nabble.com/Services-Instances- Dependencies-in-ZF2-td4584632.html • SOLID OO design: http://en.wikipedia.org/wiki/SOLID_(object- oriented_design) 31 Zend Professional Services © All rights reserved. Zend Technologies, Inc.
  • 32. • Download ZF2 and happy coding! http://zendframework.com/zf2 Thank You! http://twitter.com/mkherlakian 32 Zend Professional Services © All rights reserved. Zend Technologies, Inc.