Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

ZF2: Writing Service Components


Published on

Writing Service Components for Zend Framework 2.

Published in: Technology
  • Be the first to comment

ZF2: Writing Service Components

  1. 1. ZF2: WritingService ComponentsMike Willbanks | Barnes & NobleZend Webinar: Thursday, May 2nd 2013
  2. 2. 2Housekeeping…•  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:•  GitHub:
  3. 3. 3Agenda• Background• ZF2 Service Components• Writing a Service Component• Incorporating your Service Component
  4. 4. Background…Why Service Components
  5. 5. 5A DefinitionZF 1 contained Zend_Service_* components
  6. 6. 6Zend_Service_*Components were included in the coredistribution
  7. 7. 7Zend_Service_*Make it easy for people to have it available…
  8. 8. 81.11.13 ReleaseCaused several issues
  9. 9. 9Started Getting Messy…
  10. 10. Service Component OverviewGoals, Lifecycle, and more.
  11. 11. 11RFC March 2012 “RFC Service Components”Removal of ZF2 ZendService namespace into a separate namespace(ZendService) and under their own GitHub project.
  12. 12. Goals of ZF2 Service Components• Ability to version separately from core framework• Easier to leverage outside of a ZF context• Encourage service providers to further contribute
  13. 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!
  14. 14. 14Discuss onContributor MailingListCR Team Review Fork RepoBuild ComponentIRC Vote(MeetingAgenda)Publish!The Service Component Lifecycle
  15. 15. 15Published ZendService PackagesA published package for ZendService components exist
  16. 16. Service Component 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 compatibilitywith the latest version If unable to maintain, actively recruit, if still unable ZF or CR teamwill make a recommendation on the component.
  17. 17. Writing a Service ComponentBy example of ZendServiceGoogleGcm
  18. 18. General Information• Service Components are really just like framework libraries However, the namespace implies 3rd party integrations. They are also organized like the framework.• Service Components should be reusable for otherdevelopers Write it out based on the API and not just what you need.• Create reasonable dependencies ZendHttp and ZendStdlib being most common.
  19. 19. 19Why Not Modules?
  20. 20. Why Not Modules?• Modules are more specifically for ZF2 Applications• Service Components are reusable libraries for any codebase.• Base Rule If it involves the MVC; it should more than likely be a module.
  21. 21. 21Starting off…The file structure of a Service Component.
  22. 22. 22ZendService Components License
  23. 23. 23Composer StubMain information for your specific library.
  24. 24. 24Composer StubEnsure you handle dependencies.
  25. 25. Unit Testing• Quick Start Clone an existing Service Component (Currently no skeleton) Copy over files from the tests directory: _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 andconfiguration.
  26. 26. Unit Testing• Tests should contain proper name spacing ZendServiceTestGoogleGcm• Files you need should be located in an _files directoryinside the test area of your component tests/ZendService/Google/Gcm/_files• Write unit tests like normal!
  27. 27. 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/Google/Gcm• Ensure proper namespacing ZendServiceGoogleGcm• Attempt to follow great naming so that it makes sense!
  28. 28. Coding Standards• Remember to make use of the coding standards! Docblocks Import the proper packages from their respective namespaces Read the coding standards doc:•• Mainly; PSR-0, PSR-1, PSR-2 (there are slight differences)
  29. 29. Best Practices• Follow conventions PSR-0/1/2, file locations, options classes• Hard dependencies Use the constructor! Only set dependencies for items you require!• Write Tests Hook into Travis-CI, go for 100% code coverage• Discoverability Put the component on Packagist, submit it for inclusion toZendService.
  30. 30. 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/service-google-gcm• Write your documentation• Submit a PR
  31. 31. 31Documentation is built out of reStructuredTextYes, Docbook has gone away; so fear not!
  32. 32. 32Language FilesAlways start with English first aka “en” as it is the default in the eventa translation is missing.
  33. 33. 33Modify docs/language/en/index.rst to add in your new serviceHaving a table of contents is a good thing!
  34. 34. 34Modify docs/language/en/index.rst to add in your new serviceFollow the convention!
  35. 35. 35Write Away!docs/language/en/modules/ or the like
  36. 36. 36Seeing your Documentation• Install the proper tools apt-get install python-setuptools python-pygments easy_install -U Sphinx• Enter the docs/ directory• Run: make html
  37. 37. 37Your documentation will now be in:docs/_build/htmlReview for errors before sending the PR
  38. 38. Integrating your ServiceAdd the module to the composer configurationAdd in potential configurationSetup the service managerFetch it inside of a controller
  39. 39. 39Add to your composer.json!Followed by “php composer.phar install”
  40. 40. 40Handling ConfigurationLikely in /path/to/zf2app/config/autoload/local.php
  41. 41. 41Service Manager SetupCreating a simple factory to get a configured Client.
  42. 42. 42Leveraging your Service Factory in a ControllerWe can easily fetch it now when we have a ServiceLocator.
  43. 43. Questions?These slides will be posted to SlideShare & SpeakerDeck. SpeakerDeck: Slideshare: Twitter: mwillbanks G+: Mike Willbanks IRC (freenode): mwillbanks Blog: GitHub: