Writing Services with
         ZF2
Mike Willbanks | Barnes & Noble
Housekeeping…

    •  Talk
      Slides will be posted after the talk.

    •  Me
      Sr. Web Architect Manager at NOOK Developer

      Open Source Contributor

      Where you can find me:
        •  Twitter: mwillbanks          G+: Mike Willbanks
        •  IRC (freenode): mwillbanks   Blog: http://blog.digitalstruct.com
        •  GitHub: https://github.com/mwillbanks




2
Agenda

    •  Background
    •  ZF2 Services
    •  Writing a Service
    •  Implementing your Service




3
Background…
A Definition
    ZF 1 contained Zend_Service_* components



5
Zend_Service_*
    Components were included in the core
    distribution


6
Zend_Service_*
    Make it easy for people to have it available…



7
1.11.13 Release
    Caused several issues



8
Started Getting Messy…




9
Services in ZF2
Changing for the Better

     • RFC March 2012 “RFC Service Components”
        http://framework.zend.com/wiki/display/ZFDEV2/RFC+-+Service
        +Components
     • Removal of ZF2 ZendService namespace into a separate
       namespace and repository.




11
Goals of ZF2 Services

     • Ability to version separately from core framework
     • Easier to leverage outside of a ZF context
     • Encourage service providers to further contribute




12
Services for Contribution (Official Services)

     • Independently versioned
     • Dependencies on framework versions (2.*, 2.0.*, 2.0.1)
     • Maintain dependencies by specific packages
     • Must follow ZF coding standards
     • Must be unique
        A service that does the same thing should not already exist!




13
Service Component Lifecycle



                             Discuss on         IRC Vote
      Build Component    Contributor Mailing    (Meeting
                                List            Agenda)




        CR Team Review       Fork Repo         Publish!




14
Service Maintenance

     • Must be maintained for the duration of major versions
        Exceptions must be noted in the ChangeLog

        Component should only state dependency on minor versions

     • Maintainers must attempt at all times to keep compatibility
       with the latest version
        If unable to maintain, actively recruit, if still unable ZF or CR team
        will make a recommendation on the component.




15
How are we doing so far?




16
Writing a Service
By example of ZendServiceTwitter
General Information

     • Services are really just like framework libraries
        However, the namespace implies 3rd party integrations.

        They are also organized like the framework.

     • Services should be reusable for other developers
        Write it out based on the API and not just what you need.

     • Create reasonable dependencies
        ZendHttp and ZendStdlib being most common.




18
Why Not Modules?

     • Modules are more specifically for ZF
     • Components are reusable libraries for any code base.
     • Base Rule
        If it involves the MVC; it should more than likely be a module.




19
Starting Off..

     • Build out the file structure:
        library/ZendService/Twitter

        tests/ZendService/Twitter

     • Add LICENSE, README, .travis.yml, .gitignore
        Just copy these over from another project

        Update the readme for the project




20
Create Your Composer File




21
Dependencies




22
Unit Testing

     • Because we all do TDD right?
        From a module; copy over the _autoload.php, Bootstrap.php,
        phpunit.xml.dist, TestConfiguration.php.dist,
        TestConfiguration.php.travis
     • You will need to customize
        phpunit.xml.dist – Change out the unit test name.

        TestConfiguration.php.dist – Configure your constants and
        configuration.




23
Unit Testing

     • Tests should contain proper namespacing
        ZendServiceTestTwitter
         • Right now the Twitter component just uses ZendTestTwitter J

     • Files you need should be located in an _files directory
       inside the test area of your component
        tests/ZendService/Twitter/_files

     • Write unit tests like normal!




24
Library Code!

     • The guts of the library is what most of us are familiar with
     • Write your library inside of your compliant directory
        library/ZendService/Twitter

     • Your first file should very well likely be that of the service
       name
        library/ZendService/Twitter/Twitter.php

     • Ensure proper namespacing
        ZendServiceTwitter




25
Coding Standards

     • Remember to make use of the coding standards!
        Docblocks
         • @category    Zend
         • @package      Zend_Service
         • @subpackage Twitter
        Import the proper packages from their respective namespaces
         • No one likes to write out the full namespace time and time again…
        Read the coding standards doc:
         • http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards
         • Mainly; PSR-0, PSR-1, PSR-2 (there are likely slight differences)



26
Documentation

     • The most dreaded part of the job…
     • All of the documentation is in the “zf2-documentation”
       project under the “zendframework” github organization.
        This will likely change for services in the future.



     • Fork the project
     • Create a feature branch: feature/twitter
     • Write your documentation
     • Submit a PR


27
Documenting

     • Documentation is built out of reStructuredText
        Yes, Docbook has gone away; so fear not!

     • Language Files
        Always start with English first aka “en” as it is the default in the
        event a translation is missing.
     • Modify docs/language/en/index.rst to add in your new
       service
        Follow the convention; for a service you will use
        zendservice.twitter.intro.rst
     • Create your file
        docs/language/en/modules/zendservice.twitter.intro.rst or the
        like
28
Seeing your Documentation

     • Install the proper tools
        apt-get install python-setuptools python-pygments

        easy_install -U Sphinx

     • Enter the docs/ directory
     • Run: make html


     • Your documentation will now be in:
        docs/_build/html

        Review for errors before sending the PR



29
Integrating your Service
We will leverage the Skeleton Application

     • Add the module to the composer configuration
     • Add in potential configuration
     • Setup the service manager
     • Fetch it inside of a controller




31
Composerize




      php composer.php install
32
Potential Configuration




33
Setup the Service Manager




34
Controller Integration




35
Questions?

     •  These slides will be posted to SlideShare & SpeakerDeck.
        SpeakerDeck: http://speakerdeck.com/u/mwillbanks

        Slideshare: http://www.slideshare.net/mwillbanks

        Twitter: mwillbanks

        G+: Mike Willbanks

        IRC (freenode): mwillbanks

        Blog: http://blog.digitalstruct.com

        GitHub: https://github.com/mwillbanks




36

Writing Services with ZF2

  • 1.
    Writing Services with ZF2 Mike Willbanks | Barnes & Noble
  • 2.
    Housekeeping… •  Talk  Slides will be posted after the talk. •  Me  Sr. Web Architect Manager at NOOK Developer  Open Source Contributor  Where you can find me: •  Twitter: mwillbanks G+: Mike Willbanks •  IRC (freenode): mwillbanks Blog: http://blog.digitalstruct.com •  GitHub: https://github.com/mwillbanks 2
  • 3.
    Agenda •  Background •  ZF2 Services •  Writing a Service •  Implementing your Service 3
  • 4.
  • 5.
    A Definition ZF 1 contained Zend_Service_* components 5
  • 6.
    Zend_Service_* Components were included in the core distribution 6
  • 7.
    Zend_Service_* Make it easy for people to have it available… 7
  • 8.
    1.11.13 Release Caused several issues 8
  • 9.
  • 10.
  • 11.
    Changing for theBetter • RFC March 2012 “RFC Service Components”   http://framework.zend.com/wiki/display/ZFDEV2/RFC+-+Service +Components • Removal of ZF2 ZendService namespace into a separate namespace and repository. 11
  • 12.
    Goals of ZF2Services • Ability to version separately from core framework • Easier to leverage outside of a ZF context • Encourage service providers to further contribute 12
  • 13.
    Services for Contribution(Official Services) • Independently versioned • Dependencies on framework versions (2.*, 2.0.*, 2.0.1) • Maintain dependencies by specific packages • Must follow ZF coding standards • Must be unique   A service that does the same thing should not already exist! 13
  • 14.
    Service Component Lifecycle Discuss on IRC Vote Build Component Contributor Mailing (Meeting List Agenda) CR Team Review Fork Repo Publish! 14
  • 15.
    Service Maintenance • Must be maintained for the duration of major versions   Exceptions must be noted in the ChangeLog   Component should only state dependency on minor versions • Maintainers must attempt at all times to keep compatibility with the latest version   If unable to maintain, actively recruit, if still unable ZF or CR team will make a recommendation on the component. 15
  • 16.
    How are wedoing so far? 16
  • 17.
    Writing a Service Byexample of ZendServiceTwitter
  • 18.
    General Information • Services are really just like framework libraries   However, the namespace implies 3rd party integrations.   They are also organized like the framework. • Services should be reusable for other developers   Write it out based on the API and not just what you need. • Create reasonable dependencies   ZendHttp and ZendStdlib being most common. 18
  • 19.
    Why Not Modules? • Modules are more specifically for ZF • Components are reusable libraries for any code base. • Base Rule   If it involves the MVC; it should more than likely be a module. 19
  • 20.
    Starting Off.. • Build out the file structure:   library/ZendService/Twitter   tests/ZendService/Twitter • Add LICENSE, README, .travis.yml, .gitignore   Just copy these over from another project   Update the readme for the project 20
  • 21.
  • 22.
  • 23.
    Unit Testing • Because we all do TDD right?   From a module; copy over the _autoload.php, Bootstrap.php, phpunit.xml.dist, TestConfiguration.php.dist, TestConfiguration.php.travis • You will need to customize   phpunit.xml.dist – Change out the unit test name.   TestConfiguration.php.dist – Configure your constants and configuration. 23
  • 24.
    Unit Testing • Tests should contain proper namespacing   ZendServiceTestTwitter • Right now the Twitter component just uses ZendTestTwitter J • Files you need should be located in an _files directory inside the test area of your component   tests/ZendService/Twitter/_files • Write unit tests like normal! 24
  • 25.
    Library Code! • The guts of the library is what most of us are familiar with • Write your library inside of your compliant directory   library/ZendService/Twitter • Your first file should very well likely be that of the service name   library/ZendService/Twitter/Twitter.php • Ensure proper namespacing   ZendServiceTwitter 25
  • 26.
    Coding Standards • Remember to make use of the coding standards!   Docblocks • @category Zend • @package Zend_Service • @subpackage Twitter   Import the proper packages from their respective namespaces • No one likes to write out the full namespace time and time again…   Read the coding standards doc: • http://framework.zend.com/wiki/display/ZFDEV2/Coding+Standards • Mainly; PSR-0, PSR-1, PSR-2 (there are likely slight differences) 26
  • 27.
    Documentation • The most dreaded part of the job… • All of the documentation is in the “zf2-documentation” project under the “zendframework” github organization.   This will likely change for services in the future. • Fork the project • Create a feature branch: feature/twitter • Write your documentation • Submit a PR 27
  • 28.
    Documenting • Documentation is built out of reStructuredText   Yes, Docbook has gone away; so fear not! • Language Files   Always start with English first aka “en” as it is the default in the event a translation is missing. • Modify docs/language/en/index.rst to add in your new service   Follow the convention; for a service you will use zendservice.twitter.intro.rst • Create your file   docs/language/en/modules/zendservice.twitter.intro.rst or the like 28
  • 29.
    Seeing your Documentation • Install the proper tools   apt-get install python-setuptools python-pygments   easy_install -U Sphinx • Enter the docs/ directory • Run: make html • Your documentation will now be in:   docs/_build/html   Review for errors before sending the PR 29
  • 30.
  • 31.
    We will leveragethe Skeleton Application • Add the module to the composer configuration • Add in potential configuration • Setup the service manager • Fetch it inside of a controller 31
  • 32.
    Composerize php composer.php install 32
  • 33.
  • 34.
  • 35.
  • 36.
    Questions? •  These slides will be posted to SlideShare & SpeakerDeck.   SpeakerDeck: http://speakerdeck.com/u/mwillbanks   Slideshare: http://www.slideshare.net/mwillbanks   Twitter: mwillbanks   G+: Mike Willbanks   IRC (freenode): mwillbanks   Blog: http://blog.digitalstruct.com   GitHub: https://github.com/mwillbanks 36